Thanks to Thomas, John, Charles and Peter. Lots of great input concerning the *why* question.

I will certainly adapt my style (or lack thereof) to the better method.

As far as the part about the CGI stuff. While that is not in my realm at this time, who knows when it might be. Best to adopt the proper method now and not have to be concerned about it later.

Thanks again to all for the great, thorough explanations.

Mike



Charles K. Clarkson wrote:
M. Lewis <mailto:[EMAIL PROTECTED]> wrote:

: open my $fh, '<', $input_file or die qq(Cannot open "$input_file": $!);
[snip]
: Charles opted to use qq() which cause him to have to enclose the
: $input_file in "".

    $input_file is in quotes just in case I tried to open a file
that has leading or trailing spaces in the name. I once wrote a
script which opened a number of files and I couldn't figure out
why one file was not opening. (It was early morning, as I recall.)

        After adding the quotes I found stray white space in the name.
(I think I was splitting white space with "split /\s/;" instead of
"split ' ';" and the leading space on the first argument was left
on.)

    Here's a very simple example. In my directory "in.txt" exists,
but not " in.txt". (Note: I used "warn" here to get all the error
messages. Normally we "die" on fatal errors.)

#!/usr/bin/perl

use strict;
use warnings;
#use diagnostics;

my $file = ' in.txt';

open my $fh, '<', $file or warn qq(Cannot open "$file": $!);

close $fh or warn qq(Cannot close "$file": $!);

warn "\n";

open $fh, '<', $file or warn qq(Cannot open $file: $!);

close $fh or warn qq(Cannot close $file: $!);

__END__

Cannot open " in.txt": No such file or directory at bb.pl line 9.
Cannot close " in.txt": Bad file descriptor at bb.pl line 11.

Cannot open  in.txt: No such file or directory at bb.pl line 13.
Cannot close  in.txt: Bad file descriptor at bb.pl line 15.

        At the end of a long programming night, in the early morning
hours, as the text on the screen seems to be moving, the first set
of errors made debugging easier. You may be surprised how many
times I have tested example code for an answer on this list and
found this. :)

 Cannot open "": No such file or directory at . . .


        The quotes around the file name are not needed for
interpolation. (The qq() operator does that.) They just aid me in
picking up extra white space and have saved me some debugging time
in the past. I wrap the hole message in qq() because escaped
quotes '\"' look so ugly (to me).


HTH,


Charles K. Clarkson

--

 The whole is the sum of its parts, plus one or more bugs
  23:15:01 up 1 day,  7:38,  4 users,  load average: 0.04, 0.06, 0.08

 Linux Registered User #241685  http://counter.li.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to