Re: Perl script does not work



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

.



Relevant Pages

  • Re: renaming files
    ... Are you aware that you may use nearly any character in a UNIX filename? ... A filename may also contain whitespaces or lots of oddball characters ... Perl modules) then the oddly named files will be safely renamed just as ...
    (perl.beginners)
  • Re: Recursively change filenames with characters ? [ ] / = + < > ; : " , - *
    ... allow trailing blanks or dots in a filename. ... Since you might need to process non-printable characters, ... would use Perl. ...
    (comp.unix.shell)
  • Re: determine default filename encoding
    ... > Suse tech support for the way filename characters are represented, ... Characters are characters - just bytes. ... Then if that coding differed from the LANG coding ... filenames that were written in encoding X as they were written, ...
    (comp.os.linux.setup)
  • Re: unix filename restriction
    ... underscore _ and the dot. ... You could use other characters, ... never met a *human* defined path of more than 80 chars (most of the ... If you want to be very sure, limit your filename to the DOS limit of 8 ...
    (comp.os.linux.development.apps)
  • Re: Error Message: "The File Name is Not Valid or Too Long...."
    ... I, then, proceed to name that file using only valid characters. ... the destination folder. ... That is not counting the drive letter and slashes or the filename itself. ... (Again - not counting the extension/dot.) ...
    (microsoft.public.windowsxp.perform_maintain)