Hey catfish1116,

That error (thrown by strict, which although not explicitly used, is brought in by the use v5.12; statement) is telling you that you forgot to declare the two file handle variables into a scope (you'd use my in this case here because you want the variable to be lexically scoped to the smallest context possible):

if (! open my $in_fh, '<', $in ) { # ^^ <- HERE die "Can't open '$in': $!"; }

Both file opens need to have the my. Nice job on the three-argument open, typically we don't see that with first-timers. The way you write your file open statements is perfectly acceptable (TIMTOWTDI), but in reality, by putting your open in an if() block, you *must* put all of your reads and writes within the same block you've declared the variable. That, or declare the handle variables in a larger (file) scope, with my $in_fh;, then you can proceed to use them throughout the rest of the program without putting the my inside the definition (the if(! open...).

As an alternative, you can open them up a bit wider by re-writing your code to eliminate the if() condition:

open my $in_fh, '<', $in or die "can't open '$in': $!";

When the scope goes out of scope (whether it be a block or entire file), the file handles will automatically close themselves.

Update: Here are some scoping examples and how they work:

if block scope:

if (! open my $fh, '<', $file){ die $!; } # file handle already closed if you try to access here

Dedicated scope:

{ open my $fh, '<', $file or die $!; # can use the fh here } # but not here, fh auto-closed in the block

File scope with if:

my $fh; if (! open $fh, '<', $file){ die $!; } # you can successfully use fh here... # it'll only close if you "close $fh or die $!;", or when the file (pr +ogram) exits # the above example has the same effect of doing the below example, bu +t this is # easier as you don't have a bunch of variables declared at the top of + your file. # the entire idea is to keep variables declared, defined as close to # the place you intend to use them open my $fh, '<', $file or die $!;

There are varying reasons on why and when you'd expand or limit scope for an open file handle. Sometimes, you need only one line from a file read, so you have a small subroutine (scope/block effectively) that does the work and simply returns a bit of data. Other times, like a logging script say, you need the handle open throughout, and just let perl close it as the program finishes. It's all personal preference, but as with any variable (in any language), it's always best to limit exposure to a variable to a context that is as small, concise and tight as possible. This prevents the "broken from far away by accident" situation.


In reply to Re: First attempt at bringing in file for input/output by stevieb
in thread First attempt at bringing in file for input/output by catfish1116

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.