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:
... for more realistic applications:#!/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__
#!/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!
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
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |