perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bengt Rasmusson" <ben...@kabkonsult.se>
Subject RE: Modules not functioning correctly
Date Wed, 26 Nov 2003 12:00:36 GMT
When encountering the problem mentioned in this thread I have successfully
used Angus solutions - I usually use solution 2. But today I experienced
something curious (I am using b08).

In my previous .epl pages I successfully used "use conn;" which referred to
a file named conn.pm which at the top included the line "package conn;". In
conn.pm I have a sub called "connection" and in the .epl pages I refer to it
as "conn::connection()".

Today I created a new .epl file as a copy of one of the working .epl pages
and named it test.epl. I also created new .pm file called conn_pages.pm. In
test.epl I have the line "use conn_pages;" and in conn_pages.pm I have the
line "package conn_pages;".  In conn_pages.pm I have a sub called
"connection" and in the .epl page I refer to it as
"conn_pages::connection()". But this don't work - now and then I receive the
familiar error message about undefined function.

I then made an exakt copy of test.epl and of conn_pages.pm, and named them
test2.epl and pagesconn.pm. In test2.epl I have the line "use pagesconn;"
and in pagesconn.pm the line "package pagesconn;". And as suspected: In
pagesconn.pm I have a sub called "connection" and in the .epl page I refer
to it as "pagesconn::connection()". This works fine!

So in summary:
"use conn;", "conn::connection()", conn.pm, and "package conn;" works!
"use conn_pages;", "conn_pages::connection()", conn_pages.pm, and "package
conn_pages;" does *not* work!
"use pagesconn;", "pagesconn::connection()", pagesconn.pm, and "package
pagesconn;" works!

Can anyone explane this behavior? Isn't underscore an allowed character in a
package name? Or in library file names? Or?

Bengt

-----Ursprungligt meddelande-----
Fran: Angus Lees [mailto:gus@inodes.org]
Skickat: den 28 juli 2003 19:48
Till: Justin Harrison
Kopia: embperl@perl.apache.org
Amne: Re: Modules not functioning correctly


At Sat, 26 Jul 2003 22:08:50 -0400, Justin Harrison wrote:
> All the files are uniquely titled. i.e, there is only one code.pm, one
> rules.pm, etc. Many other embperl pages "use" rules.pm, but they are all
the
> same files.

When you do "use rules;" the first time, perl finds the right rules.pm
file and compiles it.  It then remembers that it has done so (in %INC)
and later "use rules;" invocations do nothing.  This means that every
page after the first (for each apache child process), encounters an
undefined &codeform.


So you have several solutions:

1.  ensure rules.pm gets reloaded every time.

Try replacing "use rules;" with "do $path_to_rules_pm;".  This will
force perl to recompile rules.pm every time.  Fairly nasty since it
ends up duplicating the function for each caller, each time its run.


2.  compile it once and put &codeform somewhere you can find it.

Add "package rules;" to the top of rules.pm.

Everywhere you use "&codeform", replace it with "&rules::codeform".

This is the most efficient solution, since the first bit of code to
"use rules" actually loads and compiles rules.pm and then everyone
just refers to the same function.


3.  compile it once and import an alias for &codeform into each
    caller.

Add this to the top of rules.pm:

 package rules;

 use base qw(Exporter);
 our @EXPORT = qw(&codeform);

The other thing that "use" does is invoke "import PackageName".  In
addition to method (2), the above code inherits a suitable import()
function from Exporter.  This import function will make an alias for
&rules::codeform in all packages that do "use rules".

This is the easiest solution, since you shouldn't have to actually
modify any code beyond those simple changes to rules.pm.



For more information, see "perldoc -f package", "perldoc -f use" and
"perldoc Exporter" (probably in that order).

--
 - Gus

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Mime
View raw message