in reply to Re: Get filename of STDOUT
in thread Get filename of STDOUT

Bad news: even $0 won't work on all platforms. Win32, notably, does not support it.
ActivePerl 808 on Windows seems to support $0.

Replies are listed 'Best First'.
Re^3: Get filename of STDOUT
by gaal (Parson) on Aug 10, 2004 at 18:23 UTC
    Really? What does it say? (For named scripts; for one-liners?) Can you modify it?

    Last time I checked it said something useless like "perl.exe" or maybe nothing at all. That was in some 5.6 ActivePerl.

      ActivePerl build 810 (5.8.4) gives me the following:
      > perl -e "print $0" -e > echo print $0 >test.pl > perl test.pl test.pl
      5.6.1 on Linux gave me the same values for aproxamitly the same tests. (adjusting for shell differences)
      Setting $0 has no noticeable effect on windows that I could see.
        Wow, I was really sure I did see this broken on Windows, but you're right, it does work.

        What really *doesn't* happen is this name being propagated to the process list (either ctrl-shift-esc or ps if using Cygwin). Apparently the Windows kernel doesn't honor changing $0 with the same method perl does it on unix.

        Now that I see I was wrong, I looked this up in perlvar:

        On some (read: not all) operating systems assigning to $0 modifies the argument area that the "ps" program sees. On some platforms you may have to use special "ps" options or a different "ps" to see the changes. Modifying the $0 is more useful as a way of indicating the current program state than it is for hiding the program you're running.

        I stand corrected!

      perl -e "print $0" prints -e. Oops, silly me! perl test.pl prints test.pl where test.pl is print $0;.
      >perl -v This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2001, Larry Wall Binary build 633 provided by ActiveState Corp. http://www.ActiveState. +com Built 21:33:05 Jun 17 2002