Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Another problem I have encountered whilst creating my set of modules. Say I have my set of modules that are named as follows:

Module::Main
Module::Section1
Module::Section2

Now, let's say that I have the following code in index.pl:

#!/usr/bin/perl -w use strict; use Module::Main; my $REQ = new Module::Main; $REQ->init(); $REQ->start_it();
This is the start of it. Now, a couple of other modules are added to the mix...

Module::Main contains something like the following:

package Module::Main; sub new { my $class = shift; my $self = {}; bless ($self, $class); return $self; } sub init { my $self = shift; $self->{SEC1} = new Module::Section1; $self->{SEC2} = new Module::Section2; return 1; } sub start_it { my $self = shift; $self->{SEC1}->do_something(); return 1; }
And now, let's say that Module::Section1 contains something like this:

package Module::Section1; sub new { my $class = shift; my $self = {}; bless ($self, $class); return $self; } sub do_something { my $self = shift; =cut HERE! Now I need to access $REQ->{SEC2}->method(); The problem? $self now contains the object specific to Module::Section1 (ie: $REQ->{SEC1}, not to $REQ. So I can't do $self->{SEC2}->method(). I could always change the my() declaration in index.pl to an our() and then use $::REQ->{SEC2}->method(), but this just defeats the purpose of using OO programming! Besides, this would require that the variable be named $REQ and not anything else. I could always pass a reference to $REQ to everything subroutine/method, but that just seems extremely messy and bad programming style. So how can I accomplish this? =cut }

In reply to Accessing Methods of Another Package by mt2k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-19 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found