Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'd do:
sub convert { my %arg = @_; my ($from, $to, $thing) = @arg{qw /from to thing/}; ... }
I don't enforce policies of "even if you want to pass in an argument with an undefined value, you must specify it" on my callers. I'm a Perl programmer, so my programs are programmer friendly. Absence of named parameters are fine. I don't balk at extra arguments either - this can be very useful, as wrappers can just call the function with whatever @_ they were called with.

On the other hand, I might work with defaults. Then I'd write it as:

sub convert { my %arg = @_; my $from = exist $arg{from} ? $arg{from} : 'default'; my $to = exist $arg{to} ? $arg{to} : 'default'; my $thing = exist $arg{thing} ? $arg{thing} : 'default'; ... }
Or:
sub convert { my %arg = (from => 'default', to => 'default', thing => 'default', @_); my ($from, $to, $thing) = @arg{qw /from to thing/}; ... }
Or when passing in a false value doesn't make sense, I'd make it that any false value means 'default':
sub convert { my %arg = @_; my $from = $arg{from} || 'default'; my $to = $arg{to} || 'default'; my $thing = $arg{thing} || 'default'; ... }
And when I want to do something fancy, I use Getopt::Long:
sub convert { local @ARGV = @_; GetOptions ('from:s' => \my $from, 'to:s' => \my $to, 'thing:s' => \my $thing); ... }
Perl --((8:>*

In reply to Re: Your named arguments by Perl Mouse
in thread Your named arguments by Juerd

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found