domje has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Monks! I'm humbly seeking some guidance in order to find some peace...

I'm developing my own nuts-n-bolts CMS-like software for a few intranet applications. The issue I'm encountering is that when I try to use a module I've written via one of my Mason pages I'm getting a "Can't locate object method 'foo' via package 'bar'..."

This works just fine, mind you in external scripts. I can guess it's something with the way I'm blessing the reference? I don't know. My 'new' sub works and it returns a reference.

However...If I call another routine in the module I get the error mentioned above. Where oh where am I going wrong? Here's the Mason code (I'm declaring the database handle as a global in the autohandler):

... <%once> use lib '/var/www/nlt/libs'; use CMS::Users; my $u = CMS::Users->new(); $u->SetDBH($mysql_db_handle); print $u->GetUserList(); </%once> ...
There is litterally nothing else in this file but a paragraph tag at the bottom with the word "testing" in it.

Here's some code I threw together to troubleshoot the problem:

package CMS::Users; use Digest::MD5 qw(md5); use CGI; sub new { my $class = shift; my $self = { }; bless $self, $class; return $self; } sub SetDBH { my ($self, $dbh) = @_; if ($dbh) { $self->{DBH} = $dbh; return 1; } else { return undef; } } sub GetUserList { my ($self,%args) = @_; return "Sub call worked. I'm from: $self"; } 1;
Strangely enough the SetDBH routine works without incident. While GetUserList produces the error. What gives? I'm losing my mind and can't seem to figure out why it's not working. Thank you all in advance for reading through this (if you are still reading).

--dom

Replies are listed 'Best First'.
Re: My module's methods aren't working under Mason pages?
by duyet (Friar) on Aug 10, 2011 at 06:29 UTC

    I put your code in a small script and executed it without problem. I think your Mason code is picking up a same module somewhere in your path and gets the error.

    You can try one of the followings:

    • rename your module CMS/Users.pm to something else eg. CMS/MyUsers.pm and use it in your Mason code
    • Dump the @INC from your Mason code and go thru all paths to see if there is somewhere another CMS/Users.pm

      Dump the @INC from your Mason code and go thru all paths to see if there is somewhere another CMS/Users.pm

      Or dump %INC instead :)

        @INC and %INC are 2 different things :)

        print Dumper( \@INC ); $VAR1 = [ '/usr/lib/perl5/5.10/i686-cygwin', '/usr/lib/perl5/5.10', '/usr/lib/perl5/site_perl/5.10/i686-cygwin', '/usr/lib/perl5/site_perl/5.10', '/usr/lib/perl5/vendor_perl/5.10/i686-cygwin', '/usr/lib/perl5/vendor_perl/5.10', '/usr/lib/perl5/vendor_perl/5.10', '/usr/lib/perl5/site_perl/5.8', '/usr/lib/perl5/vendor_perl/5.8', '.' ]; print Dumper( \%INC );' $VAR1 = { 'warnings/register.pm' => '/usr/lib/perl5/5.10/warnings/regi +ster.pm', 'bytes.pm' => '/usr/lib/perl5/5.10/bytes.pm', 'XSLoader.pm' => '/usr/lib/perl5/5.10/i686-cygwin/XSLoader.p +m', 'Carp.pm' => '/usr/lib/perl5/5.10/Carp.pm', 'Exporter.pm' => '/usr/lib/perl5/5.10/Exporter.pm', 'warnings.pm' => '/usr/lib/perl5/5.10/warnings.pm', 'overload.pm' => '/usr/lib/perl5/5.10/overload.pm', 'Data/Dumper.pm' => '/usr/lib/perl5/5.10/i686-cygwin/Data/Du +mper.pm' };

        If you need to check your paths @INC is a better option :)

      Thanks for the help! Renaming it seems to have done the trick for fixing the error. I've got no idea where there is another CMS/Users.pm though so that's a little weird. But hey. It works and that's what counts. Thanks again!

        I've got no idea where there is another CMS/Users.pm though so that's a little weird.

        %INC tells you which .pm was loaded , and where it was loaded from