Programming with P4Perl

The following example shows how to connect to a Helix 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();

Connecting to Helix Core 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.