Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Optional parameter before reference (!proto)

by Neutron Jack (Pilgrim)
on Jul 23, 2004 at 19:26 UTC ( [id://376965]=note: print w/replies, xml ) Need Help??


in reply to Re: Optional parameter before reference (!proto)
in thread Optional parameter before reference

Oh, that's excellent! I'd forgotten that @_'s elements are aliases for the actual scalar parameters. We get the referencing for free, without the caller having to use a backslash.

This does what I need:

sub verify { my $ref = \pop; my $string = @_ ? shift : "<default>"; $$ref = $string; # Dummy action for testing } verify $a; verify "hello", $b; print "a=$a b=$b\n"; # Expect a=<default> b=hello

Replies are listed 'Best First'.
Re^3: Optional parameter before reference (ylsned)
by tye (Sage) on Jul 23, 2004 at 22:21 UTC

    Interesting. I specifically didn't use \pop because I figured that you'd end up with a reference to a copy. But it makes sense that it is the assignment in "my $x = pop" that does the copying and not the pop. Thanks.

    - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found