Programming with P4Perl

Connect to Helix Core server

The following example shows how to connect to a Helix Core server, run a p4 info command, and open a file for edit:

#!/opt/local/bin/perl -w
use strict;
use P4;
my $p4 = new P4;
$p4->SetClient('bruno_ws');
$p4->SetUser('smoon');
$p4->SetPort('localhost:20081');
$p4->SetVersion("EnvTest 1.0");
$p4->Connect() or die("Was not able to connect\n");
my $info = $p4->Run("info"); #passing array ref
print "\n\nP4 Info Output:\n\n";
foreach my $akey (@{$info}) {
my @infos = keys %$akey; # $akey is hash ref
foreach my $hkey (@infos) {
print "$hkey => $akey->{$hkey}\n";
}
}
my $client_name = $p4->FetchClient($p4->GetClient());
print "\n\nClient Specification:\n\n";
foreach my $chkey (keys %{$client_name}) {
if ($client_name->{$chkey} =~ /^ARRAY(.+)$/) {
my $avals = $client_name->{$chkey};
foreach my $achkey (@{$avals}) {
print "$chkey => $achkey\n"; }
} elsif($client_name->{$chkey} =~ /^HASH(.+)$/) {
my $hvals = $client_name->{$chkey};
foreach my $hchkey (keys %{$hvals}) {
print "$chkey => $hvals->{$hchkey}\n";
}
} else {
print "$chkey => $client_name->{$chkey}\n";
}
}
my $changes = $p4->Run("changes","-m2");
print "\n\nTwo Most Recent Changes:\n\n";
foreach my $each_chg (@{$changes}) {
my @chg_key = keys %$each_chg; # $each_chg is hash ref
foreach my $hchg (@chg_key) {
print "$hchg => $each_chg->{$hchg}\n";
}
print "\n";
}
print "\n" . $p4->GetVersion() . "\n";
$p4->Disconnect();

Connect to Helix server over SSL

Scripts written with P4Perl use any existing P4TRUST file present in their operating environment (by default, .p4trust in the home directory of the user that runs the script).

If the fingerprint returned by the server fails to match the one installed in the P4TRUST file associated with the script’s run-time environment, your script will (and should!) fail to connect to the server.

Converting forms between formats

Sometimes you have a form in a hash format, and want it in a string. Sometimes you have a string, and want a hash. In these situations, the following methods will help.

FormatSpec( $type, $hash )

Convert Perforce form hash to string

Converts a Perforce form of the specified type (client/label, and so on) held in the supplied hash into its string representation.

Note

Shortcut methods are available that obviate the need to supply the type argument.

The following two examples are equivalent:

$string = $p4->FormatSpec( "client", $hash );

$string = $p4->FormatClient( $hash );

See below for more information on the abbreviated form.

Format<type>( $hash ) is shorthand for $p4->FormatSpec( <type>, $hash )

For example:

$change	= $p4->FetchChange();
$change->{ 'Description' } = 'Some description';
$form 	= $p4->FormatChange( $change );
printf( "Submitting this change:\n\n%s\n", $form );
$p4->RunSubmit( $change );

Convert Perforce form string to hash

Converts a Perforce form of the specified type (client/label, and so on) in the supplied string into a hash and returns a reference to that hash.

Note

Shortcut methods are available to avoid the need to supply the type argument.

The following two examples are equivalent:

$hash = $p4->ParseSpec( "client", $string );

$hash = $p4->ParseClient( $clientspec );

See below for more information on the abbreviated form.

Parse<type>( $string ) is shorthand for $p4->ParseSpec( <type>, $string )

For example:

$hash = $p4->ParseClient( $string );
$hash = $p4->ParseLabel( $string );
$hash = $p4->ParseBranch( $string );
$hash = $p4->ParseProtect( $string );