Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Lost methods

by Dave05 (Beadle)
on Nov 26, 2002 at 11:51 UTC ( #215824=perlquestion: print w/ replies, xml ) Need Help??
Dave05 has asked for the wisdom of the Perl Monks concerning the following question:

OK, I'm on day 3 of trying to track down this one, so I'm turning to the Monks for some pointers.

This thing is a web site, running under HTML::Mason. I have a MyApp::Person object representing a logged in user. I can happily use the object to run through all the registered users in my database, and call methods to print out a list of names for instance.

But when I have a specific user logged in, and identified via a cookie, I get this error when trying to call methods off the user stored in the cookie (the cookie stores an ID string, which identifies a file on the server that holds the user data which is used to initialise the $user object):

Can't locate object method "full_name" via package "MyApp::Person" (perhaps you forgot to load "MyApp::Person"?)

Here's the thing. If I check %INC, MyAPP::Person _is_ loaded.
Also: ref($user) returns MyApp::Person
The next thing: if I run a $user->can('full_name'), it can't.
Next: in fact, my $user can't do _any_ of the MyApp::Person methods.
BUT: if I stick a dummy variable in MyApp/ (our $dummy_true = 1;), then I _can_ successfully read the dummy variable (via $MyApp::Person::dummy_true).

So basically, I can access the MyApp::Person namespace, but I can't use $user to get hold of any methods defined in there, despite ref($user) == MyApp::Person

One specific question I'd like to ask is: how can I check the package my code is currently running under?

But what I'd really like is suggestions as to where to look next.


Comment on Lost methods
Select or Download Code
Re: Lost methods
by Abigail-II (Bishop) on Nov 26, 2002 at 12:20 UTC
    The answer to your specific question is: use __PACKAGE__. However, I'm not sure whether that will help you fixing your problem. Perhaps you show us the relevant code.


Re: Lost methods
by fruiture (Curate) on Nov 26, 2002 at 12:20 UTC

    Okay, although i have no idea what your problem really is, some diagnostics.

    The package you're in is in __PACKAGE__, but don't use it, create a function like "debug_whereami()" and let that function use caller(). Or easier: use Carp::croak(), it will give you all information.

    Here's the thing. If I check %INC, MyAPP::Person _is_ loaded.

    Wasn't it 'MyApp::Person'? In doubt, put a 'use MyApp::Person' in every file you need it. It's redundant, but Perl is a good language and require() will check %INC for you. Don't do this on your own, let perl do it.

    Now maybe this is really a bug, what does `perl -V` give you? Perhaps it's better to upgrade anyways...

Re: Lost methods
by mce (Curate) on Nov 26, 2002 at 12:25 UTC

    The info you gave is rather limited, so I'll try to point you in the correct direction. I guess this is a base or ISA problem in your inheritance tree. Please check the

    use base qw(Parent);
    Where Parent refers the the class (package) where the sub full_name is stored.

    use base uses and changes ISA a the same time.
    I hope this helps
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

Re: Lost methods
by Chmrr (Vicar) on Nov 26, 2002 at 12:32 UTC

    As Abigail-II already pointed out, __PACKAGE__ will tell you what package you're in. The only other, off-the-wall thought that I have is that your object reference is bless'd into class "MyApp::Person\0" somehow. This would explain why, for all intents and purposes, it ref $user says "MyApp::Person", but is unable to do anything that a MyApp::Person should be able to do. It's farfetched, but it's a possibility.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Lost methods
by Dave05 (Beadle) on Nov 26, 2002 at 15:04 UTC
    perl -V says:

    Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration:
    Platform: osname=solaris, osvers=2.8, archname=sun4-solaris

    I think 5.6.1. is OK?

    The code is a small component in my HTML:Mason site. Basically, it's a Perl subroutine embedded in an html page. Mason components run in the HTML::Mason::Commands namespace. This component should just spit out the user's name. Errors are caught by Mason and formatted as html, hence the <br />'s:

    use MyApp::Person; my $user = $session->param('user'); # CGI::Session my $name; eval{$name = $user->full_name} if $user; if ($@) { my $dump_user = Data::Dumper->Dump([$user], ['$user']); $dump_user =~ s|,|,<br />|g; my $package = __PACKAGE__; my @methods = qw( ... all the methods from MyApp::Person ... ); my $check_methods; foreach my $method (@methods) { $check_methods .= sprintf "%s %s <br />", $user->can($method) +? 'CAN' : 'CANNOT', $method; } my $dummy = $MyApp::Person::dummy_true ? $MyApp::Person::dummy_tru +e : 'FALSE'; my $loaded_modules; foreach my $mod (sort values %INC) { $loaded_modules .= "$mod<br />"; } my $ref_user = ref($user); die "Current package: $package<br /> Dummy variable: $dummy<br /> \$user is a $ref_user<br /> $check_methods<br /> \@INC:<br /> $loaded_modules<br /> $dump_user<br />"; } else { [... do something with $name ...] }

    The MyAPP was a typo ...

    All the things in the die statement check out - except all the methods come out CANNOT:

    Current package: HTML::Mason::Commands Dummy variable: 1 $user is a Icapb::Person CANNOT new CANNOT _init CANNOT _check_user_type CANNOT _load_cv CANNOT ... etc etc etc @INC: ... /home/dave05/lib/perl5/mylib/MyApp/ ...

    and $user is initialised with all the appropriate data.

    I like Chmrr's suggestion. I develop in Win32, and run this thing on solaris, so maybe a bad line/file ending got in there somewhere, I'll check that out.

    I'm not sure how use base can help me? My Person object isn't derived from anything else.

    Thanks all, progress report soon!

      Well, according to your own code, $user is an instance of the Icapb::Person class. So, unless Icapb::Person inherits MyApp::Person, I'm not at all surprised you can't call those methods.


        Sorry, that's another typo, I've been changing the names to protect the innocent, and that slipped through.
Re: Lost methods
by Dave05 (Beadle) on Nov 26, 2002 at 15:16 UTC
    ++Chmrr! I haven't figured out yet where to look in the code for a \0 sneaking in, but I did this:
    my $user = $session->param('user'); eval(Data::Dumper->Dump([$user], ['$user']));

    and $user started working! So it looks like there's something irregular getting tagged onto the end of the blessing, but only when the code is retrieved from the session.

    Well, I don't feel quite so bad that I couldn't track that one down!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://215824]
Approved by djantzen
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-20 08:26 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (157 votes), past polls