I am trying to teach myself Perl Objects.

I have a Person object with a name and a list of friends.

Here is a test program that uses it.

#! /usr/bin/perl

use Person;

$p = new Person;

$p->setname("Gary");
$p->addfriend("Bill");
$p->addfriend("John");
$p->print();

Here is the output from the test program.

perl testfriends.pl
Name Gary
Friends URLs
        HASH(0x86ea69c)
        Bill
        John


How do I get rid of the Annoying HASH(0x86ea69c) in my output?

Here is the code to the object (Person.pm)

#! /usr/bin/perl

package Person;

sub new {
    my ($class) = @_;
    my ($self) = { };
    bless $self, $class;
    $self->{'_name'} = "";
    @friends = { };
    $self->{'_friends'} = [EMAIL PROTECTED];
    return $self;
}


sub getname {
    my ($self) = @_;
    return $self->('_name');
}


sub setname {
    my ($self,$name) = @_;
    $self->{'_name'} = $name if defined($name);
}


sub addfriend {
    my ($self,$f) = @_;
    #dereference the array from the hash
    $fr = $self->{'_friends'};
    @friends = @$fr;
    push @friends,$f;
    $self->{'friends'} = [EMAIL PROTECTED];
}


sub print {
    my ($self) = @_;
    printf("Name %s\n",$self->{'_name'});
    printf("Friends URLs\n");
    #dereference the array from the hash
    $fr = $self->{'_friends'};
    @u = @$fr;
    for $u (@u) {
        printf("\t%s\n",$u);
    }
}


1;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to