APM: Use of the ampersand, "&"

Thomas L. Shinnick tshinnic at io.com
Thu Sep 29 12:05:52 PDT 2005

At 12:54 9/29/2005, Taylor Carpenter wrote:
>don james wrote:
>> She has an example where the "&" precedes a filename.  It is used in the
>> following context:
>> open(SAVED, ">&STDOUT");
>That is for duping the file handle.  See dup(2) and perldoc -f open.

perlfaq5 "How do I dup() a filehandle in Perl?" gives a good example.  You already have a file open, and now you want to re-use that handle when opening another file.
    open(LOG, ">>/foo/logfile");
and later
    open(STDERR, ">&LOG");
This allows the second open to be ignorant of what file is being used by the first.  Useful also if you want to force some output to wahtever STDERR points to
    open(TRACE, ">&STDERR");
without some underlying routine having to know.  Check out Test::Builder where they do open(TESTERR,">&STDERR') to keep a copy of STDERR in case the following code being tested changes where STDERR outputs to.


More information about the Austin mailing list