in reply to Re: Test for OS version in Perl
in thread Test for OS version in Perl

I think I found that "perlvar", and I saw that $^O, but I didn't think it would give me what I want. After thinking about it, I guess it would work in most cases, but not in my specific case.

I have a PC that has Cygwin on it (and Perl comes with the Cygwin), and I also have ActivePerl on it. I brought up a Cygwin window, and wrote a little script like this:

#!/usr/bin/perl
#!C:/Perl/bin/perl
print "^O\n";

When I run it like that I get: Cygwin

If switch the two comment lines (so #!C:/Perl... is the first line), I get: MSWin32

What I really want to know is that if I am the running the script from a Cygwin window, regardless of which of those two builds of Perl runs the script, it will tell me I am running in a Cygwin environment. If I run the script from an XP CMD window, I want it to tell me I am running in a windows environment.

Replies are listed 'Best First'.
Re^3: Test for OS version in Perl
by xdg (Monsignor) on Oct 20, 2007 at 22:45 UTC
    #!/usr/bin/perl #!C:/Perl/bin/perl

    The Cygwin shell will look at the 'shebang' line to determine what interpreter to use to run your script. So it will either run the cygwin perl (/usr/bin/perl) or the Activestate one (C:/perl/bin/perl) depending on which you put first. However, if you run it from an XP CMD window, I don't think it pays any attention to the shebang line. It'll be run with whatever perl you use from the command line or whatever perl is associated with it in the registry.

    Put differently -- $^O tells you what the executing perl was compiled for, not where it was launched from.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      So, I guess you're saying that there is no way to determine "where it was launched from", correct?

        See the discussion under $^X in perlvar for ways of discovering the path of the executable being run. That should allow you to determine whether the currently running version of perl is a native or cygwin variant in all but the most extreme cases of deliberate desception.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        So, I guess you're saying that there is no way to determine "where it was launched from", correct?

        I suspect that other than trying to do some inspection of the process tree, the answer is no.

        However, this is sounding like an XY Problem. What are you actually trying to do such that it matters where a Perl binary was launched from?

        It seems to me that once a binary is launched, it will behave according to how it was compiled, not how it was launched. The only difference I could imagine would be what the command line argument array looks like and what the inherited environment is.

        That might be something to try. E.g. print_env.pl:

        #!C:/perl/bin/perl #!/usr/bin/perl use strict; use warnings; print "\$0: $0\n"; print "ARGV: ", join( q{,}, map { qq('$_') } @ARGV ), "\n"; print "\nEnvironment:\n"; print "$_: $ENV{$_}\n" for sort keys %ENV;

        Run that from cygwin's shell and cmd.exe, try swapping around the shebang lines, etc. From cygwin's shell, try invoking it directly versus calling it as an argument to perl:

        $ chmod +x print_env.pl $ ./print_env.pl one two three $ perl print_env.pl one two three

        I think if you call it directly, you'll see something different in $0 than if you call it as the argument to perl. And you might see differences in %ENV. You might even set an environment variable in your cygwin .bashrc that you could use as a flag.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        If you run your script as perl script.pl instead of script.pl or ./script.pl (as the case may be) then I think you'll always get what you want. For me on Cygwin:
        Rob@desktop2 ~/pscrpt $ cat try.pl #!C:/perl58_M/bin/perl print $^X, "\n"; print $^O; Rob@desktop2 ~/pscrpt $ perl try.pl /usr/bin/perl.exe cygwin Rob@desktop2 ~/pscrpt $ ./try.pl C:\perl58_M\bin\perl.exe MSWin32 Rob@desktop2 ~/pscrpt
        If you want to take advantage of crappy shortcuts, then be prepared to face the associated inconvenience :-)

        Cheers,
        Rob
        Update: Hmmmm ... after about one minute's reflection, it has oocurred to me that I might have missed the point .... (apologies if that's the case).