[Israel.pm] Another newbie question - Hashes

Gaal Yahas gaal at forum2.org
Tue Feb 3 08:24:54 PST 2004


On Tue, Feb 03, 2004 at 05:40:18PM +0200, Yuval Yaari wrote:
> I tried making my example as basic as possible (a simple question 
> deserves a simple answer).
> Anyway, here's what I do in real life (as always TMTOWTDI):
> 
> ### BEGIN ###
> open(FILE, "<file");
> my $file = do{ local $/; <FILE> };
> close(FILE);
> my %hash = map { split(/\s+/, $_, 2) } split (/\n/, $file);
> ### END ###

Well, if you're already willing to read the whole file to memory why
don't you just use <> in list context:

    open my $fh, "filename" or die "$0:open:$!";
    my %hash = map { split(/\s+/, $_, 2) } <$fh>;
    close $fh or die "$0:close:$!";      # or simply let $fh go out of scope

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list