YaRness has asked for the wisdom of the Perl Monks concerning the following question:
and this package.pl that calls it#!/usr/local/bin perl -w use strict; package Yar; sub new { my $class = shift; my $self = {}; my %params = @_; foreach my $token (keys %params) { if (exists &$token) { $self->{uc $token} = $params{$token}; } else { warn "We don't have any \"$token\" thingies 'round these p +arts!\n"; } } bless($self); return $self; } sub name { my $self = shift; if (@_) {$self->{'NAME'} = shift;return 1} return $self->{'NAME'}; } sub age { my $self = shift; if (@_) {$self->{'AGE'} = shift;return 1} return $self->{'AGE'}; } return 1;
Which outputs exactly what it should, ie, it prints the warn line since we tried to add an arbitrary parameter to the new() call, and then prints the two parameters we set. What I'd like to know is can I replace the "$self->{uc $token} = $params{$token};" with an actual call to the subroutines in the package. I got various errors about stuff not being blessed, or other things I can't remember, and I was wondering if anyone had any input on how to do this, or if I should even bother.#!/usr/local/bin/perl -w use strict; use Yar; my $yarobj = new Yar(name=>"YaR",age=>"23",foo=>"bar"); print "Output: ",$yarobj->name,$yarobj->age,"\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parsing parameters in a new() object call
by chromatic (Archbishop) on Jan 25, 2001 at 02:08 UTC | |
|
(tye)Re: parsing parameters in a new() object call
by tye (Sage) on Jan 25, 2001 at 00:43 UTC | |
|
Re: parsing parameters in a new() object call
by goldclaw (Scribe) on Jan 25, 2001 at 00:21 UTC | |
|
Re: parsing parameters in a new() object call
by mr.nick (Chaplain) on Jan 25, 2001 at 00:21 UTC | |
by YaRness (Initiate) on Jan 25, 2001 at 18:10 UTC | |
|
thanks guys
by YaRness (Initiate) on Jan 25, 2001 at 18:23 UTC | |
by chipmunk (Parson) on Jan 25, 2001 at 21:40 UTC |