So here I am humming a little tune, about how awesome DBIx::AnyDBD is and loading up a bunch of SQL in the __DATA__ sections of each of my modules, when I came across an anomaly: Perl supports polymorphic subroutines but uses the data section of the class where the method is found, not the data section of the class that the search for the method started in... This is problematic because you want to put the data for each module in the __DATA__ section of that module and have the one "base" subroutine use the __DATA__ sections of the appropriate subclass.
package One; sub data { print $_ while <DATA>; } 1; __DATA__ here we have the data section of One.pm package Two; our @ISA = qw(One); sub new { my $self = {}; bless $self, __PACKAGE__; } 1; __DATA__ here we have the data section of Two.pm # and a script to use them: use lib '.'; use One; use Two; my $two = Two->new; $two->data; ### and the output: $ perl script.pl here we have the data section of One.pm

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.

Replies are listed 'Best First'.
Re: odd: __DATA__ sections are not polymorphic
by Aristotle (Chancellor) on Oct 04, 2003 at 18:35 UTC

    DATA is a package variable. Perl can't know you want to do what you are doing. Actually in most cases other than yours one probably wants the current behaviour.

    You can solve your problem using something like *{ref($self).'::DATA'} to get the handle you want. I would instead consider changing the design, however.

    Makeshifts last the longest.

Re: odd: __DATA__ sections are not polymorphic
by Zaxo (Archbishop) on Oct 05, 2003 at 05:16 UTC

    The __DATA__ section in modules is always the most recently seen one, so that of a derived class overrides that of the parent.

    Another gotcha associated with the DATA handle is that one associated with the __END__ tag is always part of package main::.

    After Compline,
    Zaxo