aufrank has asked for the wisdom of the Perl Monks concerning the following question:
hey--
I am working on my first project using OO perl, and have a quick question. I have several different files (some of which are very large) that are simple word lists-- words separated by a comma and a space. The idea is that I want to load all of the words from a file into an array via a method. The following code seems to do what I want:
sub words_from_file { my $self = shift; my $file_path = shift; my @word_list; open WORD_FILE, "< $file_path" or die "Could not open $file_path: $!\n"; while (<WORD_FILE>) { chomp; push @word_list, split ", "; } \@word_list; }
However, I am not at all sure that this is the best way to implement the functionality I want. In terms of security and speed, do you all have any suggestions? Are there file tests I should be making before the open? Should I do something involving tie to the filehandle to speed things up? (I know nothing about tie so I'm not even sure if that last question made sense :) Is there any benefit to using return \@word_list instead of what I've done in the last line?
just looking for any feedback before I get too much further into this,
--au
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: a method to get words from a file into an array
by sauoq (Abbot) on Aug 08, 2002 at 20:52 UTC | |
Re: a method to get words from a file into an array
by Juerd (Abbot) on Aug 08, 2002 at 21:16 UTC | |
by sauoq (Abbot) on Aug 08, 2002 at 22:56 UTC | |
by Juerd (Abbot) on Aug 09, 2002 at 05:50 UTC | |
Re: a method to get words from a file into an array
by DamnDirtyApe (Curate) on Aug 08, 2002 at 23:14 UTC |