Thanks Thomas. Appreciated.

Thomas Bätzler wrote:
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 /|'.

I rarely do CGI stuff so this really isn't really in my realm, although I can see how via your example, it would be a good idea to make that a habit.


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


You've lost me here Thomas. Again, his code was:

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

What am I missing here?



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


--

 Those who can't write, write help files.
  01:40:01 up 10:03,  3 users,  load average: 0.12, 0.18, 0.16

 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