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";
In reply to parsing parameters in a new() object call by YaRness
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |