Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling an undefined subroutine in OOP?

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
0
0
US
Hi, Experts,

I inherited a piece of codes that I can hardly understand.

The line of codes in question is quite simple:

Code:
my $statistics = Sys::Stat::IO->new();

However, the source code file IO.pm which is under ./Sys/Stat does not define the 'new()' subroutine. How could this be possible? Or did I miss something really simple?

In addition, is there a way to find out what values/keys/hashs/arrays contained in the obj '$statistics'?

Thanks,
 
Did you look upward in case new is inherited?
Is there an ISA at the top of IO.pm?


also, using command line grep new *.pm or something like that can be helpful if there are many files.

I often use grep like that to find a subtle mistake in some html file that only shows a vague log warning
 
Thank you, MrCBofBCinTX. And yes, it is inherited.

Now I have a somewhat related question. Is there a way to display an object's properties? For instance, after an obj is instantiated:

Code:
my $statistics = Sys::Stat::IO->new();

Is it possible that I can loop through the obj '$statistics' (like we loop through an array or a hash) to see what data members it has?
 
Most objects are hashes, but some are arrays or functions or even scalars. Depending on the programming style used, there may be everything clearly laid out, or there might be methods that are created as used. Which is a nice property of OOP that requires you to spin your head around a full 360!

If the code is fairly short and not obvious, then you will have to think your way through it.

OOP can produce very short code that kind of creates itself upon use. Wonderful and horrible at same time. :)


Your best bet is to understand what the code accomplishes so you can look for how and then be able to pull out what $statistics contains.

But yes, you can loop through its contents
 
Thank you, MrCBofBCinTX.

But how to loop through an obj?

Take the piece of codes below as an example:
Code:
my $obj = MyClass->new();
print "\$obj = #$obj#\n";

The output would be:
Code:
$obj = #MyClass=HASH(0x2db90b4)#

So, could you or someone else show me the codes that can loop through $obj?

Thanks.
 
Ok, I got it.

Actually, it is quite simple. It's exactly the same as looping through a hashref.

Thanks all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top