Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: inheritance and __DATA__

by bbfu (Curate)
on Apr 13, 2004 at 01:08 UTC ( #344580=note: print w/ replies, xml ) Need Help??


in reply to inheritance and __DATA__

You need to fully qualify the package in which the DATA section you wish to access resides. One way to do so is given below in the flux sub.

Update: Summary (click readmore for full test code and output):

sub flux { my $class = ref shift; my $fh = do { no strict 'refs'; \*{"$class\::DATA"}; }; return "A-flux: " . scalar <$fh>; }

344569.pl

#!/usr/bin/perl use warnings; use strict; use a344569; use b344569; my $a = a344569->new(); my $b = b344569->new(); print "A:\n"; print " ", $a->foo(); print " ", $a->flux(); print "B:\n"; print " ", $b->foo(); print " ", $b->bar(); print " ", $b->flux(); print "Ok\n";

a344569.pm

package a344569; use strict; sub new { return bless {}, shift; } sub foo { return "A-foo: " . scalar <DATA>; } sub flux { my $class = ref shift; my $fh = do { no strict 'refs'; \*{"$class\::DATA"}; }; return "A-flux: " . scalar <$fh>; } 1; __DATA__ This is package A This is package A This is package A This is package A

b344569.pm

package b344569; @ISA = ('a344569'); use strict; sub bar { return "B-bar: " . scalar <DATA>; } 1; __DATA__ This is package B This is package B This is package B This is package B

Produces:

[johnsca@cory tmp]$ ./344569.pl A: A-foo: This is package A A-flux: This is package A B: A-foo: This is package A B-bar: This is package B A-flux: This is package B Ok

bbfu
Black flowers blossom
Fearless on my breath


Comment on Re: inheritance and __DATA__
Select or Download Code
Re: Re: inheritance and __DATA__
by thpfft (Chaplain) on Apr 13, 2004 at 09:49 UTC

    Thank you. Worked very nicely.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-08-23 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (174 votes), past polls