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