?

Log in

Ramblings Journals I Read Calendar The Dirt MegaZone's Waste of Time Older Older Newer Newer
Perl questions on 'use' inheritance - MegaZone's Safety Valve
The Ramblings of a Damaged Mind
zonereyrie
zonereyrie
Perl questions on 'use' inheritance
I've been out of serious Perl coding for a while, and I started in Perl 4 (require) days, and never had to do anything too fancy with 'use' before, so I'm seeking wisdom. OK, for the existing ASP and ColdFusion versions of the code, we have a global 'include' file that includes the various files, then each script need only include that file to access the functions, etc.


<!-- #INCLUDE FILE = "pcConstants.asp" -->
<!-- #INCLUDE FILE = "pcFunctions.asp" -->
<!-- #INCLUDE FILE = "pcErrors.asp" -->
<!-- #INCLUDE FILE = "pcXML.asp" -->
<!-- #INCLUDE FILE = "Merchant.asp" -->
<!-- #INCLUDE FILE = "Kiosk.asp" -->


I discovered that doing a similar thing in Perl, with 'use', does not work. If I 'use' the function files in a global file, like 'pcGlobal.pm', then 'use pcGlobal' in other files, the namespace isn't inherited, so I can't just call functions from the function files in the current file without explicitly specifying the package name - which makes the include file pointless. Right now I'm just doing a 'use' on each function file every place it is needed instead. But, for purposes of keeping each implementation as much like the others as possible, if I can find the magic to make the include file work, I'll take it.

The other problem I have is that neither ASP nor CF seem to have a problem with recursion. Say a routine in pcFunctions is used in Merchant, and a routine in Merchant is used in pcFunctions. In ASP and CF, both of these files are included in pcInclude, and both of them them include pcInclude. So no problem, the functions are all in a flat namespace and the calls work just dandy.

Perl got REALLY unhappy when I tried to do 'use Merchant' in pcFunctions, and 'use pcFunctions' in Merchant - it recurses. The work around for now is doing 'use pcFunctions' in Merchant, and then using the explicit package name on the calls in pcFunctions (Merchant::*) instead of doing the include. Is this the best way to handle it?

Thanks.

I am: confused confused
Current Media: office buzz

5 STDOUT || STDIN
Comments
solipsistnation From: solipsistnation Date: May 6th, 2003 12:30 pm (UTC) (Direct Link)

require, like this:

#!/usr/bin/perl

use Socket;
use Sys::Hostname;
require "flush.pl";
require "chat-defs.pl";
require "chatsubs.pl";
require "chatevents.pl";
require "chatconfig.pl";
require "chatbot-parser.pl";
require "spewguts.pl";


zonereyrie From: zonereyrie Date: May 6th, 2003 12:39 pm (UTC) (Direct Link)
Ok. I was trying to be 'good' and use 'use' since it is the Perl 5 way. :-) But I have no problem going back to 'require'.
stormcrow42 From: stormcrow42 Date: May 6th, 2003 01:45 pm (UTC) (Direct Link)

man perlmod

Read the perlmod documentation files, and bone up on @EXPORT.
zonereyrie From: zonereyrie Date: May 6th, 2003 02:12 pm (UTC) (Direct Link)

Re: man perlmod

I am using @EXPORT:
package pcFunctions;
use 5.008;
use strict;
use warnings;
use English;
use Exporter;
use CGI 'param';
use pcConstants 2.0;
use pcErrors 2.0;
use PayCash;
use vars qw($VERSION);
$VERSION = "2.0";

our @ISA = qw(Exporter);
our @EXPORT =
    qw(fprintf getComparison printf warning debug DebugLevel SetDebugLevel


That's truncated since @EXPORT is long in that case.

From perlmod it looks like 'require' is the way to go for the recursion issue:
       In general, "use Module ()" is recommended over "require Module",
       because it determines module availability at compile time, not in the
       middle of your program's execution.  An exception would be if two mod-
       ules each tried to "use" each other, and each also called a function
       from that other module.  In that case, it's easy to use "require"s
       instead.


Others wrote to me about the pcInclude thing, saying it *should* work. So I was probably doing something boneheaded.

There are days I really feel like a complete moron, I used to work with Perl all the time. Now I'm completely frustrated by my own rustiness. And, of course, this is all under a 'needed ASAP' guideline, so I don't have a lot of time to re-read the books, etc.

What I have so far is working, but I get the feeling it'd make people cringe.
zonereyrie From: zonereyrie Date: May 6th, 2003 02:49 pm (UTC) (Direct Link)
Oh, if anyone wants to cringe, just grab this.

We give this away like free candy to anyone interested in our stuff (I work for Cyphermint BTW), so there is no problem just tossing these out there. I have two XML related files left to port, then I plan to go back and clean things up, add more comments, etc. I know things aren't super pretty right now, functionality was the first priority. The structure is also wonky, but that's a business requirement. One of the requirements was to stick as close as possible to the structure used by the ASP and ColdFusion so that we can have one set of install guides, help files, etc, that apply to all of them. I didn't write the ASP or CF versions.

From an email exchange I think the pcInclude problem was that I was using 'package pcInclude;' at the top of the file, but I wasn't re-exporting anything from the files included therein. I was advised to try it without the package declaration, I intend to test that as soon as I get some time.

My current focus is XML:LibXML - doing XML in Perl for the first time so I'm still sorting that out.
5 STDOUT || STDIN