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).

Class::ParmList

Mature, lightweight, and robustly portable all the way back to Perl 5.00x (Obligatory Disclosure: I wrote it). It also handles optional parameters, required parameters, legal parameters, parameter defaults, anon hash vs list parameters, and 'case flattening' of passed parameter names.

#!/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"; }

Sub::Parameters

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).

Params::Named

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"; }

Params::Smart

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"; }

Params::Check

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"; }

Hash Assignment

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.