> got stuck on thinking how to open summary.txt file to read and open inout.txt to write at the same time.
This is very simple: you open a filehandle in read mode and another in write mode. You use the diamond operator <> too much: it is handy but also magic, infact instead of print "Enter the name of the file: "; my $base_dir = <>; you better my $base_dir = <STDIN>; ( $userinput is a better name than $base_dir anyway..) so start out with plain things:
# never forget the following!!! use strict; use warnings; # use diagnostics; # this can be skipped but is useful when starting # grab user input.. # choose a file name for the out file.. ... open my $readhandle, '<', $filetoread or die "Unable to read [$filetor +ead]!"; open my $writehandle '>', $filetowrite or die ""Unable to write [$file +toread]!"; # not <> but a named lexical filehandle while (<$readhandle>){ if ($_ =~ /somethingtosearch/){ # print to the write file print $writehandle $_; } } # always explicitally close filehandle: Perl will normally does the ri +ght thing but it is safer to close them anyway (good habit) close $readhandle; close $writehandle; # take the file and attach to an email ...
This is the basic. After you have a working skeleton code you can add some more feature as more error control or a more elegant way to parse command line arguments aka Getopt::Simple or Getopt::Long
L*
In reply to Re: How to ask user for file name and save the output to a new text file
by Discipulus
in thread How to ask user for file name and save the output to a new text file
by mmazlan67
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |