Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Preferred technique for named subroutine parameters?

by shmem (Chancellor)
on May 23, 2009 at 10:08 UTC ( [id://765812]=note: print w/replies, xml ) Need Help??


in reply to Re: Preferred technique for named subroutine parameters?
in thread Preferred technique for named subroutine parameters?

Valid point, but Carp::cluck tells you of the invocation point, also. If you would want that globally, you could $SIG{__WARN__} = \&Carp::cluck and comment that out in the final version.

Too expensive for me if there's no other reason to pass named parameters as an anonymous hash:

use Benchmark qw(cmpthese); sub f{} cmpthese ( -1 => { list => sub { f( foo => 1) }, ref => sub { f({foo => 1})}, } ); __END__ Rate ref list ref 232162/s -- -88% list 1989486/s 757% --

Contructing and destructing a full blown hash only to pass named parameters is just a waste. Breaking named parameters into several lines aligning the fat commata vertically helps. I did commit the gaffe of passing an odd number of arguments as named subroutine parameters maybe twice in all my time as a perl programmer, but more often I've been bitten by missing the curlies for apis which required a hash ref for named parameters.

Replies are listed 'Best First'.
Re^3: Preferred technique for named subroutine parameters?
by wfsp (Abbot) on May 23, 2009 at 10:58 UTC
    My benchmark foo is very poor but does this show that if you do something with the arguments the "waste" is not as dramatic as your figures show?
    #!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); sub f{ my %hash = @_; my $value = $hash{foo}; } sub g{ my $hash_ref = shift; my $value = $hash_ref->{foo} } cmpthese ( -1 => { list => sub { f( foo => 1) }, ref => sub { g({foo => 1})}, } ); __END__ Rate ref list ref 693652/s -- -25% list 919951/s 33% --

      Yes, of course. Your benchmark is even more appropriate to show the overhead, which consists in creating, populating and destroying a hash (call with hashref) vs. populating a hash already allocated on the subroutine's pad (call with list).

      Might be small, but might sum up. Call it a micro-de-optimization, but I see no value in making perl slower for a questionable gain and a loss of readability.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found