in reply to Optional parameter before reference

sub verify { my $ref= \$_[-1]; pop(@_); my( $string )= @_; # ... }

Lots of variations on that theme are possible. Avoiding subroutine prototypes is usually best with Perl.

- tye        

Replies are listed 'Best First'.
Re^2: Optional parameter before reference (!proto)
by Neutron Jack (Pilgrim) on Jul 23, 2004 at 19:26 UTC
    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

      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