Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:06:14+00:00 2026-06-06T19:06:14+00:00

When I call perl’s two-arg open() with a filename containing a trailing newline, the

  • 0

When I call perl’s two-arg open() with a filename containing a trailing newline, the newline is ignored. The three-arg version preserves the newline, however.

Why does the behavior differ? Why is the newline ostensibly stripped out?

— The file “nl” does not exist. Make it

$ ls -1 nl; touch nl; ls -1 nl
ls: nl: No such file or directory
nl

— Try to three-arg open “nl\n” → ENOENT

strace shows the behavior I expect, FWIW.

$ perl -E 'open(F, "<", "nl\n") or die $!'
No such file or directory at -e line 1.

$ strace -e trace=open perl -E 'open(F, "<", "nl\n") or die $!' 2>&1 | grep nl
open("nl\n", O_RDONLY|O_LARGEFILE)      = -1 ENOENT (No such file or directory)

— Now, try the two-arg open “nl\n” → success ?

$ perl -E 'open(F, "nl\n") or die $!'

— What? Why did that work? Let’s look at strace.

Oh, it is ignoring the newline:

$ strace -e trace=open perl -E 'open(F, "nl\n") or die $!' 2>&1 | grep nl
open("nl", O_RDONLY|O_LARGEFILE)        = 3

— “nl” is still the only file there

$ ls 
nl

Background:

$ perl -v

This is perl 5, version 16, subversion 0 (v5.16.0) built for i686-linux-thread-multi.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:06:15+00:00Added an answer on June 6, 2026 at 7:06 pm

    perldoc perlopentut:

    The other important thing to notice is that, just as in the shell, any whitespace before or after the filename is ignored. This is good, because you wouldn’t want these to do different things:

    open INFO, "<datafile"
    open INFO, "< datafile"
    open INFO, "< datafile"
    

    This is not a bug, but a feature. Because open mimics the shell in its style of using redirection arrows to specify how to open the file, it also does so with respect to extra whitespace around the filename itself as well. For accessing files with naughty names, see Dispelling the Dweomer.

    There is also a 3-argument version of open, which lets you put the special redirection characters into their own argument:

    …

    In this case, the filename to open is the actual string in $datafile, so you don’t have to worry about $datafile containing characters that might influence the open mode, or whitespace at the beginning of the filename that would be absorbed in the 2-argument version. Also, any reduction of unnecessary string interpolation is a good thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there is a way to call Perl routines from C. As shown
Can you intercept a method call in Perl, do something with the arguments, and
Is it possible to call winrar through perl on a windows system, such as
I have a perl script (call it worker) installed on each node/machine (4 total)
I want to call subroutines in Perl like: sub temp { ---- some code
In Perl, you are able to call a function by reference (or name) like
I have Perl code that uses the system() function to call robocopy.exe, but it
I'm working on a Perl module and whenever I call the skip() method I
I have to call an existing AMF Method in a PHP (or Perl) Script
I have php scripts that call perl scripts to do various things and sometimes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.