Manjunath has asked for the wisdom of the Perl Monks concerning the following question:

I had developed a perl script to execute SNMP commands. The program is working fine. For windows, the program takes the input from c:\snmpauto. For linux, the program takes the input from /root/snmpauto. My problem is that i need to manually tell the program if i am running a windows os or linux os. Is there a way that the program recognizes the os currently running and pick up the input files from the location.

Replies are listed 'Best First'.
Re: how to recognize windows and linux os
by ysth (Canon) on Jan 30, 2006 at 14:07 UTC
    The $^O variable tells you what OS perl was built for; possible values are listed in perldoc perlport.
Re: how to recognize windows and linux os
by jasonk (Parson) on Jan 30, 2006 at 14:07 UTC

    From the perlvar documentation:

       $OSNAME
           $^O     The name of the operating system under which this copy of Perl
                   was built, as determined during the configuration process.  The
                   value is identical to $Config{'osname'}.  See also Config and
                   the -V command-line switch documented in perlrun.      
    

    Of course you could also just check and see which of the directories you are interested in exists, rather than worrying about what the operating system is.


    We're not surrounded, we're in a target-rich environment!
Re: how to recognize windows and linux os
by Not_a_Number (Prior) on Jan 30, 2006 at 14:11 UTC
    Check out $^O in perldoc perlvar.
Re: how to recognize windows and linux os
by glasswalk3r (Friar) on Jan 30, 2006 at 14:42 UTC

    Use the Config module. Sometimes $^O doesn't give you all the details:

    C:\>perl -MConfig -e "print $Config{osname},\"\n\";" MSWin32
    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
      'perldoc perlvar'
      $OSNAME $^O The name of the operating system under which this copy of Perl was built, as determined during the configuration process. The value is identical to $Config{'osname'}. See also Config and t +he -V command-line switch documented in perlrun.
        Defining Your Needs

        If you know that your module simply will not run in a certain environment, you should set up prerequisites. These allow you to provide a level of safety for your users. Prerequisites include:

        OSes that your module will not run under

        Check $^O and %Config for this. $^O will tell you the name of the operating system. Sometimes, this isn't specific enough, so you can check %Config.

        use Config; if ( $Config{ osname } ne 'solaris' || $Config{ osver } < 2.9 ) { die "This module needs Solaris 2.9 or higher to run.\n"; }

        Source: http://www.perl.com/lpt/a/2005/04/14/cpan_guidelines.html

        Alceu Rodrigues de Freitas Junior
        ---------------------------------
        "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

        jdporter fixed markup

Re: how to recognize windows and linux os
by dorward (Curate) on Jan 30, 2006 at 14:58 UTC

    Another option is to forget OS detection and just look for the files.

    open (FH, 'c:/snmpauto') || # I don't know Windows so this path syntax may be off open (FH, '/root/snmpauto') || die (q(Couldn't open the file!));
      Or ask the user where snmpauto resides
        That would require trusting the user. From the locations where he's chosen to put these files, I take it only the system administrator is supposed to be able to alter the contents of them. (I still would have put it under /etc rather than /root though.)
Re: how to recognize windows and linux os
by radiantmatrix (Parson) on Jan 30, 2006 at 20:32 UTC

    Why not simply have a list of possible locations the file is allowed to reside, and check them in a well-defined (and documented) order until you hit one? This is a good idea because it won't require any additional logic if you choose to support another OS (with yet another path). Example:

    # I assume 'snmpauto' is the name of a file. use File::Spec; my $snmp_file = find_snmpauto(); sub find_snmpauto { my $filename = 'snmpauto'; my @paths = qw(c:\\ /root); for (@paths) { my $name = File::Spec::catfile($_,$filename); return $name if -f $name; } # we hit here only if no file was found. die "Can't find a file '$filename' in the search path (path includ +es:" .join(', ',@paths) .")\n"; }

    This will result in the first filename you find to be correct being stored in $snmp_file; it will die (with a list of acceptable paths) if it fails. This way, if you add a new location, you only have to add to the @paths array.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: how to recognize windows and linux os
by Anonymous Monk on Feb 02, 2006 at 11:53 UTC
    Thanks Everybody for the reply. It is unfair to pick up just one of the solutions but i have to. I will use solution provided by radiantmatrix as i am aware of the files that i am going to handle. In this way i am double check that my input file does exists along with os detection.