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

Hello,

I am having trouble getting the following to work. Here is what I am doing,
I have a sub that searches for logs files in a directory that always start and end with 08 and 00.

I then open each one of the files and look try to match another string using regex.

Once the second string is matched I save that string to a variable, split it on : and save the 2 part to an array.

Now using the contents in the second array (@spt1) I want to search for those files names in another directory. The problem is that the $spls variable when printed in the sub wanted does not print anything.

So my question is how do I use the contents of foreach $spls in the "sub wanted" subroutine?

Thanks for the help in advance! sample code.
sub search { if ( (-f) && /08\..*00$/ ) open the file while ( $line = <open log file> ) { if ( $line =~ m/regex/ ) { @spt = split(/\:/, $line); push(@spt1,@spt[1]); foreach $spls (@spt1) { find(&wanted, $seek_path, $spls); sub wanted { print $spls; } } }

Replies are listed 'Best First'.
Re: How to pass a variable to a subroutine
by ikegami (Patriarch) on Dec 05, 2008 at 16:37 UTC

    Don't put named subs inside other named subs. Didn't you notice the "won't stay shared" warning? Anonymous subs, on the hand, are fine.

    sub wanted { my ($spls) = @_; ... you can use $spls here ... } sub search { ... find(sub { wanted($spls) }, $seek_path); ... }

    or

    sub search { ... find( sub { ... you can use $spls here ... }, $seek_path ); ... }

    Update: Fixed copy and paste error almust brought up in a reply.

      find( sub { my ($spls) = @_; ... }, $seek_path );

      I think File::Find (which I figure is being used by the OP) doesn't actually pass the "wanted" callback function any arguments, so assigning my ($spls) = @_; wouldn't achieve the desired effect.  It should work just fine without the assigment, however...  A stripped-down runnable example:

      #!/usr/bin/perl use File::Find; my $seek_path = "/usr/bin"; my @spt1 = qw(perl py); foreach my $spls (@spt1) { find( sub { print "^$spls : $_\n" if /^$spls/; }, $seek_path ); } __END__ $ ./728317.pl ^perl : perl ^perl : perlbug ^perl : perlcc ^perl : perldoc ^perl : perlivp ^perl : perl5.8.8 ^py : pydoc ^py : python ^py : python2.4
        Oops, copy and paste error.
Re: How to pass a variable to a subroutine
by mikelieman (Friar) on Dec 05, 2008 at 16:16 UTC
    perlsub
    The Perl model for function call and return values is simple: all functions are passed as parameters one single flat list of scalars, and all functions likewise return to their caller one single flat list of scalars. Any arrays or hashes in these call and return lists will collapse, losing their identities--but you may always use pass-by-reference instead to avoid this
    ( perlreftut )
Re: How to pass a variable to a subroutine
by DStaal (Chaplain) on Dec 05, 2008 at 16:20 UTC
    wanted($spls); ... sub wanted { my $spls = shift @_; print $spls; }

    See perlsub.