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

In reply to Arguments to anonymous subs by vladb

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.