I'll also jump in and say that you can go even further than autodie (++talexb) and use a file-handling module for this stuff. It's great that you are learning the basics of opening and closing and printing to files (++), but very quickly that stuff will become boring boilerplate, and the logical computing you do between reading and writing the file will be what you'll want to focus on. Be sure to get well-grounded in the basics, but that includes becoming familiar with the basic toolkit so you can have more fun building things :-)

I recommend using Path::Tiny for file operations; it keeps things simple and easy and intuitive. For what you are doing in your test script:

#!/bin/perl use v5.14; use warnings; use Path::Tiny; my $in = $ARGV[0] or die "Usage: $0 <filename>"; path($in)->copy($in =~ s/(\.\w+)?$/.out/r); __END__
... for more realistic applications:
#!/bin/perl use v5.14; use warnings; use Path::Tiny; my $in = $ARGV[0] or die "Usage: $0 <filename>"; my $out = path($in =~ s/(\.\w+)?$/.out/r); for my $line ( path($in)->lines({ chomp => 1 }) { # do something interesting with the line $out->append("$line\n"); } __END__

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: First attempt at bringing in file for input/output by 1nickt
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.