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

Re: [MacPerl-AnyPerl] Multi Platform Scripts



At 9:35 -0500 1999.08.09, Richard L. Grubb wrote:
>I working on a perl script that will be executed on at least a Mac and a
>UNIX box. When I try to run it on UNIX, the script fails because it
>cannot find Mac Specific modules, even though they are inside of if
>blocks that are not to be executed on a UNIX machine.

use is executed at compile time, regardless of where it is in a program
(unless it is in an eval() (which is different than an eval{}).

>How do I hide
>these use statements? Or do I just install dummy .pm files on my UNIX
>machine?

>sub BEGIN
>{
>  unshift (@INC, "/acct/rlg0301/perl5/lib");
>  unshift (@INC, "/acct/rlg0301/perl5/lib/aix");
>  unshift (@INC, "/acct/rlg0301/perl5/site_lib");
>  unshift (@INC, "/acct/rlg0301/perl5/site_lib/aix");
>  print "@INC \n";
>}

Don't Do That unless perl cannot find the lib.pm pragma.  Instead:

  use lib "/acct/rlg0301/perl5/lib", "/acct/rlg0301/perl5/lib/aix",
    "/acct/rlg0301/perl5/site_lib", "/acct/rlg0301/perl5/site_lib/aix";


># for Mac users who forget to drag&drop the input file
>if ($^O eq 'MacOS')
>{
>  if ($#ARGV < 0)
>  {
>    use Mac::StandardFile;
>    $file = StandardGetFile('', 'TEXT');
>    if ($file->sfGood())
>    {
>      push(@ARGV, $file->sfFile());
>    }
>    else
>    {
>      exit(1);
>    }
>  }
>}

There are lots of ways to do this, and Perl Cookbook lists several of them.
One common way is to use require:

    require Mac::StandardFile;

This is fine for your situation here.  It will not, however, import the
symbols you need.  You need to either call the subroutine by its full name
(Mac::StandardFile::StandardGetFile()), or import it in a second line:

    import Mac::StandardFile;

This isn't always ideal, because require/import is not exactly the same as
use, but it should work for your situation.  Note that this is only for
delayed module usage.  If you want to trap module usage for errors, that's
something else.  See Perl Cookbook for examples (everyone should have this
book).

-- 
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-anyperl-request@macperl.org