Hi, 

M. Lewis <[EMAIL PROTECTED]> asked:
> 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": $!);
[...]
> 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.

Passing a filehandle to a subroutine is possible, but if
you use IO::Handle objects like Charles, it's much easier.

> - Charles explicitly said the file was being opened for 
> reading. My understanding is that is the default. I assume 
> that Charles did this for clarity.

Security, too. In Charles' case, all the open ever does is
try to open a file. In your case, it could be used to do
almost anything. That's fine as long as your code runs on
your box only, probably with a fixed "my $file = ..."
right above it - but just imagine your code if part of a
CGI program and $file is a CGI parameter, and somebody
set it to '/bin/rm -rf /|'.

> - Charles opted to use qq() which cause him to have to 
> enclose the $input_file in "".

Dead wrong. He used qq() so that he did not have to quote
the "" inside a string which he wanted to be interpolated.

Delimiting arguments in an error message by quotes is 
a very good idea because it lets you see leading or trailing
whitespace which you'd otherwise miss.

> My question is, which way is better, and why is it better? My 
> assumption is Charles' method is better, but I would 
> appreciate knowing why.

HTH,
Thomas

-- 
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