Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello White Raven and welcome to the monastery and to the wonderful world of perl!

You already got a good clue: $^O is the perl special variable containing the OS, from the perl point of view. But with perl you have many many options to finely leverage the platform/architecture checking.

1) $^O being your first option: you can use it in if construct: if ($^O eq 'MSWin32 ) { ...' or in a dispatch thable:

my %clear_command = ( MSWin32 => 'cls', Linux => 'clear' ... ); system (1, $clear_command{ $^O } );

As said in perlvar concerning $^O on windows it always returns MSWin32 but using Win32 module you have something better:

print Win32::GetOSName(),Win32::GetOSVersion(); # output: Win10Business (64-bit)100179362002561

2) Config is a core module that holds ~900 values concerning the current perl installation: for example the architecture:

print $Config{archname}; # MSWin32-x64-multi-thread or if current perl is configured to use threads: $Config{usethreads} The module populates for you a readonly hash named %Config

3) the if module (not the normal if ) is useful to import the rigth module depending on situation: if you need to work with the OS name (given the above) you may need Win32 module to get the right name:  use if $^O eq 'MSWin32', 'MSWin32'; # note that it wants module in a string!

4) in perlvar look also at $] and $^V useful to inspect the current perl version (versions are a bit.. confusing in perl: recent history shows.. ;)

5) last but not least consider that many modules do their best in being agnostinc in respect of the operating system: come to mind Term::ReadLine that works fine on different platforms. Under the hood the module will load the appropriate module dependin on platforms: Term::ReadLine::Linux for example if run on Linux.

The very same does the best module for portable program: the core module File::Spec that abstracts many important file and path related operations being very OS agnostic

6) a look at perlport is recomended

good luck!

L*

PS (the title was: Perl Newbie question) next times try to choose more meaning titles for your questions: it wil be useful for future searcher!

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Easing cross-platform scripting -- portable scripts - OS agnostic tools by Discipulus
in thread Easing cross-platform scripting by White Raven

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-18 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found