P is for Practical | |
PerlMonks |
Re: Easing cross-platform scripting -- portable scripts - OS agnostic toolsby Discipulus (Canon) |
on Nov 27, 2018 at 08:28 UTC ( [id://1226408]=note: 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:
As said in perlvar concerning $^O on windows it always returns MSWin32 but using Win32 module you have something better:
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 Section
Seekers of Perl Wisdom
|
|