#! /usr/bin/perl
use strict;
use warnings;
use Cwd;
use Data::Dumper;
## Use the P4Perl API module
use P4;
## Set your username and password for the Workshop
my $user = "paul_allen";
my $pass = "password";
## Create a new p4 instance.
my $p4 = new P4;
## Set the environment for the p4 instance
$p4->SetPort( "public.perforce.com:1666" );
## Connect to the Perforce Server
$p4->Connect() or die( "Failed to connect to Perforce Server" );
## Login with Password or Ticket value
$p4->SetUser( $user );
$p4->SetPassword( $pass );
$p4->RunLogin();
die( "Failed to login" ) if $p4->ErrorCount();
## Set the name of the workspace we want to create/use
my $client_name = $user . ".perl-sdk.test.ws";
$p4->SetClient( $client_name );
## Create Client Workspace
my $client = $p4->FetchClient();
## ... set 'Root' - root location of workspace on disk.
$client->{'Root'} = cwd();
## ... set 'View' - an array of depot path mappings into workspace.
$client->{'View'} = [
"//guest/$user/perl-sdk/test/... //$client_name/..."
];
## Save the Client Workspace
$p4->SaveClient( $client );
## Check for errors or warnings
print( Dumper $p4->Errors() ) if $p4->ErrorCount();
print( Dumper $p4->Warnings() ) if $p4->WarningCount();
## Sync the latest changes from the Server
$p4->RunSync();
## Create some changes
open( my $fh, '>>', 'file.txt' ) or die;
say $fh "change by user: $user";
close $fh;
## Check status of the files in the Workspace
my $files = $p4->RunReconcile();
print( Dumper $files );
## Submit the changes file(s)
$p4->RunSubmit( "-d", "My Change Description" );
## Close the Perforce Server connection
$p4->Disconnect();