|
Posted by Philluminati on May 1, 2008, 11:26 am
Please log in for more thread options On May 1, 4:16 pm, xhos...@gmail.com wrote:
> > Hi everyone.
>
> > I'm writing a SOAP Server in perl and I need/want to use objects. I
> > have an array called @users which is populated in sub new. I want to
> > store this array in my $self variable which is passed to all functions
> > and then pull it out later. Can somebody help me?
>
> > sub new
> > {
> > my ($class_name) = @_;
> > my ($self) = {};
> > bless ($self, $class_name);
>
> > my $phill = new user;
> > $phill->setId(6);
> > $phill->setUsername("ptaylor");
> > $phill->setPassword("password");
> > $phill->setSessionKey("");
>
> > #cut some code out to stop the post being enormous.
>
> > my @users = ( $phill );
>
> @users will hold exactly one thing, $phill. Why use an array for that?
>
>
>
>
>
> > $self-> = [ @users ];
>
> > return $self;
>
> > }
> > sub login
> > {
> > my ($self, $username, $password) = @_;
> > my @users = @$self->; #<--
> > THIS IS LINE 62
>
> > how I get this message in my VB Soap Client:
>
> > Can't use string ("user_management") as an ARRAY ref while "strict
> > refs" in use at user_management.pm line 62.
>
> The problem is in some part of the code you didn't show us.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.
The array only held one value because I didn't want to post an
enourmous block of text. Anyway here is the entire copy of the code.
Each instance gets an array of three people called @users. It's shoved
into $self->. Then in each function I want to pull it out
again but I can't seem to do it. The query functions shows there are
no users what so ever in the array:
#!/usr/bin/perl -w
package user_management;
use user;
#use strict;
sub new
{
my ($class_name) = @_;
my ($self) = {};
bless ($self, $class_name);
#some objects for the array
my $phill = new user;
$phill->setId(6);
$phill->setUsername("ptaylor");
$phill->setPassword("password");
$phill->setSessionKey("");
my $simon = new user;
$simon->setId(7);
$simon->setUsername("sroberts");
$simon->setPassword("cheese");
$simon->setSessionKey("");
my $nick = new user;
$nick->setId(8);
$nick->setUsername("nick");
$nick->setPassword("rock");
$nick->setSessionKey("");
#put them in the array
my @users = ( $phill, $simon, $nick );
$self-> = \@users; #put the array in an instance variable
(or perl equiv)
$self-> = 1;
return $self;
}
sub login
{
my ($self, $username, $password) = @_;
#print "usr: $username\npwd: $password\n";
#my $size = @users;
#;return "ERR: Count is $size";
#return "ERR: I GOT $username, $password";
my @users = @{ $self-> };
foreach my $user (@users)
{
if ($user->getUsername() eq $username)
{
if ($user->getPassword() eq $password)
{
if ($user->getSessionKey ne "")
{
return "ERR: User already logged in";
}
else
{
#generate session key
$user->setSessionKey("SESS" . $self->);
$self-> = $self-> + 1;
return $user->getSessionKey();
}
}
else
{ return "ERR: Password is wrong"; }
}
}
return "ERR: No users";
}
sub query
{
my $self = shift;
my @users = @{ $self-> };
my $active = 0;
my $total = @users;
my $retval = "";
foreach my $usr (@users)
{
if ($usr->getSessionKey() ne "")
{
$retval = $retval . "user " . $usr->getUsername() . " logged in\n";
$active++;
}
else
{
$retval = $retval . "user " . $usr->getUsername() . " logged out
\n";
}
}
return "$retval$active of $total";
}
sub logout
{
my ($self, $sessionKey) = @_;
my @users = @{ $self-> };
foreach my $usr (@users)
{
if ($usr->getSessionKey() eq $sessionKey)
{
$usr->setSessionKey("");
return 1;
}
}
return 0;
}
1;
--
Before anyone makes a note of it - I am aware that each instance of
the user_management object will get a duplicate array. This isn't my
primary concern at the moment however. I just want to be able to store
arrays inside object instances.
|