[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] conditional use of Mac::XXX.pm modules?



At 16.58 98.01.26, Paul DuBois wrote:
>require Config;
>$OS = $Config::Config{'osname'};
>$macintosh = ($OS =~ /^macos$/i);

Easier:

$macintosh = ($^O eq 'MacOS');


>I'd then like to include Mac OS-specific modules if $macintosh is
>true.  But this doesn't work:
>
>if ($macintosh)
>{
>        use Mac::StandardFile;
>}
>
>
>(It works on a Macintosh, of course; it doesn't work under UNIX, because
>it fails at compile-time.)  Changing the code as follows works under both
>systems:
>
>if ($macintosh)
>{
>        eval "use Mac::StandardFile;";
>}
>
>
>But is this really the "right" way to do it?  Is there a better way?

Depends on how you want to do it.  This should work:

$macintosh = ($^O eq 'MacOS');
if ($macintosh) {
  require Mac::StandardFile;
  Mac::StandardFile->import();
}

use() imports anything the module tries to export by default.  require()
does not.  So you call the import() method manually with require.  You can
also use the form:

  require ':Mac:StandardFile.pm';

which delays the require until runtime.  See p. 286 of the Blue Camel for
more details.

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==       New Book: MacPerl:  Programming for the Rest of Us       ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch