From owner-freebsd-questions Sun Mar 24 2:14:59 2002 Delivered-To: freebsd-questions@freebsd.org Received: from smtp04.retemail.es (smtp04.iddeo.es [62.81.186.14]) by hub.freebsd.org (Postfix) with ESMTP id A441737B417 for ; Sun, 24 Mar 2002 02:14:52 -0800 (PST) Received: from conway.localdomain ([62.174.23.182]) by smtp04.retemail.es (InterMail vM.5.01.03.02 201-253-122-118-102-20010403) with SMTP id <20020324101445.SAUD1921.smtp04.retemail.es@conway.localdomain>; Sun, 24 Mar 2002 11:14:45 +0100 Date: Sun, 24 Mar 2002 11:18:05 +0100 From: F.Xavier Noria To: Илья Шипицин 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> X-Mailer: Sylpheed version 0.7.4 (GTK+ 1.2.10; i386-portbld-freebsd4.4) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sun, 24 Mar 2002 11:26:25 +0500 (YEKT) Илья Шипицин 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