Helix Core is now P4. Product user interfaces will be updated soon. Learn more >>

P4 API for Perl Documentation | 2025.1

# Programming with P4 API for Perl

## Connect to P4 Server

The following example shows how to connect to a P4 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('bruno');
    $p4->SetPort('localhost:1666');
    $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 P4 Server over SSL

Scripts written with P4 API for Perl 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.

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.

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 );

## Adding and submitting multiple files

The following example creates sample files, marks them for adding to the default changelist and submits the change.

    #!/opt/local/bin/perl -w
    use strict;
    use P4;

    my $p4 = new P4;
    $p4->SetClient('bruno_ws');
    $p4->SetUser('bruno');
    $p4->SetPort('localhost:1666');
    $p4->Connect() or die("Was not able to connect\n");

    #We are creating test files to be added to a changelist

    #We then use the RunAdd method on the newly created file, to mark it for add in the default changelist

    foreach my $f ( qw( file_1 file_2 file_3 ) ) {
      my $p = $f;
      open( FH, ">$p" ) or die( "Can't create '$p'" );
      print( FH "This is a test file\n" );
      close( FH );
      $p4->RunAdd( $p );
    }

    #Create a change object and using the FormatChange method, we print the Change spec and see what we are adding

    my $change = $p4->FetchChange();
    $change->{ 'Description' } = "Adding test files";
    my $form = $p4->FormatChange($change);
    printf("Submitting this change \n\n%s\n", $form);

    #Submit the change

    $p4->RunSubmit( $change );
    $p4->Disconnect();

Your search for returned results