Edit on 11/26 - Please note - I am not looking for the "right" way to open/read a file, or the way I should open/read a file every single time.  I was just interested to find out what way most people use, and maybe learn a few new methods at the same time :)
A very common block of code in my perl programs is opening a file and reading o...
            
           
          
            
            I want to generate some lines of Perl code by using file handling in Perl, for example: 
open(FILEHANDLE, ">ex.pl") or die "cannot open file for reading: $!";
print FILEHANDLE "use LWP::UserAgent;"
....
.... some code is here 
....
print FILEHANDLE "my \$ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; r...
            
           
          
            
            open(LOG,"logfile.txt") or die "Unable to open $logfile:$!";
print "\n";
while(<$LOG>){
  print if /\berror\b/i;
}
close(LOG);
...
            
           
          
            
            I have a script in Perl that searches for an error that is in a config file, but it prints out any occurrence of the error.  I need to match what is in the config file and print out only the last time the error occurred.  Any ideas?
...
            
           
          
            
            $ cat flaglist.log
flag1
flag2
flag3
flag4
$
Perl code
my $infile = "flaglist.log";
open my $fpi, '<', $infile or die "$!";
while (<$fpi>) {
    chomp;  
    if ($ENV{$_}) {   # something wrong here
        func($_);
    }       
    else {  
        print "oops\n";
    }       
}
$ perl code.pl
oops
oops
oops
oops
$
All the four f...
            
           
          
            
            I want to redirect the die messages to a separate file so that I can compare that file later to determine what went wrong.
But this code gives me errors:
$ cat test.pl
use strict;
use warnings;
my $log = "msglog.log";
die $log "DEAD$!";
$ perl test.pl
Missing comma after first argument to die function at test.pl line 5, near ""DEAD$!...