Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: RFC: Idiom for named read-write arguments (aliases) instead of using $_[0] etc

by LanX (Canon)
on Sep 02, 2012 at 12:23 UTC ( #991272=note: print w/ replies, xml ) Need Help??


in reply to Re: RFC: Idiom for named read-write arguments (aliases) instead of using $_[0] etc
in thread RFC: Idiom for named read-write arguments (aliases) instead of using $_[0] etc

> Second, as long as we're working so hard to modify the parameter list, let's make the following a fatal error: swap( qw(this that) );

Thanks for pointing out this subtle difference!

After some meditation I actually I prefer this not being a fatal error!

The (lisp like) possibility of having references to literals always confused me to hell.

Back in time when LISP was running on 4 KB RAM it certainly made sense to avoid copies, but nowadays?

this little experiment shows whats happening when dereferencing a ref to a literal

$x=\1; $y=$$x; # equivalent to $y = 1, i.e. a copy $y++; print $y; #> 2

it's a copy and I thinks thats saner.

The other point is what a modifier-routine should return.

> But they all have a return value of the swapped list, so they're useful even when modifying the parameter list isn't the goal, right?

There are many possibilities:

- dual use by returning the swap => fault tolerance

- empty return to avoid double use => explicit use, ignore error

- carping if defined wantarray. => runtime warnings

But actually these design decisions are out of my scope, cause it doesn't inflict the idiom, cause it's the same problem when using $_[0] aliases etc.

for simplicity I'd simply add an empty return() to the example:

sub swap { my ($a, $b) = \(@_); ( $$a, $$b ) = ( $$b, $$a ); return; }

Cheers Rolf


Comment on Re^2: RFC: Idiom for named read-write arguments (aliases) instead of using $_[0] etc
Select or Download Code
Replies are listed 'Best First'.
Re^3: RFC: Idiom for named read-write arguments (aliases) instead of using $_[0] etc
by davido (Archbishop) on Sep 02, 2012 at 17:07 UTC

    When I first /msg'ed you to inquire whether you saw any differences, I at the time didn't either, and it wasn't until I investigated further that I realized there should be a fatal runtime error (and there is) with the non-unpacked version.

    Anyway, you present good arguments, but I am stubborn against considering it a new Perl idiom that should find its way into real code. I do like Moritz's "use perl6" version, because of how explicit it is, but still have concerns. :)

    Anyway, great RFC, as it produced good comments.


    Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (19)
As of 2015-07-29 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (265 votes), past polls