Re: Perl script does not work
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 22:36:28 GMT
Tim Hammerquist <penryu@xxxxxxxxxxxxx> wrote in
news:slrne57okh.2bfl.penryu@xxxxxxxxxxx:
John Bokma <john@xxxxxxxxxxxxxxx> wrote:
Tim Hammerquist <penryu@xxxxxxxxxxxxx> wrote:
John Bokma <john@xxxxxxxxxxxxxxx> wrote:
"A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx> wrote:
* Use the three argument form of open.
unless ( open my $old, '<', $file ) {
push @errors, "Failed to open '$file': $!";
next FILES;
}
Why?
Be cause there's no longer MTOWTDI.
There always is :-)
No. This is Pyth^W the New Perl. :)
(The following is *not* directed at Mr. Bokma.)
....
To bring this slightly back on topic, I can't find any place
where perlfunc says the 1- or 2-arg form of open() is
deprecated. In fact, it mentions several conveniences offered
by the 1-/2-arg form.
I did not go into detail, because I thought it wasn't as important an
issue as attempting to read from and write to possibly unopened
filehandles.
While '>temp.txt' is quite safe, with "<$file", $file is interpolated.
That interpolation can have unintended effects.
In the OP's case, there is no guarantee on what characters may appear in
the filename. In that particular case, perldoc -f open specifically
recommends the 3-argument form:
<blockquote>
Use 3-argument form to open a file with arbitrary weird
characters in it,
open(FOO, '<', $file);
otherwise it's necessary to protect any leading and trailing
whitespace:
$file =~ s#^(\s)#./$1#;
open(FOO, "< $file\0");
(this may not work on some bizarre filesystems). One should
conscientiously choose between the *magic* and 3-arguments form
of open():
</blockquote>
It can also be easier to read because it separates the mode from the
file name.
If the OP had limited the possible filenames to be processed by
filtering them in some way, I would not have made the recommendation.
Therefore, I find it better to stick with the three argument form unless
there is a specific reason to prefer the two argument form.
Sinan
--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
.
- Follow-Ups:
- Re: Perl script does not work
- From: John Bokma
- Re: Perl script does not work
- References:
- Perl script does not work
- From: CSUIDL PROGRAMMEr
- Re: Perl script does not work
- From: A. Sinan Unur
- Re: Perl script does not work
- From: John Bokma
- Re: Perl script does not work
- From: Tim Hammerquist
- Re: Perl script does not work
- From: John Bokma
- Re: Perl script does not work
- From: Tim Hammerquist
- Perl script does not work
- Prev by Date: Re: share a session between two website
- Next by Date: Re: share a session between two website
- Previous by thread: Re: Perl script does not work
- Next by thread: Re: Perl script does not work
- Index(es):
Relevant Pages
|