Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Easing cross-platform scripting -- portable scripts - OS agnostic tools

by Discipulus (Abbot)
on Nov 27, 2018 at 08:28 UTC ( #1226408=note: print w/replies, xml ) Need Help??

in reply to Easing cross-platform scripting

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!


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.

Replies are listed 'Best First'.
Re^2: Easing cross-platform scripting -- portable scripts - OS agnostic tools
by White Raven (Initiate) on Nov 27, 2018 at 10:34 UTC
    Thanks. This was really helpful. Also note taken on the choice of title for future posts.
      ... choice of title for future posts.

      Why not make it easier on future Seekers of Perl Wisdom concerning this topic and change the title of this post? Please see How do I change/delete my post? for guidance.

      Give a man a fish:  <%-{-{-{-<

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1226408]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-01-26 17:53 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (69 votes). Check out past polls.