Click here to get back home

Stuffing @users into $self->

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Stuffing @users into $self-> Philluminati 05-01-2008
Posted by J. Gleixner on May 1, 2008, 1:59 pm
Please log in for more thread options
                Philluminati wrote:

> package user_management;

Generally you don't want a lower case name for your package.

>
> use user;
> #use strict;

Why comment that out?

>
> sub new
> {
> my ($class_name) = @_;
>
> my ($self) = {};
> bless ($self, $class_name);
Little cleaner as:

my $self = bless {}, $class_name;

>
>         #some objects for the array

This stuff would typically be found in an init method, or
passed in to new or something other than having new
create it. Keep your new method short and simple.

>         my $phill = new user;
>         $phill->setId(6);
>         $phill->setUsername("ptaylor");
>         $phill->setPassword("password");
>         $phill->setSessionKey("");
[...]
>
>         #put them in the array
>         my @users = ( $phill, $simon, $nick );
>
>         $self-> = \@users; #put the array in an instance variable
> (or perl equiv)

You might want to create an add_user method, instead of doing that. e.g.

$self->add_user( $phill );

That way it'd be much easier to test also. You would call the add_user
from whatever is using this class.


>         $self-> = 1;

Probably want a nextSessionId and/or sessionId method for that too.

>
> return $self;
>
> }
>
> sub login
> {
>         my ($self, $username, $password) = @_;

>         my @users = @{ $self-> };

Create a get_user method.

>
>         foreach my $user (@users)

You probably want to use a hashref, instead of an array, that
way you have one look-up instead of iterating over many
@users.

>         {
>                 if ($user->getUsername() eq $username)
>                 {
>                         if ($user->getPassword() eq $password)
>                         {
>                                 if ($user->getSessionKey ne "")
>

]...]
> --
>
> 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.

Based on your code above, using a hash would be much more efficient.

Posted by Chris Mattern on May 1, 2008, 4:04 pm
Please log in for more thread options
>                 Philluminati wrote:
>
>> package user_management;
>
> Generally you don't want a lower case name for your package.
>
>>
>> use user;
>> #use strict;
>
> Why comment that out?
>
Dealing with errors raised by strict by commenting out "use strict;"
is like dealing with a bleeding wound by closing your eyes so you
don't have to look at the blood any more.

--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities

Posted by szr on May 1, 2008, 11:15 pm
Please log in for more thread options
Chris Mattern wrote:
>> Philluminati wrote:
>>
>>> package user_management;
>>
>> Generally you don't want a lower case name for your package.
>>
>>>
>>> use user;
>>> #use strict;
>>
>> Why comment that out?
>>
> Dealing with errors raised by strict by commenting out "use strict;"
> is like dealing with a bleeding wound by closing your eyes so you
> don't have to look at the blood any more.


Maybe a better analogy would be putting tape over the warning lights
(check engine, ...) in your car.

--
szr



Posted by Lawrence Statton on May 2, 2008, 11:41 am
Please log in for more thread options
>
> Maybe a better analogy would be putting tape over the warning lights
> (check engine, ...) in your car.
>

That reminds me, I need to buy more tape.


--
        Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
place them into the correct order.

Similar ThreadsPosted
Any RT users November 23, 2004, 10:11 pm
On redhat, different users = different @INC August 20, 2007, 3:00 pm
Mac Users cannot access perl CGI June 15, 2005, 9:50 pm
NewsPro Users Webring November 18, 2005, 1:15 am
Maybe difficult for English Users - but I ask anyway January 20, 2006, 3:18 am
Best way to keep registered users logged in? October 21, 2006, 4:40 am
removing users from /etc/group September 12, 2007, 11:57 pm
web based bibliography creation by users January 1, 2005, 5:18 pm
file upload failing for some users June 23, 2005, 10:17 am
perl better than python for users with disabilities? December 20, 2006, 11:11 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap