in reply to Is it possible to write a sub that understand default variables ?
If you're using 5.10, you can use the new _ (underscore) prototype to create subroutines that magically use either a passed parameter or $_:
sub display_thing (_) { print $_[0] }; $_='Hello 5.10'; display_thing;
Another way would be to just wrap display_thing with your own wrapper that handles the decision between $_ and @_:
my $old_display_thing = \&display_thing; *display_thing = sub { if (@_) { &$old_display_thing(@_) } else { &$old_display_thing($_) }; };
But if you're rewriting or wrapping display_thing (and I see no way around rewriting or wrapping it), why not rewrite or wrap it to accept multiple arguments straight away?
sub display_thing { for (@_) { print "Displaying thing >$_<\n"; }; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is it possible to write a sub that understand default variables ?
by dcd (Scribe) on Dec 24, 2007 at 15:13 UTC | |
|
Re^2: Is it possible to write a sub that understand default variables ?
by bcarnazzi (Novice) on Dec 24, 2007 at 13:13 UTC |