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;

In reply to Re^2: Defining classes and inheritance using packages within a single .pl file, without creating modules by alw
in thread Defining classes and inheritance using packages within a single .pl file, without creating modules by MaxKlokan

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.