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

Is there a way (in Perl) to pass a list of arguments to an 'anonymous' subroutine? To give you a better idea of what I mean, take a look at the code below:

#!/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?

I stumbled across this problem while thinking of how to replace my `find ... ` commands with the find() subroutine implemented in File::Find module. According to docs, find() may accept a reference to a sub, which really benefited my code in that I didn't have to modify it much. However, I still have to pass 'custom' skip matches (for files/directories) in form of an argument in order to achieve desired results.

Here's an example of the code where find() might be used:
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; }

thanks for your help.

"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
    I stumbled across this problem while thinking of how to replace my `find ... ` commands with the find() subroutine implemented in File::Find module. According to docs, find() may accept a reference to a sub, which really benefited my code in that I didn't have to modify it much. However, I still have to pass 'custom' skip matches (for files/directories) in form of an argument in order to achieve desired results.
    Just pass it as a closure variable. In fact, in your example, just referring to $skip_match will give you the right value!

    -- Randal L. Schwartz, Perl hacker

      Ahh, definitely! ;-). Thanks for the reminder, I should have definitely thought of closures for that matter. cheers

      "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
Re: Arguments to anonymous subs
by dragonchild (Archbishop) on Dec 19, 2001 at 19:41 UTC
    In your example, you have
    sub foo { $_[0]->() } sub bar { # Do stuff here } foo(\&bar(), stuff);
    That won't pass any args to bar(). However, if you write it like this, it will pass all args.
    sub foo { my $func = shift; $func->(@_); }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.