in reply to Re: How to pass a variable to a subroutine
in thread How to pass a variable to a subroutine

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

Replies are listed 'Best First'.
Re^3: How to pass a variable to a subroutine
by ikegami (Patriarch) on Dec 06, 2008 at 05:56 UTC
    Oops, copy and paste error.
      Thanks to everyone for the help and replies.

      However I am not sure what the problem is but even though $FN_SPLIT and $FOUND_FILE match they do not produce the correct results. Here is the code that I am using along with the results of running it. I am not sure why even though both variables match($FN_SPLIT and $FOUDN_FILE) they do not produce a print of MATCH MATCH as seen the the third entry under results.

      If I manually replace $FN_SPLIT in this line ( $FOUND_FILE =~ /($FN_SPLIT)/ ) with a file name, lets say the same one shown below then I get MATCH MATCH MATCH line to print, but again not sure why when the variable is passed it does not produce a match.

      Thanks for the help in advance once again.
      @FN = split(/\=/, $LINE); push(@FILE_NAME, @FN[1]); foreach my $FN_SPLIT (@FILE_NAME) { chomp $FN_SPLIT; print "This is the fn_split variabl +e: $FN_SPLIT\n\n"; find ( sub { my $FOUND_FILE = $_; chomp $FOUND_FILE; print "This is the FOUND_ +FILE variable: $FOUND_FILE\n\n"; if ( $FOUND_FILE =~ /($FN +_SPLIT)/ ) { print "============== +==================================================\n"; print "MATCH MATCH MA +TCH $FN_SPLIT : $FOUND_FILE MATCH MATCH MATCH\n"; print "============== +==================================================\n"; } else { print "$FOUND_FILE do +es not equal: $FN_SPLIT\n\n"; } }, $SEARCH_DIR ); }
      Results
      This is the fn_split variable: 08ptr00 This is the FOUND_FILE varible: . . does not equal: 08ptr00 This is the fn_split variable: 08r2t00 This is the FOUND_FILE varible: 08r3t00 08r3t00 does not equal: 08ptr00 This is the fn_split variable: 08ptr00 This is the FOUND_FILE varible: 08ptr00 08ptr00 does not equal: 08ptr00

        Five problems unrelated to your question:

        • Don't you have warnings turned on!? @FN[1] issues "Scalar value @FN[1] better written as $FN[1]".
        • Get rid of chomp $FOUND_FILE;. $FOUND_FILE contains a file name, not a filename followed by a newline.
        • $FN_SPLIT doesn't contain a regexp pattern, so /($FN_SPLIT)/ should be /(\Q$FN_SPLIT\E)/.
        • The captures in the regexp pattern are a waste of resource. /(\Q$FN_SPLIT\E)/ should be /\Q$FN_SPLIT\E/.
        • Finally, the regexp pattern doesn't check for equality. For that, you'd want $FOUND_FILE =~ /^\Q$FN_SPLIT\E\z/ or better yet $FOUND_FILE eq $FN_SPLIT.

        Try using

        use Data::Dumper; local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; print("FOUND_FILE: ", Dumper($FOUND_FILE), "\n"); print("FN_SPLIT: ", Dumper($FN_SPLIT ), "\n");

        There's probably leading or trailing spaces.