Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Case-insensitive, dash-optional named parameters for your functions

by iblech (Friar)
on Nov 08, 2004 at 16:11 UTC ( [id://406096]=note: print w/replies, xml ) Need Help??


in reply to Case-insensitive, dash-optional named parameters for your functions

# Perl 6 :) sub restart_server (Str +$host, Int +$port, Int +$timeout) { ...; } # Call using restart_server host => "...", port => ..., timeout => ...; # or restart_server :host«...», :port(...), :timeout(...);

By just adding a + in front of the variable name you can (actually have to) pass this parameter named.

I noticed, in Perl 5, that you often have only a little small function taking only one parameter:

# Perl 5 sub foo { my $a = shift; ...; }

Later on, you see the need to add a second positional parameter. And maybe your small function won't stop growing and you require a third and fourth parameter, too.

sub foo { my ($a, $b, $c, $d) = @_; ...; }

You think, "I should use named paramaters here", but you (read: I) are probably too lazy to rewrite foo to:

sub foo { my %args = @_; my ($a, $b, $c, $d) = @args{qw( a b c d )}; # Or: my ($a, $b, $c, $d) = @{{@_}}{qw( a b c d )}; ...; }

Instead, you keep your positional argument calling (bad). But in Perl 6, all you'll have to do is to add one small little +, and (of course) you won't have to extract the parameters out of @_ yourself! And you get compile-time checking, too :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://406096]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found