FAQ 5.17 How can I open a file with a leading ">" or trailing blanks?

FAQ 5.17 How can I open a file with a leading ">" or trailing blanks?

am 08.10.2007 15:03:03 von PerlFAQ Server

This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

------------------------------------------------------------ --------

5.17: How can I open a file with a leading ">" or trailing blanks?


(contributed by Brian McCauley)

The special two argument form of Perl's open() function ignores trailing
blanks in filenames and infers the mode from certain leading characters
(or a trailing "|"). In older versions of Perl this was the only version
of open() and so it is prevalent in old code and books.

Unless you have a particular reason to use the two argument form you
should use the three argument form of open() which does not treat any
charcters in the filename as special.

open FILE, "<", " file "; # filename is " file "
open FILE, ">", ">file"; # filename is ">file"



------------------------------------------------------------ --------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Re: FAQ 5.17 How can I open a file with a leading ">" or trailingblanks?

am 09.10.2007 09:50:06 von Joe Smith

PerlFAQ Server wrote:

> 5.17: How can I open a file with a leading ">" or trailing blanks?
>
> Unless you have a particular reason to use the two argument form ...

It's probably worth mentioning one good reason for using the two
argument form, and that is if your program is documented to accept
a filename of "-" to read from STDIN.
-Joe

Re: FAQ 5.17 How can I open a file with a leading ">" or trailing blanks?

am 09.10.2007 13:53:24 von Michele Dondi

On Tue, 09 Oct 2007 00:50:06 -0700, Joe Smith wrote:

>> 5.17: How can I open a file with a leading ">" or trailing blanks?
>>
>> Unless you have a particular reason to use the two argument form ...
>
>It's probably worth mentioning one good reason for using the two
>argument form, and that is if your program is documented to accept
>a filename of "-" to read from STDIN.

IMHO that's OT wrt this faq entry and better apt for another one, i.e.
"Why should I use the three args form of open()?" (I don't know if it
actually exists.)

As far as your comment is concerned, two things can be said:

(i) one can have his program documented to accept a filename of '-' to
read from STDIN and reimplement some of the features of two args
open() manually. I say some because the latter does more than what you
say. For example you can give a program on the command line, with a
pipe, and one may want the '-' feature but not this other one.

(ii) if one wants that he may use the ARGV magic filehandle which uses
the two args form behind the curtains: when I find that behaviour
appropriate, I do @ARGV hacks all the time. But I seldom feel the need
for explicit two args open()s.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: FAQ 5.17 How can I open a file with a leading ">" or trailing blanks?

am 10.10.2007 16:54:31 von brian d foy

In article , Joe Smith
wrote:

> PerlFAQ Server wrote:
>
> > 5.17: How can I open a file with a leading ">" or trailing blanks?
> >
> > Unless you have a particular reason to use the two argument form ...
>
> It's probably worth mentioning one good reason for using the two
> argument form, and that is if your program is documented to accept
> a filename of "-" to read from STDIN.

That getsw away from the topic of the question, so it's a bit
distracting. The weasel words "Unless you have a particular reason"
really says "you have to find out on your own because we're not going
to encourage it". :)