Passing paramaters to the File::Find find() function

Discussion in 'Perl Misc' started by niall.macpherson@ntlworld.com, Feb 15, 2006.

  1. Guest

    I've just used the File::Find module for the first time.

    I can't figure out how to pass variables into the find() function.

    Here is a trivial example (I know that what this does can be done many
    other better ways, but it was about the smallest bit of code that I
    could come up with which illustrates my problem)

    The point is that $mytext and @myfiles both have to be global variables
    since there doesn't seem to be a way of passing them from the
    testclpm7() sub to the pushfiles() sub via the find() call.

    Is it possible to pass values through so I can avoid having to use
    global variables ?

    Thanks

    use strict;
    use warnings;
    use File::Find;
    use Data::Dumper;
    use vars qw (@myfiles);
    use vars qw ($mytext);

    #-----------------------------------------------------------
    sub pushfiles
    {
    if(!/$mytext/)
    {
    push @myfiles, $File::Find::name;
    }
    return;
    }
    #-----------------------------------------------------------
    sub testclpm7
    {
    ($mytext) = @_;
    ## Works but I would like to pass both $mytext and @myfiles
    ## here so that they don't have to be global
    find (\&pushfiles , "C:/develop/NiallPerlScripts");
    print Dumper \@myfiles;
    }
    #-----------------------------------------------------------
    if($#ARGV != 0)
    {
    die "Usage $0 <text>";
    }
    testclpm7($ARGV[0]);
     
    , Feb 15, 2006
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I've just used the File::Find module for the first time.
    >
    > I can't figure out how to pass variables into the find() function.
    >
    > Here is a trivial example (I know that what this does can be done many
    > other better ways, but it was about the smallest bit of code that I
    > could come up with which illustrates my problem)
    >
    > The point is that $mytext and @myfiles both have to be global variables
    > since there doesn't seem to be a way of passing them from the
    > testclpm7() sub to the pushfiles() sub via the find() call.
    >
    > Is it possible to pass values through so I can avoid having to use
    > global variables ?


    [snip]

    > sub testclpm7
    > {
    > ($mytext) = @_;
    > ## Works but I would like to pass both $mytext and @myfiles
    > ## here so that they don't have to be global
    > find (\&pushfiles , "C:/develop/NiallPerlScripts");


    Since File::Find::find doesn't take these parameters you won't have
    much joy giving them to it. You need to write your own variant.

    > print Dumper \@myfiles;
    > }



    Untested:

    sub my_find {
    my ( $myfiles, $mytext, $dir) = @_;
    File::Find::find(
    sub { push @$myfiles, $File::Find::name if /$mytext/ },
    $dir,
    );
    }

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Feb 15, 2006
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I've just used the File::Find module for the first time.
    >
    > I can't figure out how to pass variables into the find() function.
    >
    > Here is a trivial example (I know that what this does can be done many
    > other better ways, but it was about the smallest bit of code that I
    > could come up with which illustrates my problem)
    >
    > The point is that $mytext and @myfiles both have to be global variables
    > since there doesn't seem to be a way of passing them from the
    > testclpm7() sub to the pushfiles() sub via the find() call.
    >
    > Is it possible to pass values through so I can avoid having to use
    > global variables ?


    [snip]

    > sub testclpm7
    > {
    > ($mytext) = @_;
    > ## Works but I would like to pass both $mytext and @myfiles
    > ## here so that they don't have to be global
    > find (\&pushfiles , "C:/develop/NiallPerlScripts");


    Since File::Find::find doesn't take these parameters you won't have
    much joy giving them to it. You need to write your own variant.

    > print Dumper \@myfiles;
    > }



    Untested:

    sub my_find {
    my ( $myfiles, $mytext, $dir) = @_;
    File::Find::find(
    sub { push @$myfiles, $File::Find::name if /$mytext/ },
    $dir,
    );
    }

    Call as

    my @myfiles;
    my_find( \ @myfiles, '\.jpg$', 'C:/develop/NiallPerlScripts');


    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Feb 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M?rcia Elias
    Replies:
    4
    Views:
    421
    M?rcia Elias
    Jan 27, 2004
  2. Vic
    Replies:
    5
    Views:
    6,644
    Luke Tulkas
    Jun 28, 2004
  3. Robert Mark Bram

    Paramaters in EL?

    Robert Mark Bram, Mar 20, 2005, in forum: Java
    Replies:
    2
    Views:
    433
    Chris Riesbeck
    Mar 21, 2005
  4. Praveen Kumar Madis

    Memory allocation for function paramaters

    Praveen Kumar Madis, May 27, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,362
    Dan Pop
    Jun 1, 2004
  5. Richard Hengeveld

    Trying to understand pointers for function paramaters

    Richard Hengeveld, Sep 29, 2004, in forum: C Programming
    Replies:
    51
    Views:
    916
    Mark McIntyre
    Oct 6, 2004
Loading...

Share This Page