Perl FAQ 5.26: How can open a file with a leading ">" or trailing blanks?

Perl FAQ 5.26

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

Normally perl ignores trailing blanks in filenames, and interprets certain leading characters (or a trailing "|") to mean something special. To avoid this, you might want to use a routine like this. It makes non-fullpathnames into explicit relative ones, and tacks a trailing null byte on the name to make perl leave it alone:

    sub safe_filename {
      local($_) = shift;
      m#^/# ? "$_\0" : "./$_\0";
    }
    $fn = &safe_filename("<<<something really wicked   ");
    open(FH, "> $fn") || "couldn't open $fn: $!";


Other resources at this site: