vladb has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/local/bin/perl -w use strict; sub foo { $_[0]->(); } sub bar { print "here: $_[0];\n"; } # invoke directly &bar("foo"); # invoke 'indirectly' # however, there's no way to pass in arguments to an # anonymouse subroutine? # (note: will die with use strict since @_ would # not have been initialized) # foo (sub { print "here: $_[0];\n"; }); # say, like this... foo (sub ("foo") { print "here: $_[0];\n"; }); # or, even (rather wicked)... # foo (sub { print "here: $_[0];\n"; }("foo")); # i know it looks absurd; however, shouldn't there # be a quicker way to make a do with anonymous subs # rather than using explicit sub declarations # when there's a need to also pass certain arguments # to the anonymous sub?
use File::Find; sub get_subdirs { my ($dir,$skip_match) = @_; # i can't figure how to pass $skip_match?? so that it could be use +d # inside the anonymous sub invoked by find(). my @dirs = find(sub { (my ($dev,$ino,$mode,$nlink,$uid,$gid) = lst +at($_)) && -d _; }, $dir); \@dirs; }
| "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Arguments to anonymous subs
by merlyn (Sage) on Dec 19, 2001 at 04:07 UTC | |
by vladb (Vicar) on Dec 19, 2001 at 04:15 UTC | |
|
Re: Arguments to anonymous subs
by dragonchild (Archbishop) on Dec 19, 2001 at 19:41 UTC |