Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Help using Find::File::name

by mikelupo (Initiate)
on Nov 27, 2010 at 22:27 UTC ( #874034=perlquestion: print w/replies, xml ) Need Help??

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

I'm a perl noob
I'm trying to inventory a bunch of file with .wgt extension and store the matches (from some seeded input) in a report at the end. I need the full path that Find::File::name provides.
To that end, I'd like to be able to store the output of Find::File::name into a local variable so I can eventually do append the string to a file.
It seems that I should be able to do this:
my $var = $Find::File::name;
but lo, it does not work.
but...I'm confused because I can send it to std out by using print so it seems to reason that it's returning a string.
print $Find::File::name;

Here is my attempted implementation of Wanted.
sub Wanted { return unless (-f $_); # only operate on compiled widgets /\.wgt$/ or return; my $file = $_; chomp ($file); chomp ($line); #print $file ." \n" if $file eq $line; if (lc($line) eq lc($file)){ print $File::Find::name. "\n"; #my $foundFile = $File::Find::name. "\n"; #this does not work +hence it is commented out!! } return; }
Any takers?
Thanks in advance.

Replies are listed 'Best First'.
Re: Help using Find::File::name
by stefbv (Curate) on Nov 28, 2010 at 10:35 UTC

    Your question has been answered, this is my answer to the question you didn't (directly) ask, how can I easily find all (nonempty) files with a '.wgt' extension in a specified path.

    use strict; use warnings; use File::Find::Rule; my $search_path = '.'; my @files = File::Find::Rule ->name('*.wgt') ->file ->nonempty ->in($search_path); foreach my $file (@files) { # do something with '$file' }

    The downside is that File::Find::Rule is not a core module yet, but my opinion is that it should be. Anyway the great power of Perl stands in the huge library of modules on CPAN.

    Regards, Stefan

Re: Help using Find::File::name
by pobocks (Chaplain) on Nov 28, 2010 at 07:39 UTC

    It's almost certainly a scoping problem, as others have said.

    Additionally, I'm not sure what you're trying to do in this Wanted. Even if you had proper scoping, you'd still be re-assigning the variable each time you went over it - in the end, you'd just have whatever the last value that matched the test.

    Here's a try at something more reasonable:

    my @foundfiles; sub Wanted { return unless (-f $_); # only operate on compiled widgets /\.wgt$/ or return; my $file = $_; chomp ($file); chomp ($line); #print $file ." \n" if $file eq $line; if (lc($line) eq lc($file)){ # Adds the files to the array push @foundFiles, $File::Find::name; } return; }

    Alternately, you could keep foundfile as a scalar, and append to it thusly:

    $foundfile .= "$File::Find::name\n";

    Either way, the important thing is that your variable needs to be in a scope where it won't go away before you use it.

    These are pretty common errors, and Perl can catch a lot of them. If you aren't already, start putting this code at the top of every Perl program you write:

    use strict; use warnings;

    This will catch a LOT of problems before they turn into headaches. For even more detail (at some performance cost, you can use diagnostics;

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
Re: Help using Find::File::name
by 7stud (Deacon) on Nov 27, 2010 at 23:29 UTC
    Spelling counts; it's $File::Find::name, and you can certainly assign the string to a variable--afterall $File::Find::name is just the name of a scalar variable itself (see the $ sign?). If you declare a variable in your program, like $full_path, the variable's real name is $main::full_path. So there is nothing special about $File::Find::name--it's just a regular ole variable, albeit it's declared in another file. When you write: use File::Find you are telling perl to look for a directory called File, which contains a file named Find. Inside the file, you will find the declaration of the variable $name.
Re: Help using Find::File::name
by Anonymous Monk on Nov 27, 2010 at 23:32 UTC
    Where do you want to use the variable $foundFile? Outside of the if-block? If so, you have a scoping problem, i.e. $foundFile will be out of scope and gone after the closing '}' of the if-block.

    Maybe you want to store the find results in some global variable (array), or some such...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://874034]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-02-05 17:28 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (32 votes). Check out past polls.