Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: A looping question

by Anonymous Monk
on Sep 10, 2010 at 08:49 UTC ( [id://859608]=note: print w/replies, xml ) Need Help??


in reply to A looping question

Code posted above should have looked like this
use warnings; use strict; use Term::Prompt; use File::Find; no warnings 'File::Find'; sub Wanted; my ($dirname,$ans); $basedir="/dmp/backup"; $dirname = prompt('e', 'Enter a dir name', 'e.g. regression-EO +DDAY1', '','[A-Za-z0-9_-]+' ); # Check whether the dir already exists if (find(\&Wanted, $basedir)) { $ans = prompt('y', 'That dir already exists, do you wi +sh to overwrite the existing contents ?','[y/n]','n'); if ($ans == 1) { $dirname = prompt('e', 'Enter a dir name', 'e. +g. regression-EODDAY1', '','[A-Za-z0-9_-]+' ); } if (find(\&Wanted, $basedir)) { $ans = prompt('y', 'That dir already exists, d +o you wish to overwrite the existing contents ?','[y/n]','n'); if ($ans == 1) { $dirname = prompt('e', 'Enter a dir na +me', 'e.g. regression-EODDAY1', '','[A-Za-z0-9_-]+' ); } } } sub Wanted { my $dir = $File::Find::name; print if /$dirname/ ; return; }

Replies are listed 'Best First'.
Re^2: A looping question
by Erez (Priest) on Sep 10, 2010 at 09:11 UTC

    First, remove that "no warning" clause, when you develop, warnings are a guide to things that may be wrong, or are wrong. As posted, the program don't compile for a strict violation, you should declare $basedir (and while at it, there's no need to declare Wanted both on the top and bottom of the program.

    Second, you check twice on the return value of "find" which goes contrary to File::Find's purpose, which is to loop over, recursively, a file tree and perform a certain action on each file/folder. Your logic should go inside the Wanted sub, rather than on an outer loop.

    "Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert..

      Ok , your answer has confused me. I take the point about $basedir which I've now declared and also removed the warning and only declared Wanted once. Is find the wrong thing to use to check for the existence of a file ?

        I see what you mean. File::Find isn't about "finding files", but is a tool for recoursing through a directory tree, and performing an action on some/all of the files/directories.

        To check for the existence of a file/directory you can use the file tests that come with perl itself (http://perldoc.perl.org/functions/-X.html). for example if you have a $path, -e $path will return whether the path exists, -d $path will return true if the path is of a directory, and -f $path will return true if the path is of a file

        "Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert..

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-23 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found