M. Lewis wrote: > > Charles K. Clarkson recently replied to Sreedhar Reddy in which one of > the corrections he made was: > > open my $fh, '<', $input_file or die qq(Cannot open "$input_file": $!); > > It seems that Charles' habits are to prevent excess typing and to 'be > lean on variables' as he phrased it I believe. > > When I have opened a file, I have always done: > > open (FILE, $file) || die "Cannot open $file :$!"; > > A couple of things different. > - I use a FILEHANDLE, where Charles used a variable.
Charles used a *lexical* variable so that the scope of the variable is limited and the filehandle is automatically closed when the variable goes out of scope while FILEHANDLE is a package variable that is more "globally" visible. > - Charles explicitly said the file was being opened for reading. My > understanding is that is the default. Yes, you could even write it simply as: $FILE = 'somefile.txt'; open( FILE ) || die "Cannot open $FILE :$!"; But that won't work if $FILE is a lexical variable. > I assume that Charles did this for clarity. And for safety. perldoc -q "How can I open a file" > - Charles opted to use qq() which cause him to have to enclose the > $input_file in "". Perl has several forms of quoting but he didn't "have to enclose" $input_file in "". The following are equivalent: die qq(Cannot open "$input_file": $!); die "Cannot open \"$input_file\": $!"; die "Cannot open \042$input_file\042: $!"; die 'Cannot open "', $input_file, '": ', $!; die 'Cannot open "' . $input_file . '": ' . $!; Or he could enclose $input_file in single quotes: die "Cannot open '$input_file': $!"; > My question is, which way is better, and why is it better? The safest way is usually better. :-) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>