Would have been nice if you could have named those "little things"
That's a fair point. Here's a modified version of the code doing what I think you might have been trying to do. I'm also realizing I may have misread the question, but I've got momentum now. Might as well paste what I have.
#!/usr/bin/perl
# Use the warnings pragma rather than the -w flag
use warnings;
use strict;
{ package One;
# Define a constructor for One
sub new {
my $class = shift; # Constructors need to know what class they
+'re for
my $self = {};
bless($self, $class); # Tell Perl what $self is being blessed
+into
}
sub test {
my $self = shift; # Is this an object method? Better grab $sel
+f
my $x = pop @_; # I usually shift from the argument list, but
+whatever
# Also, I like to be explicit about what I'm
# popping/shifting from
print "$x\n";
}
}
{
package Two;
use base "One";
# Two::new didn't add anything to One, so I removed it.
sub eg {
my $self = shift; # Is this an object method? Better grab $se
+lf
my $arg = pop @_; # pop() again? Okay, break it out and make
+it explicit.
$self->test($arg); # Is test() an object method? I'll use it a
+s if it was.
}
}
my $obj = new Two();
$obj->eg("hello");
Edit: Incidentally, I think jethro has the solution you're looking for. I've become so used to making everything explicit by spelling out object or module connections that I didn't even think of Exporter.
-
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.
|