Re: Annoying HASH(0x...) in Output 
GMan wrote:

> 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 = { };

@friends is an array, not a hash....change @friends = {} to @friends =
();


>     $self->{'_friends'} = \\@friends;
>     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'} = \\@friends;
> }
>
>
> 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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


