Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Objects and passing/calling methods

by driador (Novice)
on Sep 22, 2018 at 20:01 UTC ( [id://1222851]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings all. This humble novice requests your assistance...

Imagine I have a base package (SomePackage.pm) that performs a number of functions against a website with a restful interface. That package is written in a perl OO way, with a new() constructor. Another script (caller.pl) will be the user of this package.

That said, I have a number of other sub modules in the base package: SomePackage::Auth.pm, SomePackage::Client.pm, SomePackage::Tickets.pm, etc.

Auth provides the methods to handle authentication to a given site. Client provides a LWP::UserAgent and any methods around certain functions the website offers. Tickets provides methods to create/modify/manage tickets on the site.

The goal I have here is that in the caller.pl, I instantiate the base package SomePackage->new; That sets up the base as well as setting up internal members to be a client (via SomePackage::Client) and ticket (via SomePackage::Ticket). It also has a method wrapper around SomePackage::Ticket's createTicket method:

package SomePackage; use strict; use warnings; use SomePackage::Auth; use SomePackage::Client; use SomePackage::Ticket; sub new { my $class = shift; my $self = {}; SomePackage::Auth->getCredentials; $self->{client} = SomePackage::Client->new; $self->{ticket} = SomePackage::Ticket->new; bless $self, $class; return $self; } sub createTicket { my $self = shift; my $class = shift; my $project = shift; return $self->{ticket}->createTicket($self, $project); }

Now, in caller.pl, I do something like

my $packObj = SomePackage->new; $packObj->createTicket($project);

On running caller.pl, I get use of unitialized value in subroutine entry. What is the proper way to structure classes/objects such that makes passing internal attributes/objects around painless? Am I going about this wrong way?

TIA
A perl OO newb

Replies are listed 'Best First'.
Re: Objects and passing/calling methods
by choroba (Cardinal) on Sep 22, 2018 at 20:19 UTC
    When you call
    $packObj->createTicket($project);
    the method gets called with only two arguments, $self and $project; there's no class.

    "Unitialized value in subroutine entry" means you're calling an XS sub with undef in place of one of its arguments. We need more details to comment on that.

    ($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,
      Ah, that explains it then. I was under the assumption that self and class are always passed inherently. So if class is not, what is the class of the ticket object? SomePackage or SomePackage::Ticket?
        The one it's been blessed to.

        ($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,
        I was under the assumption that self and class are always passed inherently.

        I can recommend a read of perlobj, which says (among many other useful things):

        When you call a method, the thing on the left side of the arrow is passed as the first argument to the method. That means when we call Critter->new(), the new() method receives the string "Critter" as its first argument. When we call $fred->speak(), the $fred variable is passed as the first argument to speak().

        And BTW, since there was mention of it further down in the thread, having to retrieve the class of one's own object with something like ref would seem like a code smell to me, most things can (and should) be solved with the appropriate OO concepts (encapsulation, inheritance, etc.). But I guess you were asking about the class out of curiosity, not because you need it?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found