This modified code demonstrates package inlining and inheritance. For small scripts, I like to inline a package once in a while, escpecially when subclassing a standard module. if I put the main package before the other packages, it doesn't work.
#!/usr/bin/perl
use warnings;
no warnings qw/uninitialized/;
use strict;
use Data::Dumper;
#######################
package Class1;
sub new {
my $classname = shift;
my $self = {@_};
$self->{NAME} = undef unless $self->{NAME};
$self->{AGE} = undef unless $self->{AGE};
bless($self,$classname);
return $self;
}
sub PrintHello
{
my $this = shift;
print "$this->{TITLE}\tHello $this->{NAME}\t$this->{AGE}\n";;
}
#######################
package Class2;
our @ISA = qw(Class1);
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{TITLE} = 'This is Class2';
return $self;
}
########################
package main;
my $max = Class1->new(NAME=>'Someone Else',AGE=>42);
my $max2 = Class2->new(NAME => 'MaxKlokan',AGE=>21) ;
$max2->PrintHello();
print Dumper $max;
print Dumper $max2;
exit;
-
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.
|