#!/usr/bin/perl -w # # Example of opening a filehandle and writing into it. # print "What file should I write into? "; chomp( my $filename = <> ); open MYFILE, ">$filename" or die " ** Error opening '$filename' for writing = '$!' , "; print MYFILE "Here is some stuff to put in the file.\n"; close MYFILE; exit; #---------------------- alternative forms : # Another way to write the open: open(MYFILE, ">$filename") or die(" ... "); # Yet another way. unless ( open MYFILE, ">$filename" ) { die " ... "; } # Yet another way. if (not open MYFILE, ">$filename") { die " ... "; } # Or even (though it emphasises the wrong part, I think) die " ... " unless open MYFILE, ">$filename"; # ---------- # Note the use of the > before the file name to mean "open for writing".