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

Re: [MacPerl] Portable code



At 15.49 -0400 1999.06.02, James Lund wrote:
>I ported my program to the Mac and got it working, and then wrapped up the
>Mac specific stuff in a
>
>if ( $^O =~ /macos/i ) { Mac code }

Not that it matters much, but $^O eq 'MacOS' is better than $^O =~
/macos/i.  Equality tests are faster and less susceptible to error than
regexes, when you know exactly what the text will be.

>block.  But when I move it to a UNIX box, the Mac 'use MacModule;' statements
>cause the program to die ('cause the UNIX box is missing them).  How do I hide
>them from the interpreter when the program isn't running on a Mac?

There are various methods.  The Perl Cookbook goes over some of them.  One
I use  is:

  if ($^O eq 'MacOS') {
    require "Mac/Module.pm";  # yes, "/" does work here on a Mac
    Mac::Module->import;
  }

One drawback here is that subroutines are not seen at compile time, so you
cannot use bareword names.  For instance, if you would normally use this
code:

  use Mac::AppleEvents;
  print typeType;

It now has to be:

  require "Mac/AppleEvents.pm";
  Mac::AppleEvents->import;
  print typeType();

etc.  But other than that, it works fine.

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org