Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Foncé asked specifically for an explaination so I will do a little expaining of DamnDirtyApe's code. I'm sure any mistakes I make will be corrected Immediately.
open( FH, 'addys.txt' ) or die "Couldn't open file: $!" ;
This line opens the file "addys.txt" to filehandle FH. The function open will return a true value if the file open's sucessfully and a false value if it fails. If the file opens sucessfully the or will "short circuit" meaning since it already has a true value there is no need to evaluate the rest of the statement. If the open fails the die will be executed and the program will quit giving an error message. This is a pretty common way of testing that an open succeeded. You should always check the return value of an open it will usually save you a lot of debug time.
my @email_addys = <FH> ;
This line does the bulk of what you ask for. The my @email_addys creates a lexical array variable to hold the email addresses. The diamond operator ( <> ) on a filehandle reads the next line from the file. If it is being used in what is called a list context it will slurp up the whole file and return a list where each line of the file will be one of the elements of the list.
List context means that whatever it is feeding is expecting a list of items. Since we are assigning to an array variable this is a list context. If we were asigning to a scalar variable the diamond operator would only return a scalar containing the next line of the file (or undef if we were past the end of the file). Understanding context is one of the most important parts of understanding perl.
close( FH ) ;
Close the filehandle.

Notes: Each element of the @email_addys array will have a trailing newline "\n" (except maybe the last one depending on whether the last line of the file had a newline at the end.)

--

flounder


In reply to Re: Re: Simple file to array by flounder99
in thread Simple file to array by Foncé

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 sharing their wisdom with the Monastery: (4)
    As of 2024-10-08 19:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The PerlMonks site front end has:





      Results (44 votes). Check out past polls.

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.