[p4perl] Problems inheriting from P4

Tony Smith tony at smee.org
Wed Feb 2 08:08:57 PST 2005


Hi Terry,

> I am trying to implement my own derived class from the P4 class. Here is
> the code...
>
> package Perforce;
> use strict;
> use P4;
>
> our @ISA = qw(P4);
>
> sub new
> {
>     my $class = shift;
>     my $self = bless {}, $class;

That just creates an anonymous hash and blesses it into the class. It hasn't 
run any kind of constructor or initialized the Perforce API at all.

>     $self->{username} = shift;
>     $self->{password} = shift;
>     $self->{server} = shift;
>     $self->{clientspec} = shift;
>     return $self;
> }
>
> sub init
> {
>     my $self = shift;
>     $self->SetPort($self->{server});
>     $self->SetUser($self->{username});
>     $self->SetPassword($self->{password});
>     $self->SetClient($self->{clientspec});
>     $self->Init() or die("Failed to connect to Perforce\n");
> }
>
> 1;

Unfortunately it's currently (v3.x) difficult to derive from the P4 class. A 
P4 object is not a hash, it's a blessed reference to an integer, an I32 in 
Perl terms (oddly, an I32 can be 64-bits long), containing a pointer to a C++ 
object. 

I'll look into making it a blessed hashref and stashing the pointer as a 
member of the hash, but in the meantime perhaps it would help if you told us 
what you are trying to achieve - there might be another way?

Tony



More information about the p4perl mailing list