Hello,
I was recently inspired by a recent thread ( to expand my skills in Perl, and begin to programme using OOP. I have read example code in my time, but never applied my knowledge. I decided I would create my first OOP programme, which is a simple script designed to create and manipulate "user profiles". Its working pretty well, but theres alot I still don't know / understand.
1) I'm a little confused with namespacing, specifically $class and $self. I have seen the first arguement been assigned to $class in one script, but the first arguement been assigned to $self in another similar script. The way I figured it was to use $class when creating a new object, then $self from then on. Does this make sense?
2) At the end of my code, I have commented out a section. Basically, if I want to print out every 'name' in @foundpeople, I could easily do "print $_->{'name'}, '<br />' foreach (@foundpeople);", but this breaks away from complete OOP. I could create another method but it wouldn't be as practical as to do it the way I have commented out. Somehow I need to bless $person into some class when constructing the object data, but i'm baffled on what to do. How could I get my commented section of code to work as expected ($person becomes a class), ignoring the dummy methods which I haven't created yet?
3) In order to have as much freedom as possible whilst manipulating data, I have been creating sub routine references as arguements. I have never seen it done this way, but I couldn't think of a better way. It works well, but is there a better, more useable / readable way to do it i.e. having to use $_[0] is pointless right, because all the method needs to know is e.g. 'name'?
I'm sorry if I haven't been clear enough, but its kind of difficult to explain when my knowledge is limited. I would appreciate any advice / improvements etc.
Thank you very much,
Chris
I was recently inspired by a recent thread ( to expand my skills in Perl, and begin to programme using OOP. I have read example code in my time, but never applied my knowledge. I decided I would create my first OOP programme, which is a simple script designed to create and manipulate "user profiles". Its working pretty well, but theres alot I still don't know / understand.
1) I'm a little confused with namespacing, specifically $class and $self. I have seen the first arguement been assigned to $class in one script, but the first arguement been assigned to $self in another similar script. The way I figured it was to use $class when creating a new object, then $self from then on. Does this make sense?
2) At the end of my code, I have commented out a section. Basically, if I want to print out every 'name' in @foundpeople, I could easily do "print $_->{'name'}, '<br />' foreach (@foundpeople);", but this breaks away from complete OOP. I could create another method but it wouldn't be as practical as to do it the way I have commented out. Somehow I need to bless $person into some class when constructing the object data, but i'm baffled on what to do. How could I get my commented section of code to work as expected ($person becomes a class), ignoring the dummy methods which I haven't created yet?
3) In order to have as much freedom as possible whilst manipulating data, I have been creating sub routine references as arguements. I have never seen it done this way, but I couldn't think of a better way. It works well, but is there a better, more useable / readable way to do it i.e. having to use $_[0] is pointless right, because all the method needs to know is e.g. 'name'?
I'm sorry if I haven't been clear enough, but its kind of difficult to explain when my knowledge is limited. I would appreciate any advice / improvements etc.
Thank you very much,
Chris
Code:
#//////////////////////////////////////////////////
#!/usr/bin/perl
use strict;
use warnings;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
print "Content-type: text/html\n\n";
warningsToBrowser(1);
#//////////////////////////////////////////////////
package profilePeople;
use strict;
use warnings;
use Data::Dumper;
sub new { #input=hash ref(s), output=object
my $class = shift;
my @people = @_;
my $self = [];
my $return = bless $self, $class;
$return->newPeople(@people);
return $return;
}
sub newPeople { #input=hash ref(s), output=array ref
my $self = shift;
my @people = @_;
my $return = [];
foreach my $person (@people) {
#push @$self, (bless $person, ?);
push @$self, $person;
push @$return, $person;
}
return $return;
}
sub updatePeople { #input=hash ref then subroutine ref, output=array ref
my $self = shift;
my $update = shift || {};
my $condition = shift || sub{1};
my $return = [];
foreach my $index (0 .. $#$self) {
if (&$condition($$self[$index])) {
while (my ($key, $value) = (each %$update)) {
#if ($value eq '') { delete $$self[$index]{$key}; next; }
$$self[$index]{$key} = $value;
push @$return, $$self[$index];
}
}
}
return $return;
}
sub deletePeople { #input=subroutine ref, output=array ref
my $self = shift;
my $condition = shift || sub{1};
my $return = [];
foreach my $index (0 .. $#$self) {
if (&$condition($$self[$index])) {
push @$return, $$self[$index];
delete $$self[$index];
}
}
return $return;
}
sub findPeople { #input=subroutine ref, output=array ref
my $self = shift;
my $condition = shift || sub{1};
my @return = grep { &$condition($_) } @$self;
return \@return;
}
sub dumpIt { #input=ref, output=scalar
my $self = shift;
my $return = Dumper($self);
return $return;
}
#//////////////////////////////////////////////////
#$object->updatePeople({'name' => 'newname', 'age' => '', 'location' => 'UK'}); #no 2nd arg = update everyone
#$object->deletePeople(); #no args = delete everyone
#my @foundpeople = @{$object->findPeople()}; #no args = find everyone
my $object = profilePeople->new({'name' => 'peter'}, {'name' => 'william'});
$object->newPeople({'name' => 'chris', 'age' => '20', 'sex' => 'male'}, {'name' => 'john', 'age' => '44', 'sex' => 'male'});
$object->newPeople({'name' => 'laura', 'age' => '22', 'sex' => 'female'});
$object->updatePeople({'name' => 'newname', 'age' => '', 'location' => 'UK'}, sub{$_[0]{'name'} eq 'chris'});
$object->deletePeople(sub{$_[0]{'name'} =~ m/^laura|john$/i});
my @foundpeople = @{$object->findPeople(sub{($_[0]{'age'} > 21) || ($_[0]{'name'} =~ m/W/i)})};
print $_->{'name'}, '<br />' foreach (@foundpeople);
print $object->dumpIt;
#foreach my $person (@foundpeople) {
# my @keys = $person->personKeys('22');
# my @values = $person->personValues('name', 'age');
#}
#//////////////////////////////////////////////////