Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Object method in wrong namespace

by Bod (Parson)
on Feb 27, 2022 at 20:27 UTC ( [id://11141684]=perlquestion: print w/replies, xml ) Need Help??

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

Wise Monks,

I have been scratching my head for too long here...it is time to seek your wisdom. I suspect the problem is simple and sufficiently obvious that I shall kick myself when it is revealed!

I am developing a module for internal use that manipulates blog posts. It takes a hashref from a database, manipulates the data and can return either another hashref or any of the individual pieces of data. But it's not working so I have added some debug code. It is the behaviours of this debug code that I cannot understand.

my $query = $dbh->prepare("SELECT * FROM Blog ORDER BY created DESC"); $query->execute; my $test; while( my $bg = $query->fetchrow_hashref ) { my $blog = Bod::Blog->new($bg) or die "Blog not defined"; $test = $blog->heading; # line 152 push @blogs, $blog->hashref; }
I am getting this error:
Can't locate object method "heading" via package "main" at admin.pl line 152.

The heading method should be in the Bod::Blog namespace but the error suggests that Perl is looking for it in the main namespace. I cannot figure out why that might be!

Here is the first part of Blog.pm...

package Bod::Blog; use strict; use warnings; sub new { my ($class, $blog, $vars); $vars->{'heading_class'} ||= 'blogHead'; $vars->{'subheading_class'} ||= 'blogSubHead'; $vars->{'body_class'} ||= 'blogBody'; $vars->{'base_path'} ||= '/blog/'; $vars->{'image_path'} ||= '/images/blog/'; my $self = bless { 'blog' => $blog, 'vars' => $vars, }, $class; return $self; } sub heading { my $self = shift; return $self->{'heading'}; }

Replies are listed 'Best First'.
Re: Object method in wrong namespace
by LanX (Saint) on Feb 27, 2022 at 20:42 UTC
    maybe try

    my ($class, $blog, $vars) = @_;

    instead of

    > my ($class, $blog, $vars);

    :)

    edit

    PS: shouldn't

    return $self->{'heading'};

    rather be

    return $self->{vars}{'heading'};

    ?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      my ($class, $blog, $vars) = @_;

      Like I said...something worthy of kicking myself!!!
      Kicking done!

      Thank you...

        > Like I said...something worthy of kicking myself!!!

        I can't say this never happened to me, too! 🤷🏽

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11141684]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found