statebelt has asked for the wisdom of the Perl Monks concerning the following question:

Can I tell a sub to take $_ instead of @_, as some builtins do when the parameter list is not specified? I want to say:

foo foreach @list; # or maybe &foo to satisfy use strict;

rather than:

foo($_) foreach @list;

If the answer is NO, that's fine. ;-) Thanks! Thomas

  • Comment on Can I tell a sub to take $_ instead of @_ (like some builtins do)

Replies are listed 'Best First'.
Re: Can I tell a sub to take $_ instead of @_ (like some builtins do)
by broquaint (Abbot) on Aug 22, 2002 at 18:50 UTC
    sub foo { # changing $_ is not a good idea local $_ = $_; print "\$_ is currently: $_" } foo foreach qw(one two three four); __output__ $_ is currently: one $_ is currently: two $_ is currently: three $_ is currently: four
    Because $_ is global you should be able to access it from anywhere (but be careful not to mess with it!).
    HTH

    _________
    broquaint

•Re: Can I tell a sub to take $_ instead of @_ (like some builtins do)
by merlyn (Sage) on Aug 22, 2002 at 18:43 UTC
Re: Can I tell a sub to take $_ instead of @_ (like some builtins do)
by no_slogan (Deacon) on Aug 22, 2002 at 18:48 UTC
    You can easily write a sub so that it uses $_ if it gets no other arguments.
    sub foo { my $arg = @_ ? shift : $_; ... }
    Or some such like that...
    or maybe &foo to satisfy use strict;
    Be very careful about using &foo. That lets foo have access to @_, which might get you in trouble. It's generally better to say foo() or maybe &foo().

    Update: The other people who responded have a good point about localizing $_ if you're going to be changing it. But I don't think that's what you were asking for.

Re: Can I tell a sub to take $_ instead of @_ (like some builtins do)
by BrowserUk (Patriarch) on Aug 22, 2002 at 19:27 UTC

    A slight variation

    #! perl -w sub test { my $parm = $_[0] || $_; print $parm, $/; } test( "fred"); test for qw(the quick brown fox); __END__ # Output C:\test>192129 fred the quick brown fox C:\test>

    What's this about a "crooked mitre"? I'm good at woodwork!
      About your line:
          my $parm = $_[0] || $_;
      
      What is you call this?
      test(0);
      test("");
      
      You should wait for Perl 6 and use the new operator //.

        <tongue-in-cheek>

        Printing a null byte to the screen isn't very useful anyway!

        It's a feature:)

        </tongue-in-cheek>

        my $parm = defined $_[0] ? $_[0] : $_;?


        What's this about a "crooked mitre"? I'm good at woodwork!
Re: Can I tell a sub to take $_ instead of @_ (like some builtins do)
by Aristotle (Chancellor) on Aug 22, 2002 at 23:11 UTC
    You could write that as
    sub foo { my $var = @_ ? \$_[0] : \$_; modify($$var); } foo for @list;
    Beware of false laziness however. You should not use this kind of interface lightheartedly. Are you sure it is a better idea than just wrapping the inside of &foo with a for loop?
    sub foo { for(@_) { modify($_); } } foo @list;

    Makeshifts last the longest.