driador has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Objects and passing/calling methods
by choroba (Cardinal) on Sep 22, 2018 at 20:19 UTC | |
by driador (Novice) on Sep 22, 2018 at 20:25 UTC | |
by choroba (Cardinal) on Sep 22, 2018 at 20:26 UTC | |
by driador (Novice) on Sep 22, 2018 at 20:37 UTC | |
by AnomalousMonk (Archbishop) on Sep 22, 2018 at 23:13 UTC | |
| |
by haukex (Archbishop) on Sep 24, 2018 at 20:37 UTC |