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

Discussion in 'Perl Misc' started by PerlFAQ Server, Feb 2, 2011.

  1. 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.19: 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
    characters in the filename as special.

    open my $fh, "<", " file "; # filename is " file "
    open my $fh, ">", ">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.
     
    PerlFAQ Server, Feb 2, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Les Caudle
    Replies:
    0
    Views:
    672
    Les Caudle
    Mar 7, 2004
  2. tfs
    Replies:
    1
    Views:
    409
    John Saunders
    Jun 28, 2004
  3. Leo
    Replies:
    0
    Views:
    475
  4. jalkadir
    Replies:
    6
    Views:
    446
    jalkadir
    Oct 15, 2005
  5. PerlFAQ Server
    Replies:
    0
    Views:
    109
    PerlFAQ Server
    Mar 27, 2011
Loading...

Share This Page