Greetings!
I am trying to build a class that has a hash of objects. Specifically,
the class is UserList, a list of users of a system. Users are
represented by instances of the User class. Each user has a login
name, a first name, a last name, a telephone number and an E-mail
address.
The UserList class has a hash named 'users'. The login name from the
User object will also be used as the key to the hash. The value of the
hash will be a reference the User object that has the given login name.
The login name is stored in two places: the hash key and the User
object.
The line of code that is supposed to add the new User object to the
'users' hash of the UserList object is:
$self->{'users'}->{$loginName} = $user; #<== This is line 47
The error message is:
"Can't use string ("interrobang") as a HASH ref while "strict refs" in
use at UserList.pm line 47, <USERLIST> line 1."
"Interrobang" is the login name of the only user in the data file I am
testing this script against.
I am pretty much trying to get this to work by trial and error. I have
another class in which a similar idiom works, but I don't understand
why it works and this doesn't. While I understand the concept behind
references, the mechanics of referencing and dereferencing in Perl are
still a mystery to me. Could someone explain why Perl thinks my string
is a hash reference instead of a key?
The complete script, with the classes, is short enough to be included
in this message. It is below.
Thanks very much!
Rob Richardson
Here's the User class:
use warnings;
use strict;
package User;
sub new
{
my $class = shift;
my $self = {};
$self->{'loginName'} = '';
$self->{'title'} = '';
$self->{'firstName'} = '';
$self->{'lastName'} = '';
$self->{'phone'} = '';
$self->{'email'} = '';
bless $self, $class;
return $self;
}
my $motto = "Where no one has gone before!";
Here are the new(), Load() and AddUser() methods of the UserList class:
sub new
{
my $class = shift;
my $self = {};
# Create an anonymous empty hash to hold users
$self->{'users'} = {};
bless $self, $class;
return $self;
}
sub Load
{
my $self = shift;
my $fileName = shift;
my $user;
open (USERLIST, $fileName) || die "cannot open database $fileName:
$!\n";
if ($^O ne "MSWin32")
{
flock(USERLIST, 1);
}
while (<USERLIST>)
{
chomp;
AddUser (split /,/);
}
}
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; <== This is line 47
}
Finally, here is my test script, which creates a UserList, reads users
from a file, adds them to the UserList object, and nothing else:
#!/usr/bin/perl
use warnings;
use strict;
use UserList;
my $theList = new UserList;
$theList->Load("c:\\indigoperl\\htdocs\\data\\user.in");
print "Done!";
__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]