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

I have a function in an external file called with require:
sub dvdr1_readvarsfromfile { my($filename, $fn); &dvdr1_initblankvars; $filename=$numerodaficha; if (length($filename)<6){ if ($filename<10) {$filename="00000".$filename} elsif ($filename<100) {$filename="0000".$filename} elsif ($filename<1000) {$filename="000".$filename} elsif ($filename<10000) {$filename="00".$filename} elsif ($filename<100000) {$filename="0".$filename} } $codnum=$filename; if ($_[0] eq ""){ $filename=glob("$dvdr1orderdb_path\/$codnum.*"); } else{ $filename="$_[0]"; } open (DB, "$filename") || (&erro("$!") && die); $fn=$filename; ...
The problem here is the glob:

when I call:

&dvdr1_readvarsfromfile the first time, the glob always finds the correct file.

When I call it the second time, even though I'm looking for a different file, and the value of $numerodaficha is different, glob always returns blank. Why and how can I solve it?

Thanks!

Edit kudra, 2001-10-02 Moved plaintext out of code tags

Replies are listed 'Best First'.
Re: glob problem...
by jeroenes (Priest) on Oct 01, 2001 at 13:00 UTC
    How odd this may seem, it is a documented function of glob. From the docs:

    A glob evaluates its (embedded) argument only when it is starting a new list. All values must be read before it will start over. In a list context this isn't important, because you automatically get them all anyway. In a scalar context, however, the operator returns the next value each time it is called, or a FALSE value if you've just run out. Again, FALSE is returned only once. So if you're expecting a single value from a glob, it is much better to say ($file) = <blurch*>;

    HTH,
    Jeroen
    "We are not alone"(FZ)

Re: glob problem...
by snowcrash (Friar) on Oct 01, 2001 at 13:09 UTC
    one comment on your code: to zeropad a number better use something like
    my $paddednum = sprintf "%06d", $num;
    than that if/elsif constructs... look into the docs for printf and sprintf in perlfunc.

    cheers snowcrash
Re: glob problem...
by LFonseca (Initiate) on Oct 01, 2001 at 15:10 UTC
    Yes, I am looking for only a file. Your solution worked, thanks. Also, I had already figured out the ifs where not a good solutions :)