I have the following hypothetical class which uses blessed anonymous subs as objects. For now I realize that it does nothing I couldn't do with grep, but please bear with me as this is only a slimmed-down example for the sake of this node.
package BlessedSub; use base 'Exporter'; @EXPORT = qw/blessed_sub/; ## prototyped for convenience sub blessed_sub(&) { my $anonymous_sub = shift; bless $anonymous_sub, __PACKAGE__; } sub filter { my ($self, @items) = @_; ## check if all the items return succesfully for (@items) { return 0 unless $self->($_); } return 1; }
The blessed_sub sub is prototyped so I can remove the sub keyword on the arguments, like this:
my $obj = blessed_sub { $_[0] =~ /foo/ }; die unless $obj->filter(qw/foobar foobaz bazfoo barbaz barfoo/);
But I soon after writing many lines like these, I decided that if I were already using prototypes to make blessed_sub behave more like a builtin, why not make it even more like a builtin? Our neighborhood friendly builtins like map and grep use the $_ variable to grab their argument, not @_ and $_[0]. I noticed that my anonymous subs were being called from within a for loop, so I changed the calling code to this:
my $obj = blessed_sub { /foo/ }; ## now uses $_, not @_ die unless $obj->filter(qw/foobar foobaz bazfoo barbaz barfoo/);
..and it worked in exactly the same way. Now blessed_sub really looks and feels just like a builtin. I enjoy being able to shorten builtins that default to $_ when possible, and I think the readability increases anyway from replacing $_[0] with $_. But am I missing some dangerous consequences of using $_ here? I think it's pretty safe from within a for loop, but I want to make sure no bad things can happen.

Thanks for your comments,

blokhead


In reply to Passing with $_ instead of @_ in anonymous subs by blokhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.