Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Understanding a OOp code

by sachinz2 (Acolyte)
on Sep 26, 2016 at 08:44 UTC ( [id://1172609]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I am working on some OO perl script, which was written by some other user, also, I am new to OO perl, There is this line of code Which I am not able to understand

Please if some one can shed some light , It will be a help, below is the code

sub LicenseData{ my ($lic) = @_; my $hash = LIC_INFO($lic); return $hash->LicenseData(); }

Here LIC_INFO is another sub-routine which returns some data, so.. $hash has some Info in it(I checked with 'print Dumper $hash'). But I am not able to understand what '$hash->LicenseData()' does, and when I did 'print Dumper $hash->LicenseData()' it is giving "undef"

Replies are listed 'Best First'.
Re: Understanding a OOp code
by choroba (Cardinal) on Sep 26, 2016 at 08:58 UTC
    That's the way to call a method on an object (or a class) in Perl. $hash is a bad name, as it suggests it contains a hash reference , but in fact, it contains an object, otherwise you wouldn't be able to call a method on it. You can find the method's definition in the package that defines its package (run ref $hash to get the package name), or its parent, or its parent's parent, etc.

    When run, the method gets the object as its first argument (or, in case of a class method, the class name).

    BTW, if the method takes no parameters, the parentheses after the method's name are optional.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Understanding a OOp code
by GrandFather (Saint) on Sep 26, 2016 at 08:58 UTC

    $hash is an object. $hash->LicenseData(); calls the "method" LicenseData on the hash object. Without bothering with details, that means a sub named LicenseData is called and passed $hash as the first parameter. The LicenseData sub uses the contents of $hash to do some work.

    $hash probably isn't a very good name. If the code were written:

    sub LicenseData{ my ($licCode) = @_; my $licObj = LIC_INFO($licCode); return $licObj->LicenseData(); }

    it may be a little clearer.

    Premature optimization is the root of all job security
      hi

      Thanks for so quick a reply,

      but, please, I am still not able to get it, I get some data with 'my $licObj = LIC_INFO($licCode);'

      but , I do not understand the objective(logic) behind calling the same sub-routine with '$licObj->LicenseData();'

      'sub LicenseData' is the sub-routine where "my licObj is created"

        Hi sachinz2,

        the objective(logic) behind calling the same sub-routine

        It's possible likely there are two subs with the name LicenseData in different packages (classes). Inside the sub, you could try doing print __PACKAGE__, " / ", ref($hash), "\n";, that will print the current package and the package of $hash (more specifically, the package that $hash is blessed into, or in OO terminology the object's class).

        Otherwise, if there's only one sub LicenseData, then this would indeed be a recursive call, and the next call to LicenseData would receive in its $lic parameter the $hash value of the caller. How the recursion ends depends on what LIC_INFO is doing.

        Update: Actually, what I wrote above about there being two (or more) sub LicenseDatas is much more likely, since I don't see a clean way for the recursion to end if there's only one sub LicenseData.

        Hope this helps,
        -- Hauke D

        $licObj->LicenseData(); doesn't call the same sub. It is another example of unfortunate naming in the code. There is an element of truth in:

        There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

        Naming things well is hard. However the big item here is that you aren't up to speed with what OO is about. The key is that $licObj is an object and methods (LicenceData() in this case) do things with the object. Maybe Not quite an OO tutorial will help make it a little clearer. A huge amount has been written about OO, but until the "Ah ha" moment comes it doesn't make much sense and even then it's likely not obvious where the benefit is. Used well it does make sense and there is a benefit.

        Premature optimization is the root of all job security

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found