How can I 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.

The three argument form of open() lets you specify the mode separately from the filename. The open() function treats special mode characters and whitespace in the filename as literals

	open FILE, "<", "  file  ";  # filename is "   file   "
	open FILE, ">", ">file";     # filename is ">file"
It may be a lot clearer to use sysopen(), though:

    use Fcntl;
    $badpath = "<<<something really wicked   ";
    sysopen (FH, $badpath, O_WRONLY | O_CREAT | O_TRUNC)
	or die "can't open $badpath: $!";

Back to perlfaq5