in reply to "Pro Perl's" Redirecting Filehandles - Unexpected Result

Remove the space from "> &". ">&" is an "open mode" like ">>" and "+<" and even ">&=". In modern Perl, you might even prefer to put the mode in a separate argument:

open( OLDOU­T, ">&", STDOUT ) or die "Can't dup STDOUT to OLDOUT: $!\n";

- tye        

Replies are listed 'Best First'.
Re^2: "Pro Perl's" Redirecting Filehandles - Unexpected Result (space)
by ack (Deacon) on Jan 18, 2008 at 04:47 UTC

    Thanks, I'll give it a try.

    I was looking carefully at the material in the book and the author definitely uses the space between the direction mode symbol (the angle bracket) and the special symbol (the ampersand).

    In the previous section (which discusses duplicating files) of the book, the author has a special not that says: "These special modes only work with the two-argument form of open; they do not work with teh mode separated from the name." So I had presumed that the 3-argument form of open wouldn't work. I'll give it a try...maybe the author is wrong.

    Just out of curiosity, in the 2-argument form of open, why in your opinion does the space between the mode specifier (i.e., the angle bracket) and the ampersand cause the problem of creating the mysterious file &STDOUT?

    ack Albuquerque, NM

      Because otherwise how would you open a file named "&STDOUT" if that was what you wanted to do? Before there was 3-argument open, the safe way to open a file was open HANDLE, "> $name" (with the space being important there) and that should be safe even if the user entered "&STDOUT" for $name.

      - tye        

        Thanks tye. I think I'm getting confused...must be age senility. I appreciate your patience and attempt to help me and appologize in advance for not letting this drop...but I am confused.

        I thought the author of the book was trying to show me a way to *not* create a real file (i.e., to *not* create the &SDTOUT file)...only create the log file...but still effectively re-direct SDTOUT without using the select($logfile) which only works as long as the programmer doesn't do an explicit print to SDTOUT (e.g., print SDTOUT "Hello, world\n" which would would not print to the log file, but rather would print to SDTOUT).

        But if the open creates &SDTOUT then the programmer has to invest in additional code to delete that file...which seems like that would defeat the intentions...IMHO.

        But in addition, based upon what you wrote, I'm still confused about why the 2-arguent version of open *without* the space between the direction and the & would *not* create the file, but with the space it does. Why does the lack of the space prevent the creation of the file?

        I think I'm also confused about the purpose of the anpersand (i.e., the &)...I thought it was a special symbol or prefix...that wouldn't get interpreted as a part of the filename, but rather was necessary to *prevent* the interpretation of SDTOUT as a file name. So...since I obviously don't understand the true nature of the & symble, can you enlighten me as to what *is* it's purpose.

        DarkLord1 Looking for Redemption in the Monastery