There are about a half dozen modules on CPAN including Params::Check, Params::Smart, Params::Named, Sub::Parameters, and Class::ParmList (I've omitted some modules that perform validation but no actual parsing of the passed parameters).
#!/usr/bin/perl use strict; use Class::ParmList qw (simple_parms parse_parms); my_sub( handle => 'Test', 'thing' => 'something'); other_sub( handle => 'Test', 'other' => 'oops'); sub my_sub { my ($handle, $thing) = simple_parms([qw(handle thing)], @_); print "HANDLE: $handle\nTHING: $thing\n"; } sub other_sub { my ($handle, $thing, $other) = parse_parms( -parms => \@_, -legal => ['handle','thing'], -required => ['other'], -defaults => {'thing' => 'uncle'}, )->get('handle','thing','other'); print "HANDLE: $handle\nTHING: $thing\nOTHER: $other\n"; }
Appears elegant, but has several module dependencies and is fragile. I could not get it to succcessfully install on any version of Perl I have on my desk machine (5.005_04, 5.6.2, 5.8.0 or 5.8.6).
Only works for Perl 5.8.x or later but is pretty clean. It can't handle 'case flattening', however.
#!/usr/bin/perl use strict; use Params::Named; my_sub( handle => 'Test', 'thing' => 'something'); sub my_sub { MAPARGS \my($handle, $thing); print "HANDLE: $handle\nTHING: $thing\n"; }
Requires at least Perl 5.6.x. Handles required and optional parameters and has advanced processing options available.
#!/usr/bin/perl use strict; use Params::Smart; my_sub( handle => 'Test', 'thing' => 'something'); sub my_sub { my %args = Params(qw(handle thing ?other))->args(@_); print "HANDLE: $args{handle}\nTHING: $args{thing}\nOTHER: $args{ot +her}\n"; }
Portable (installs on Perl 5.00x and later), very powerful (supports optional, legal, required, validation, callbacks, and defaulted parameters and handles case flattening). Its downside is that there really isn't a 'simple' mode.
#!/usr/bin/perl use strict; use Params::Check qw(check); my_sub( handle => 'Test', 'thing' => 'something'); other_sub( handle => 'Test', 'other' => 'oops'); sub my_sub { my %hash = @_; my $x; my $tmpl = { handle => { required => 1 }, thing => { required => 1 }, }; my $args = check( $tmpl, \%hash ) or die("Failed to parse args"); print "HANDLE: $args->{handle}\nTHING: $args->{thing}\n"; } sub other_sub { my %hash = @_; my $x; my $tmpl = { handle => { required => 0 }, thing => { required => 0, default => 'uncle' }, other => { required => 1 }, }; my $args = check( $tmpl, \%hash ) or die("Failed to parse args"); print "HANDLE: $args->{handle}\nTHING: $args->{thing}\nOTHER: $arg +s->{other}\n"; }
And there is always the classic, no bells and whistles, but fast and portable hash assignment:
#!/usr/bin/perl use strict; my_sub( handle => 'Test', 'thing' => 'something'); sub my_sub { my %args = @_; my ($handle, $thing) = @args{'handle','thing'}; print "HANDLE: $handle\nTHING: $thing\n"; }
In reply to Re: How to make an object method read named parameters?
by snowhare
in thread How to make an object method read named parameters?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |