For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2004 > Reference confusion in object method call









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reference confusion in object method call
Rob Richardson

2004-05-23, 7:30 pm

Greetings!

References are good things. They are useful. They let you accomplish
useful things.

I hate them.

I have a class named User. Here is it's constructor:

sub new
{
my $class = shift;
my $self = {};
$self->{'loginName'} = '';
$self->{'title'} = '';
$self->{'firstName'} = '';
$self->{'lastName'} = '';
$self->{'phone'} = '';
$self->{'email'} = '';
bless $self, $class;
return $self;
}

I have a class named UserList. Here is its constructor:

sub new
{
my $class = shift;
my $self = {};
# Create an anonymous empty hash to hold users
$self->{'users'} = {};
bless $self, $class;

# If there was another argument, it will be the file name, and we can
load the list.
if (defined($_[0]))
{
$self->Load($_[0]);
}
return $self;
}

Here is the function that creates a user and adds it to the user list:

sub AddUser
{
my $self = shift;
my $user = new User;
($user->{'loginName'},
$user->{'title'},
$user->{'firstName'},
$user->{'lastName'},
$user->{'phone'},
$user->{'email'}) = @_;
my $loginName = $user->{'loginName'};
$self->{'users'}->{$loginName} = $user;
}

Here is the function of the UserList class that retrieves a user with a
given login name:

sub GetUser
{
my $self = shift;
my $userName = shift;
my $user = new User;

if (exists $self->{'users'}->{$userName})
{
$user = $self->{'users'}->{$userName};
}
else
{
$user = new User;
$user->{'loginName'} = 'unknown';
}
return $user;
}

I have a routine named trace() that writes a string to a text file.
The following two lines:

$user = $userList->GetUser($logname);
trace("User: $user->LoginName(), $user->FirstName(),
$user->LastName()\n");

give me the following result:

User: User=HASH(0x174c234)->LoginName(),
User=HASH(0x174c234)->FirstName(), User=HASH(0x174c234)->LastName()

I am losing track of what is a reference to what, and what needs to be
dereferenced into what, but I don't know why or what it is I need to
do. I've done similar things elsewhere in this application without
this problem, but I don't see what I'm doing differently. Can somebody
please explain this?

Thanks very much!

Rob Richardson






__________________________________
Do you Yahoo!?
Yahoo! Domains – Claim yours for only $14.70/year
http://smallbusiness.promotions.yahoo.com/offer
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com