chomp (my @lines = <DATA>);
Who knows how that works, but it does. It's a perl idiom you need to know.
Yes, it’s a useful idiom — but surely not so mysterious?
-
my @lines =
Declares a lexical array variable and initialises it (with a list of array elements).
-
@lines = <DATA>
The LHS (left-hand side) of the assignment is an array, which gives list context to the expression on the RHS. When the readline or “diamond” operator <...> is placed in list context, it “reads until end-of-file is reached and returns a list of lines.” Lines are defined as the text between successive input record separators. The input record separator, stored in $/, “is newline by default.” See readline and General Variables in perlvar.
-
chomp(...);
When chomp is given a list, it chomps (removes the input record separator from) each element in the list.
That wasn’t really so hard, was it? ;-)
Avoid writing $_ in your code.
Yes, avoid writing $_ explicitly where possible. But be aware that an explicit $_ is often needed; for example:
Hope that helps,
-
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.
|