On Wed, May 26, 2010 at 1:06 PM, HACKER Nora <nora.hac...@stgkk.at> wrote:
> Hi, > > I am having troubles with a variable that should not be defined but > seems to be somehow. In the following code, the variable $localdir is > declared, then I am doing a find, and in case I get a result the > returned path is assigned as a value to $localdir. But somehow the > latter IF results to TRUE (=$localdir is defined) although the find does > not return a path (which is ok for my current test). Please could tell > me what I do not understand? I thought this was the proper way to check > whether a variable has a value or not (or at least my Perl book says > so)!? > > *** snip *** > > my $localdir; > if (defined $localdir) { print "defined 1: $localdir\n"; } # > undefined = ok for this test > > my $find = File::Finder->type('d')->name("$sdir")->print; > $localdir = find ( $find, "$dbpath/dumps"); > if (defined $localdir) { print "defined 2: $localdir\n"; } # > DEFINED although no directory :-( > > *** snip *** > > Kind regards, > Nora > > Hi Nora, Ok, so what is in $localdir when it is printed? I suspect that the answer is an empty string. If so then Perl is 100% correct as most likely File::Finder will return the result of it's findings in a string format thus if it finds nothing it returns a empty string. Which means $localdir is actually defined. Try adding the following to the if ( defined ... ) statement: if ( defined $localdir && $localdir ne q{} ) { print ... } # q{} is the same as '' or "" but it is more readable in many font types. Regards, Rob