in reply to how to make wildcard work in if (-e *..)

print "Found textfile!" if <$dir/*.txt>

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

  • Comment on Re: how to make wildcard work in if (-e *..)

Replies are listed 'Best First'.
•BEWARE broken code: Re: Re: how to make wildcard work in if (-e *..)
by merlyn (Sage) on May 16, 2002 at 20:44 UTC
    Beware that this executes the glob in a scalar context, which will really confuse you when you reexecute the code, since a scalar glob has local magical state. For example, run:
    foreach $n (1..50) { print "$n: "; print "yes" if -e <*>; print "\n"; }
    If you have seven files, every 7th number will be missing, because the glob has run out, and has returned undef, and is resetting.

    This is better, as it uses glob in a list context:

    print "Found textfile!" if () = <$dir/*.txt>;

    -- Randal L. Schwartz, Perl hacker