Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Inheritance in the same file

by stephen (Priest)
on Jun 11, 2013 at 17:19 UTC ( [id://1038292]=note: print w/replies, xml ) Need Help??


in reply to Re: Inheritance in the same file
in thread Inheritance in the same file

Alternatively, you can wrap the class code in a BEGIN block. That will ensure that your classes are initialized before the main code gets run. You don't need "require" or '1;' when the code is in the same file. Also, 'use strict' is lexically scoped, so you really only need to say it at the top.

#!/usr/bin/perl use strict; use warnings; my $a = B->new(); BEGIN { package A; sub new { my ( $class, %args ) = @_; my $self = bless {}, ref($class) || $class; return $self; } package B; @B::ISA = qw(A); sub _init { my ( $self, %args ) = @_; $self->{_title} = $args{title} || undef; } }

stephen

Replies are listed 'Best First'.
Re^3: Inheritance in the same file
by yoda54 (Monk) on Jun 12, 2013 at 08:04 UTC
    Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1038292]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found