Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 24 Mar 2002 11:18:05 +0100
From:      F.Xavier Noria <fxn@isoco.com>
To:        Илья Шипицин <ilia@chel.skbkontur.ru>
Cc:        questions@FreeBSD.ORG
Subject:   Re: Perl thing
Message-ID:  <20020324111805.2c05b3a6.fxn@isoco.com>
In-Reply-To: <20020324112440.B97396-100000@sol.chel.skbkontur.ru>
References:  <20020324112440.B97396-100000@sol.chel.skbkontur.ru>

next in thread | previous in thread | raw e-mail | index | archive | help
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?20020324111805.2c05b3a6.fxn>