Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^5: Objects and passing/calling methods

by AnomalousMonk (Archbishop)
on Sep 22, 2018 at 23:13 UTC ( [id://1222859]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Objects and passing/calling methods
in thread Objects and passing/calling methods

You can (usually) easily tell what class an object is blessed into by using one of many fine dumpers; I like Data::Dump, but it's not core; Data::Dumper is core.

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.014; ;; package Foo { sub defaults { return { 'default' => 'zing' }; } sub new { my $class = shift; my ($hr_args) = @_; my $obj = { %{ $class->defaults }, %{ $hr_args || {} } }; return bless $obj => $class; } sub show { my $self = shift; print qq{@{[ %$self ]} @_}; } } ;; package Bar { use parent -norequire, qw(Foo); sub show { my $self = shift; $self->SUPER::show('from ' . __PACKAGE__, @_); } } ;; my $b = Bar->new({ 'hi' => 'there' }); $b->show('zoot'); ;; use Data::Dump qw(dd); ;; dd $b; " hi there default zing from Bar zoot bless({ default => "zing", hi => "there" }, "Bar")

Update: Per kevbot's comment:   Addition to code posted above:

... print ref $b;
And it's output:
Bar


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^6: Objects and passing/calling methods
by kevbot (Vicar) on Sep 23, 2018 at 00:03 UTC

    One can also determine the class using the ref function.

    If the referenced object has been blessed into a package, then that package name is returned instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1222859]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found