Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

return $paths only if ...

by cesapun (Acolyte)
on Aug 20, 2012 at 14:32 UTC ( #988457=perlquestion: print w/ replies, xml ) Need Help??
cesapun has asked for the wisdom of the Perl Monks concerning the following question:

Hellooo

I have a problem/question.I have this code(just a part ):

############ SCAN DIRECTORY SUBROUTRINE #################### { my @paths; sub dir_listing { my ($root) = @_; $root .= '/' unless $root =~ /\/$/; for my $f (glob "$root*"){ push @{$paths}, $f if $f =~ m/\.rar$/; dir_listing($f) if -d $f; } return $paths; } }

and the problem is that "return" is defined even if directory does not contain any .rar files.

To check if subroutine returned any paths i use:

if (defined $paths){ ###do something; print "paths defined"; } else{ #do something else; print "paths not defined"; }

When i run the script,all i get is "paths defined" even if my directory does not contain rar files.

Help ~! :(

Comment on return $paths only if ...
Select or Download Code
Re: return $paths only if ...
by toolic (Chancellor) on Aug 20, 2012 at 14:44 UTC
    When I use strict, I get this compile error:
    Global symbol "$paths" requires explicit package name at
    Change:
    my @paths;
    to:
    my $paths;
    That may not solve your problem, but it's a step in the right direction.
      Before if (defined $paths){

      i have

       my $paths = [];

      so that's not the problem. :(

        No, toolic wasn’t referring to the $paths in your

        if (defined $paths){

        test, but rather to the two “paths” variables in scope at sub dir_listing@paths and $paths, which are entirely separate variables (except that they happen to share the same name after the sigil). The first is an array, which is never used; the second is a scalar, which you are treating as an array reference.

        See the code by Anonymous Monk, below, for the correct way of declaring and using these variables.

        Athanasius <°(((><contra mundum

Re: return $paths only if ...
by aitap (Deacon) on Aug 20, 2012 at 15:18 UTC
    Try checking for elements inside the array, not for definedness. For example, if ($#{$paths} > -1) { ... }.
    Sorry if my advice was wrong.
Re: return $paths only if ...
by Anonymous Monk on Aug 20, 2012 at 15:23 UTC
    use strict; use warnings; ############ SCAN DIRECTORY SUBROUTRINE #################### { my $paths = []; sub dir_listing { my ($root) = @_; $root .= '/' unless $root =~ /\/$/; for my $f (glob "$root*") { push @{$paths}, $f if $f =~ m/\.rar$/; dir_listing($f) if -d $f; } return $paths; } } my $rar_files = dir_listing('/a/path'); if (scalar @{$rar_files}) { ###do something; print "paths defined"; } else { #do something else; print "paths not defined"; }
      Many thanks.

      That's what i was looking for.

      Close this.:D

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-11-26 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (162 votes), past polls