You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Raymond Wan <rw...@kuicr.kyoto-u.ac.jp> on 2008/02/14 14:33:34 UTC

handler using a module

Hi all,

I have written a handler to take care of requests and there is something 
unexpected [to me, anyway :-) ] happening which may be my fault, but I 
was wondering if someone can explain it to me.  Perhaps there is a 
problem with my programming style.

The code is long, so I'll briefly mention what I think is relevant 
[hopefully I don't leave anything out].  I have a handler called 
myHandler.pl:

-----
##  File myHandler.pm
package myHandler;

use lib 'my Path';
use myOwnFunctions;  ##  Located in "my Path"

my $ah = HTML::Mason::ApacheHandler -> new (...);

sub handler {
  my $r = shift;
  $ah -> handle_request ($r);
}

1;
-----

In the file myOwnFunctions.pl:

----
package myOwnFunctions;

sub myFunction {

}

1;
-----

So in an HTML file that will be processed by Mason, I call "myFunction 
()" and it says it is undefined.  This surprised me since I indicated 
that I am using myOwnFunctions in the handler with the "use" directive.  
Is that not the way to do it?  So, then I thought I should call 
myFunction as:

myOwnFunctions::myFunction ()

and that gave me an error.  By accident, I typed:

myHandler::myFunction ()

and that worked...  I don't quite know why.  I presume I have done a 
mistake somewhere.  Or is this how it should be?

Thanks!

Ray



Re: handler using a module

Posted by Raymond Wan <rw...@kuicr.kyoto-u.ac.jp>.
Hi Perrin,

Perrin Harkins wrote:
> Yes, there is.  Read the information in the Mason documentation about
> namespaces.  There should be examples in there of how to do it.  


Thanks for the tip; I found it quite easily in the documentation and 
indeed there are examples and at least two options to try.  Thanks!


> The best way to do this is actually to not import it at all and just
> call myOwnFunctions::myFunction().  That saves some memory.  It is
> more typing though.  If that way of calling it isn't working for you,
> I suspect you might have a typo somewhere.  It should not be affected
> by Mason.  

I see.  I probably made a careless mistake somewhere...thank you for 
your help!

Ray



Re: handler using a module

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, Feb 15, 2008 at 12:16 PM, Raymond Wan <rw...@kuicr.kyoto-u.ac.jp> wrote:
>  I see.  So if myOwnFunctions is used by many of my components, there is
>  no way to have it load once but used by all of the components?

Yes, there is.  Read the information in the Mason documentation about
namespaces.  There should be examples in there of how to do it.

The best way to do this is actually to not import it at all and just
call myOwnFunctions::myFunction().  That saves some memory.  It is
more typing though.  If that way of calling it isn't working for you,
I suspect you might have a typo somewhere.  It should not be affected
by Mason.

- Perrin

Re: handler using a module

Posted by Raymond Wan <rw...@kuicr.kyoto-u.ac.jp>.
Hi Malcolm,


Malcolm wrote:
> Mason uses it's own namespace for the code it generates from your templates. 
> So unless you have a "use lib...; use myOwnFunctions;" in your template, it 
> won't have your functions in scope.
>   


I see.  So if myOwnFunctions is used by many of my components, there is 
no way to have it load once but used by all of the components?  That was 
my motivation for putting the two "use" lines in the handler.  I guess 
in the end, the added execution time is small; I was just wondering if 
there was a more efficient way of doing it.


> imports "myOwnFunctions" into the myHandler namespace, which is visible from 
> Mason (either because you did a "use" or through Mason magic).
>   


I see -- thank you for the explanation!

Ray



Re: handler using a module

Posted by Perrin Harkins <pe...@elem.com>.
On Thu, Feb 14, 2008 at 10:40 AM, Malcolm
<mj...@liminalflux.net> wrote:
>  Mason uses it's own namespace for the code it generates from your templates.
>  So unless you have a "use lib...; use myOwnFunctions;" in your template, it
>  won't have your functions in scope.

I don't think that explains why the fully-qualified
myOwnFunctions::myFunction() isn't working.

- Perrin

Re: handler using a module

Posted by Malcolm <mj...@liminalflux.net>.
On Thursday 14 February 2008 08:33:34 am Raymond Wan wrote:

> So in an HTML file that will be processed by Mason, I call "myFunction
> ()" and it says it is undefined.  This surprised me since I indicated
> that I am using myOwnFunctions in the handler with the "use" directive.
> Is that not the way to do it?  So, then I thought I should call
> myFunction as:
>
> myOwnFunctions::myFunction ()
>
> and that gave me an error.  By accident, I typed:
>
> myHandler::myFunction ()
>
> and that worked...  I don't quite know why.  I presume I have done a
> mistake somewhere.  Or is this how it should be?


Mason uses it's own namespace for the code it generates from your templates. 
So unless you have a "use lib...; use myOwnFunctions;" in your template, it 
won't have your functions in scope.


This:
> package myHandler;

> use lib 'my Path';
> use myOwnFunctions;  ##  Located in "my Path"

imports "myOwnFunctions" into the myHandler namespace, which is visible from 
Mason (either because you did a "use" or through Mason magic).



Re: handler using a module

Posted by Raymond Wan <rw...@kuicr.kyoto-u.ac.jp>.
Hi Marcel,

Marcel Greter wrote:
> I can only tell why myHandler::myFunction () is working.
> I think you export the myFunction in Module myOwnFunctions.
> I assume you didn't post that part of the code?


Ah, yes -- I didn't think that was doing it but yes, I also have:

require Exporter;

our @ISA = qw (Exporter);
our @EXPORT = qw (myFunction);
our @EXPORT_OK = qw (myFunction);


> Therefore myFunction is also accessible within the namespace myHandler,
> and therefore the second version is working as I would expect.
>
> But I cannot tell you why myOwnFunctions::myFunction itself is not 
> working.


Hmmm, I see.

Is there any problem if I continue with myHandler::myFunction () for 
now?  Maybe later on, I will figure out what is wrong...perhaps some 
other silly mistake that I also didn't post?  I will continue 
investigating...

As someone new to Mason and modperl, what is the correct way of doing 
this?  i.e., the correct practice?  Should myOwnFunctions be exporting?  
More importantly, should I do this:

use lib 'my Path';
use myOwnFunctions;  ##  Located in "my Path"

I thought by doing so, it loads myOwnFunctions once per process.  But 
I'm having doubts about whether what I'm doing makes any sense...  :-)

Thank you!

Ray