|
|
|
|
|
Posted by cartercc on June 2, 2008, 10:22 am
Please log in for more thread options
Two classes, Person and Instructor, with Instructor inheriting from
Person.
Person constructor looks like this:
sub new
{
my $class = shift;
bless
{
_id => $_[0],
_first => $_[1],
_middle => $_[2],
_last => $_[3],
_dob => $_[4],
}, $class;
}
Instructor adds accessors and mutators for degree, rank, etc.
Instructor also has a method named print_instructor that looks like
this:
sub print_instructor
{
my $self = shift;
print "Instructor data:\n";
foreach my $key (%$self)
{
print "$key\t"; #line 1
#print "\n\t$key: $self->"; #line 2
#print "\n\t$self->"; #line 3
}
print "\n";
}
Here is the (partial) output from line 1. Note that I only ask for the
$key, but it prints the values as well.
Instructor data:
_id 2 _university Johns Hopkins University
_last Dewey
_first John _field Philosophy _middle nmn _rank
Professor
_degree Ph.D. _dob 20-OCT-1859
Here is the (partial) output from line 2. Note that it prints the $key
AND value but then repeats the value on the next line.
Instructor data:
_id: 2
2:
_university: Johns Hopkins University
Johns Hopkins University:
_last: Dewey
Dewey:
_first: John
John:
_field: Philosophy
Philosophy:
_middle: nmn
nmn:
_rank: Professor
Professor:
_degree: Ph.D.
Ph.D.:
_dob: 20-OCT-1859
20-OCT-1859:
Here is the (partial) output from line 3. Note that it prints just the
values, which is exactly the expected behavior.
Instructor data:
2
Johns Hopkins University
Dewey
John
Philosophy
nmn
Professor
Ph.D.
20-OCT-1859
Questions:
Why does line 2 print the values twice?
Why does line 1 print the values at all?
I confess, I'm clueless, CC.
|
|
Posted by J. Gleixner on June 2, 2008, 10:33 am
Please log in for more thread options
cartercc wrote:
> Two classes, Person and Instructor, with Instructor inheriting from
> Person.
>
> Person constructor looks like this:
> sub new
> {
> my $class = shift;
> bless
> {
> _id => $_[0],
> _first => $_[1],
> _middle => $_[2],
> _last => $_[3],
> _dob => $_[4],
>
> }, $class;
Your class would be more readable if you passed those arguments
via a hash.
> }
>
> Instructor adds accessors and mutators for degree, rank, etc.
> Instructor also has a method named print_instructor that looks like
> this:
> sub print_instructor
> {
> my $self = shift;
> print "Instructor data:\n";
> foreach my $key (%$self)
That's not how you get the 'keys' out of the hash.
perldoc -f keys
or
perldoc -f each
> I confess, I'm clueless, CC.
|
|
Posted by szr on June 2, 2008, 2:06 pm
Please log in for more thread options J. Gleixner wrote:
> cartercc wrote:
>> Two classes, Person and Instructor, with Instructor inheriting from
>> Person.
>>
>> Person constructor looks like this:
>> sub new
>> {
>> my $class = shift;
>> bless
>> {
>> _id => $_[0],
>> _first => $_[1],
>> _middle => $_[2],
>> _last => $_[3],
>> _dob => $_[4],
>>
>> }, $class;
>
> Your class would be more readable if you passed those arguments
> via a hash.
Please correct me if I'm missing something, but isn't he in fact passing
an anonymous hash (ref) to bless already?
Yes, he could declare a named ref to an anonymous hash, like
my $obj = { ... };
bless $obj, $class;
but in the end I think that makes little difference in this case.
--
szr
|
|
Posted by J. Gleixner on June 2, 2008, 2:44 pm
Please log in for more thread options szr wrote:
> J. Gleixner wrote:
>> cartercc wrote:
>>> Two classes, Person and Instructor, with Instructor inheriting from
>>> Person.
>>>
>>> Person constructor looks like this:
>>> sub new
>>> {
>>> my $class = shift;
>>> bless
>>> {
>>> _id => $_[0],
>>> _first => $_[1],
>>> _middle => $_[2],
>>> _last => $_[3],
>>> _dob => $_[4],
>>>
>>> }, $class;
>> Your class would be more readable if you passed those arguments
>> via a hash.
>
> Please correct me if I'm missing something, but isn't he in fact passing
> an anonymous hash (ref) to bless already?
[...]
I meant pass them to new() as named arguments:
e.g.
Person->new( 1234, 'abc', ... );
Is more readable and less error prone as:
Person->new( id => 1234, first => 'abc', ... );
|
|
Posted by szr on June 2, 2008, 3:37 pm
Please log in for more thread options J. Gleixner wrote:
> szr wrote:
>> J. Gleixner wrote:
>>> cartercc wrote:
>>>> Two classes, Person and Instructor, with Instructor inheriting from
>>>> Person.
>>>>
>>>> Person constructor looks like this:
>>>> sub new
>>>> {
>>>> my $class = shift;
>>>> bless
>>>> {
>>>> _id => $_[0],
>>>> _first => $_[1],
>>>> _middle => $_[2],
>>>> _last => $_[3],
>>>> _dob => $_[4],
>>>>
>>>> }, $class;
>>> Your class would be more readable if you passed those arguments
>>> via a hash.
>>
>> Please correct me if I'm missing something, but isn't he in fact
>> passing an anonymous hash (ref) to bless already?
> [...]
> I meant pass them to new() as named arguments:
>
> e.g.
>
> Person->new( 1234, 'abc', ... );
>
> Is more readable and less error prone as:
>
> Person->new( id => 1234, first => 'abc', ... );
Ah, indeed that is much better. Thanks for clearing that up.
--
szr
|
| Similar Threads | Posted | | Print dereferenced hash of arrays keys and values | May 24, 2006, 8:12 pm |
| Comparing values of multiple hash keys | July 26, 2006, 2:50 am |
| maximum hash/array keys/values | May 5, 2008, 8:04 pm |
| (",) Print This! Press [Ctrl][P] Keys To Print... >> http://www.phpbbserver.com/phpbb/viewtopic.php?t=2&mforum=anysubjectchat < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | May 30, 2005, 2:05 pm |
| Perl data structure to hold same multiple keys and different values | September 17, 2007, 6:38 am |
| * * * Please Read And/Or Print This! * * * Press [Ctrl][P] Keys On Your Keyboard To Print >> June 1, 2004 8:24:38 pm >> http://115639.aceboard.net/forum2.php?rub=158&cat=61&login=115639&page=0#id96 << * * * * * * * * * * * * * * * * * * * * * * * * * * * | June 1, 2005, 6:24 pm |
| References as hash keys (Srinivasan's "Advanced Perl Programming")? | June 27, 2006, 11:27 am |
| Modify keys in a %hash using tr/// or s/// | February 17, 2005, 8:03 pm |
| chomp hash keys? | April 28, 2006, 8:03 pm |
| using an array to set up hash keys | November 20, 2006, 7:44 pm |
|
|
|
|