Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to a method to get words from a file into an array by aufrank

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found