Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: File::Find Usage with 2 subroutines

by Laurent_R (Parson)
on Jun 14, 2013 at 22:33 UTC ( #1039047=note: print w/ replies, xml ) Need Help??


in reply to File::Find Usage with 2 subroutines

I can see a number of problems and possible pmroblems in your program.

Your are not showing the find subroutine, we can't know what it does.

The finda and findb procedures don't use any parametgers passed to them. This lost probably can't be right.

The Find:Fine:name call does not serem to use any parameter from your previous code.

Please show your full program.


Comment on Re: File::Find Usage with 2 subroutines
Re^2: File::Find Usage with 2 subroutines
by pnaik (Initiate) on Jun 15, 2013 at 00:46 UTC

    I'm not using use strict or use warning in my script.

    Well the find sub is not shown as it comes from the package use File::Find.

    My script is quite big, should I paste it fully here or is there a way to attach it ?

    One more observation meanwhile, if I change the second find call from find(\&findb, "/pqr") to find(&findb, "/pqr"), it does enter findb sub now but the file pointed by $File::Find::name is still from the /abc/xyz folder, i.e file1.

    2nd Output: In findb, /abc/xyz/file1

      Hello pnaik!
      It's well worth reading this: How do I post a question effectively? :)
      You should always have "use strict" and "use warning" near the start of your script (except in trivial scripts). They'll save you a lot of time catching bugs at compile time.
        I tried running this:
        use strict; use warnings; use File::Find; sub finda { print "In finda, $File::Find::name\n"; } sub findb { print "In findb, $File::Find::name\n"; } find (\&finda, "/abc/xyz"); find (\&findb, "/pqr");
        and got this result:
        In finda, /abc/xyz # a folder In finda, /abc/xyz/file1 # a file In findb, /pqr # a folder In findb, /pqr/file2 # a file
        This is what you expected (apart from the 2 folders)
        So, as the snippets of code you provided behave as expected, you need to post your code before we can help further.

        Update: I mean: post a minimal script that reproduces your problem.
        (This is all covered in How do I post a question effectively? !)
      One more observation meanwhile, if I change the second find call from find(\&findb, "/pqr") to find(&findb, "/pqr"), it does enter findb sub now but the file pointed by $File::Find::name is still from the /abc/xyz folder, i.e file1.

      That is to be expected. It actually calls findb() before calling File::Find::find() -- that's what the syntax means. It's not a subroutine reference but a call.

      Anyway, there is no problem with your code as you've posted; I'd look into other possible problem areas such as file system permissions and the logic in the rest of your script

        I found the issue, I showed wrong sequence of 'find' calls, actually the find was getting called recursively, and in that it retains the context of parent call probably. I was thinking it to be a re-entrant function, but looks like its not. This is how it looked :

        sub finda { print "In finda, $File:Find:name\n"; find (\&findb, "/pqr"); } sub findb { print "In findb, $File:Find:name\n"; } find (\&finda, "/abc/xyz");

        Anyways, now I have changed the script to have the recursive nature removed, so that the two calls happens sequentially and now its working fine. Sorry for the wrong code and very thankful for your help indeed !

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039047]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-12-25 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (161 votes), past polls