in reply to Re: Working with a unkown number of arrays
in thread Working with a unkown number of arrays

Hi, I understand that what you wrote is marked as "pseudocode", but I noticed a couple of strange things:
$files{3} = $input_directory,"3";
Maybe you meant to put a dot "." instead of a comma ","?

Another thing that I don't understand from this fragment is if %files holds filenames or directory names. The use of open() and close() seems to point to "filenames", but in this case the readdir(DATA) should be simply <DATA>.

Regarding the open(), at the price of backward compatibility with 5.6, I use lexical filehandles instead of barewords:

open my $data, ...
Moreover, I usually stick to the three argument version of open(), not because this is requested by this particular application (I actually don't know), but more for sake of a mental habit (yes, I'm mental :) :
open my $data, '<', $files{$_} ...
I find it better not only because it avoids nasty problems when the filename comes from the "untrusted external" (which could lead to embarassing security holes), but also because it visually marks that this open is for read instead of write.

Last, even if the plain die does the work (especially in pseudocode!), it could be helpful to provide a more sensible feedback message, either directly:

open my $data, '<', $files{$_} or die "open('$files{$_}'): $!";
or with
use Fatal qw( open );
in the script's preamble.

Forgive me for jumping on your shoulders this way, but the OP seems to have some bias towards blind cut-and-paste, you know, so it could be useful for her|him to think about these issues ;)

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

Replies are listed 'Best First'.
Re^3: Working with a unkown number of arrays
by ikegami (Patriarch) on Feb 06, 2007 at 05:36 UTC

    Regarding the open(), at the price of backward compatibility with 5.6, I use lexical filehandles instead of barewords:

    Lexical handles (and 3-arg open) work fine in 5.6.

    >c:\progs\perl560\bin\perl script.pl open(my $data, '<', $0) or die; print while <$data>;

    Lexical handles were introduced in 5.6, so you are sacrificing backwards compatibility wih 5.5 (5.005).

    What was introduced in 5.8 is file handles to variables (open(my $fh, '<', \$buf)).

Re^3: Working with a unkown number of arrays
by wojtyk (Friar) on Feb 05, 2007 at 22:35 UTC
    Heh, no worries. I wasn't exactly trying to meet stellar code standards. It was the best I could spit out in about 30-40 seconds ;)