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