blokhead has asked for the wisdom of the Perl Monks concerning the following question:
The blessed_sub sub is prototyped so I can remove the sub keyword on the arguments, like this: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; }
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 { $_[0] =~ /foo/ }; 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.my $obj = blessed_sub { /foo/ }; ## now uses $_, not @_ die unless $obj->filter(qw/foobar foobaz bazfoo barbaz barfoo/);
Thanks for your comments,
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing with $_ instead of @_ in anonymous subs
by stefp (Vicar) on Oct 28, 2002 at 09:05 UTC | |
|
Re: Passing with $_ instead of @_ in anonymous subs
by fruiture (Curate) on Oct 28, 2002 at 11:05 UTC |