Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Backward compatibility: $^O in perl 4

by bronto (Priest)
on May 07, 2003 at 13:30 UTC ( #256193=perlquestion: print w/replies, xml ) Need Help??
bronto has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

A colleague of mine is programming a script that does sort of system inventory on Unix machines. For some reason that is too long to explain here, he needs the script to be backward-compatible with perl 4. A problem he's currently facing is that he can't find an analogous of the $^O variable of Perl 5.

It's been too long since I last used Perl 4... Can you help him or should he definitely resolve to capturing the output of the uname command?

Thanks in advance

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: Backward compatibility: $^O in perl 4
by antirice (Priest) on May 07, 2003 at 14:45 UTC
    A possible solution may be to look at $ENV{'OSTYPE'}. The variable is set by the shell on most Unix systems and as such should serve your purpose.

    Please realize that $ENV{'OSTYPE'} doesn't necessarily always contain the OS as well as the version. As an example, a machine of mine running FreeBSD 4.7 with perl 5.8.0 prints the following:

    > perl -e 'print $ENV{'OSTYPE'}," ",$^O,$/' FreeBSD FreeBSD >
    This is my only machine that reports without the version, but just a head's up in case.

    Update: Ah, this is wonderful:
    > uname FreeBSD >
    So perhaps $ENV{'OSTYPE'} is just as good as attempting to capture the output from uname.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      No, worse. uname is available on pretty much every flavour of Unixoid system under the sun, while the presence of $OSTYPE depends on additional factors.

      Makeshifts last the longest.

Re: Backward compatibility: $^O in perl 4
by Maddingue (Sexton) on May 07, 2003 at 15:15 UTC
    Looking throught the Perl 4 documentation and library, I can't find anything that could help you. Using uname is indeed the simplest solution, but be careful that very few options of uname are common (or have the same meaning) upon the various Unix systems. I remember facing some troubles on HPUX and IRIX. Probably a good start is to look at how autoconf or the Configure script from Perl do their system detection.
Re: Backward compatibility: $^O in perl 4
by krujos (Curate) on May 07, 2003 at 16:42 UTC
    Why not just use
    $OS_TYPE=`uname -s` $OS_VERSION=`uname -r
    Seems eaiser. Update: This way there are no special cases needed in the code.

      Actually there is at least one special case: AIX. AIX does it its own way: uname -r returns the minor version of the OS, while uname -v returns the major version; this is the output on AIX 4.3:

      bash$ uname -s AIX bash$ uname -r 3 bash$ uname -v 4

      With your code the system will appear to have AIX 3 intead of 4...

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
        Whoa. I have never caught that before, never had to worry about versions on AIX. Thanks, I will change my code accordingly. You Rock!
        Josh
Re: Backward compatibility: $^O in perl 4
by hawtin (Prior) on May 08, 2003 at 06:30 UTC

    I had the same problem and came across a solution that gcc uses. For various reasons this is expressed as a bash script and a more recent version is probably abailable. I leave the conversion to Perl as an exercise for the reader:

    # Short circuit this function if we know which system we are on if [ $OS_SYSTEM_REALLY ]; then echo "${OS_SYSTEM_REALLY}" return fi # First get the values for this machine # Code lifted from config.guess in the gcc software OS_UNAME_M=`(uname -m) 2>/dev/null` || OS_UNAME_M=unknown OS_UNAME_S=`(uname -s) 2>/dev/null` || OS_UNAME_S=unknown OS_UNAME_R=`(uname -r) 2>/dev/null` || OS_UNAME_R=unknown OS_UNAME_V=`(uname -v) 2>/dev/null` || OS_UNAME_V=unknown OS_UNAME="${OS_UNAME_M}:${OS_UNAME_S}:${OS_UNAME_R}:${OS_UNAME_V}" # Note: order is significant - the case branches are not exclusive +. case "${OS_UNAME}" in sun4*:SunOS:5.[012345]*:*) echo "sol24" return ;; sun4*:SunOS:5.[6789]*:*) echo "sol26" return ;; sun4*:SunOS:[7891]*:*) echo "Unknown Solaris version" >&2 echo "Try setting \$OS_SYSTEM_REALLY to sol26" >&2 echo "unknown" return ;; sun4*:SunOS:*:*) echo "sun" return ;; *:AIX:1:4) echo "aix41" return ;; *:IRIX*:5.*:*) echo "sgi" return ;; *:IRIX*:6.[2345]:*) echo "irix62" return ;; 9000/[78]??:HP-UX:*:*) echo "hp" return ;; i[345678]86:Linux:*:*) echo "linux" return ;; sparc:Linux:*:*) echo "ulinux" return ;; *:*:*:*) # If we get here then we have something weird echo "Unknown system \"${OS_UNAME}\"" >&2 echo "Try setting \$OS_SYSTEM_REALLY to one of sun, sol, h +p etc" >&2 echo "unknown" return ;; esac
Re: Backward compatibility: $^O in perl 4
by dragonchild (Archbishop) on May 07, 2003 at 13:49 UTC
    It's been a few hours since I used Perl4, but what is he doing that is system-dependent to the point that he needs to worry about uname?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      I thought it was obvious from the question: the OS name, for example... and the OS version


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://256193]
Approved by Corion
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2018-08-19 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (186 votes). Check out past polls.

    Notices?