I think you are not well served by page 72 of Learning Perl. It is far more clear, and much easier to debug, if you open the file directly, after checking, than in the background. If you are feeling daring, then go with toolic's
open F, '<', $in_file or die "Can't open $in_file: $!";
followed by
while (<F>) { push @lines, $_ unless /espf\[/ }
although if the goal is to edit $in_file deleting lines, then you probably don't want to push it into a temporary array, but instead open a temp file, write to the temp file, unlink $in_file, and rename the temp file to $in_file. However, since this is probably doing a ton of disk access, I'd probably do a few sanity checks first.
return undef if not defined $in_file; if (-e $in_file) { if (-r $in_file) { open F, "<", $in_file or die "Can't open $in_file: $!" # etc } else { warn "can't read $in_file\n"; return undef; } } else { warn "bad file $in_file submitted, but does not exist\n"; }
Part of the reason I say that reassigning ARGV is not good practice is the principle of least surprise. The "while (<>)" construct is best used in the fashion described in perlopentut:
One of the most common uses for open is one you never even notice. When you process the ARGV filehandle using <ARGV>, Perl actually does an implicit open on each file in @ARGV. Thus a program called like this:
$ myprogram file1 file2 file3
Can have all its files opened and processed one at a time using a construct no more complex than:
while (<>) { # do something with $_ }
If @ARGV is empty when the loop first begins, Perl pretends you've opened up minus, that is, the standard input. In fact, $ARGV, the currently open file during <ARGV> processing, is even set to "-" in these circumstances.
While you *can* use the magic of an implicit open to open files, its better to avoid magic when possible for the next poor sap who has to debug your code.

--woody


In reply to Re^3: @ARGV while (<>) hangs by WoodyWeaver
in thread @ARGV while (<>) hangs by gatorreina

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.