Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

File::Find Usage with 2 subroutines

by pnaik (Initiate)
on Jun 14, 2013 at 16:22 UTC ( #1038991=perlquestion: print w/replies, xml ) Need Help??
pnaik has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have coded a find call with 2 different subroutines being called to do two different things. However, my observation is that, the first find call with its associated sub works just fine. But when find is called the second time with a different sub, still the first sub is actually being entered. Just wanted to know if there is any limitation on find and number of subs associated ? Else i'll debug my code.

Example representation of what i'm trying to do in my script

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");

Note : (Folder abc/xyz contains a file named file1, and folder pqr contains a file named file2)



In finda, /abc/xyz/file1

In findb, /pqr/file2


In finda, /abc/xyz/file1

In finda, /abc/xyz/file1

Replies are listed 'Best First'.
Re: File::Find Usage with 2 subroutines
by talexb (Canon) on Jun 14, 2013 at 16:38 UTC

    You've provided snippets of code -- do you have a complete script that exhibits this debhaviour, specifically including use strict and use warnings?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: File::Find Usage with 2 subroutines
by Laurent_R (Canon) on Jun 14, 2013 at 22:33 UTC

    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.

      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.
        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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038991]
Approved by talexb
Front-paged by toolic
[Corion]: Yay! As I had to reset my phone again, I now also back up my SMS automatically. Highly conveniently, there is a program to back up my SMS to my IMAP server as mails ;)
[1nickt]: Corion this is an android phone? If so, are you encountering problems with recent upgrades? Lately my phone dies suddenly quite often, seemingly coredumps caused by interaction of two actions, eg phone playing a sound and user swiping simultaneously :-/
[Corion]: 1nickt: Yeah, it's Android, but it's CM 11, so fairly old. I'm looking for a newer (china) phone, but I think I'll wait until Oreo ROMs get out for them
[Corion]: Otherwise, the Vernee Mars looks tempting
[Corion]: ... but alas, no Oreo support in sight yet, and I prefer to flash a stock Android over the custom ROMs, and Oreo promises to make the userland more maintainable.
[LanX]: Corion: shouldn't pmdev have permission to view code of nodelets?
[1nickt]: And of course everything is "unlocked" in EU, right?
[1nickt]: I think there are far more choices than in the US

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2017-11-18 15:07 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.