It's generally a good idea to put a leading space before the actual name of the file/command, not before any control characters that determine how the filehandle should be opened. Consider:
# this is pseudo-code $filename = '>test' or '+test' open(HANDLE, ">$filename"); # >>test or >+test (ruh-roh!) # likewise $filename = 'ls|' or '>test; open(HANDLE, "$filename"); # ls| (pipe) or clobbers 'test' # generally safe (in that behavior is not unexpected): open(PIPE, " $command |") open(PIPE, "| $command\0"); # safe version of "|ls" open(FILE, "> $filename\0"); # ">file" open(FILE, " $filename\0"); # "file"
In the last case, the presence of a space doesn't affect what is open, but since the first character is a space, any leading "mode" characters in $filename will be ignored, much like what was happening with your code.

Also see sysopen and perlipc (e.g., using fork/exec), which (in my opinion) are significantly more readable/safer than using weird manglings of open. Note that issues of shell meta-characters and other security considerations are out of the scope of this message. Each of the "PIPE" examples above are still incredibly insecure if untrusted input is allowed to use any shell metacharacters in the string. See perlsec and use Taint-checking.


In reply to Avoiding surprises using 'open' by Fastolfe
in thread VMS: Pipe to process by bbfu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.