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

Re: [MacPerl] How can you get runtimes to find modules loaded with'use' ?



On Thu, 14 Mar 1996 HARBAUGH@FCRFV2.NCIFCRF.GOV wrote:

> Folks:
> 
> I have a script which begins like this:
> 
>       use AnyDBM_File;
>       use Fcntl;
> 
>       tie(%T,AnyDBM_File,'Development:testit',O_RDWR|O_CREAT,640)
>            || die "can't open db file: $!\n";
> 
>        ...
> 
> The script runs under the macperl application, creating the DB file as
> expected.  However, when I save the file as a runtime (setting the @INC
> array by hand), I can't get the program to find the module files in the
> lib directory with the 'use' command.  NOTE that the program *CAN* find
> the modules with the 'require' command; that is, the following statment
> *does not* cause the program to stop:
> 
>       require AnyDBM_File.pm || die "can't open AnyDBM_File\n";
> 
> Substituting 'require' for 'use' works partly; in a runtime (*only*) the
> tie command fails if the DB file does not already exist.
> 
> Although I am using the MacPerl 5.0.7r1m application, I encountered this
> same problem with the previous version (5.0.6r1m).  I have duplicated
> the problem on two different macs, a IIci and a Centris 650.
> 
> I would appreciate it if someone could help me out with this.  Let me
> know if this is a bug, or give me an example of a runtime which can
> create a DB file.
> 
> PS: Is it possible to "save" the MacPerl application's library path
>     in a runtime so that it doesn't have to be explicitely set within
>     a script?

Well, if I'm not mistaken I think the problem is that 'use' files get 
compiled and run at the moment it's encountered.  Here are some excerpts 
from the pod's:

Perl modules are included by saying 

        use Module;

This is exactly equivalent to 

        BEGIN { require "Module.pm"; import Module; }

...and...

Because the use statement implies a BEGIN block, the importation of
semantics happens at the moment the use statement is compiled, before the
rest of the file is compiled. 

...and...

A BEGIN subroutine is executed as soon as possible, that is, the moment it
is completely defined, even before the rest of the containing file is
parsed. You may have multiple BEGIN blocks within a file--they will
execute in order of definition. 

So, one way around your problem is to do the equivalent of 'use' without 
the BEGIN block:

require "Module.pm"; import Module;

...or, you could arrange to change @INC before the 'use' statement:

BEGIN { @INC = ... }

use Module.pm;

...or there may be a way to include the library path in the runtime, I 
don't know for sure.

(See the perlmod pod for more info on 'use' and 'BEGIN.')

John Peterson -- University Networking Services -- Brigham Young University
Internet: John_Peterson@byu.edu                       Phone: (801) 378-5007