Date: Wed, 27 Mar 2002 22:17:09 +0100 From: F.Xavier Noria <fxn@isoco.com> To: ilia@chel.skbkontur.ru Cc: questions@FreeBSD.ORG Subject: Re: perl thing (again) Message-ID: <20020327221709.44d78c3a.fxn@isoco.com> In-Reply-To: <20020328000648.J83795-100000@sol.chel.skbkontur.ru> References: <20020328000648.J83795-100000@sol.chel.skbkontur.ru>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 28 Mar 2002 00:10:13 +0500 (YEKT) Илья Шипицин <ilia@chel.skbkontur.ru> wrote: : I was told by the list to use $ENV{'OSTYPE'} to ensure whether program : runs under FreeBSD or not. It works well from regular user shell, but from : crontab it says "Use of uninitialized value". No, no, $ENV{'OSTYPE'} is not the correct answer, $^O is, did you miss my message? I copy it below: the first part says what is the closest we can get to #define formally, the second one says how that #define thing is done the Perl way. -- fxn On Sun, 24 Mar 2002 11:26:25 +0500 (YEKT) Илья Шипицин <ilia@chel.skbkontur.ru> wrote: : is there anything that I could use it in perl program like I can write in : C: : : #ifdef __FreeBSD__ : : #endif perl runs your script through the C preprocessor before its very compilation with the option -P, for instance print 'This is a '; #if defined(__FreeBSD__) print 'FreeBSD'; #elif defined(__linux__) print 'Linux'; #elif defined(__sun) print 'Sun'; #endif print " box.\n" gives this when executed with -P: bash-2.05a$ perl -P foo.pl This is a FreeBSD box. This has its gotchas however, since some preprocessors see in "s/foo//;" a C++-like comment. See the documentation in perldoc perlrun for them. Another approach, more Perlish IMO, would be to check the name of the operating system at runtime with $^O (that is a capital-oh): bash-2.05a$ perl -le 'print $^O' freebsd and either write chains if-elsif-else, or factor out platform dependant code in interfaces, implement them in corresponding modules and have a driver that requires the correct code in runtime and eventually has common code. This is what the standard module File::Spec does for instance, he begins like this: my %module = (MacOS => 'Mac', MSWin32 => 'Win32', os2 => 'OS2', VMS => 'VMS', epoc => 'Epoc'); my $module = $module{$^O} || 'Unix'; require "File/Spec/$module.pm"; as you see, what is platform dependant is defined in File::Spec::Unix, File::Spec::Mac, and so on, and Spec.pm, which is the only module loaded by users, requires behind the scenes the suitable implementation. -- fxn To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020327221709.44d78c3a.fxn>