in reply to OO and <DATA>

However, as would be obvious to people with more OO experience than I have, when the output sub in a Resume::Text object calls apply_stylesheet (which is defined in the parent Resume package, rather in the child Resume::Text package), the the sub tries to read the data in the __DATA__ section of Resume.pm, not the one in Resume/Text.pm.

You can deal with this problem by further isolating your use of the __DATA__ section behind a method that does a one-time, lazy setup from <DATA>. This turns   my $doc = $self->{'parser'}->parse_string(<DATA>); into

my $stylesheet = $self->get_stylesheet(); my $doc = $self->{'parser'}->parse_string($stylesheet);

This also frees you to override get_stylesheet() in subclasses. How and where get_stylesheet() gets the stylesheet is an implementation detail. A subclass needn't use the __DATA__ section.

Replies are listed 'Best First'.
Re: Re: OO and <DATA>
by trs80 (Priest) on Aug 25, 2002 at 16:05 UTC
    dws is correct, but I don't know if the __DATA__ concern is clear enough for newer users. __DATA__ is only read ONCE during the execution of a script, any secondary calls to it will result in an empty string.

    I have found it helpful to use something like Text::Template to store information that will be read in multiple times and if setup correctly will allow for edits while the process is running. I like Text::Template for its simplicity, but there are dozens of templating options that might help here.

      Does seek data,0; not work?
        Yes, but don't forget to use tell first, lest you spit out the entire program via seek DATA,0,0 :
        use strict; my $pointer = tell DATA; print get_data(); seek DATA,$pointer,0; print get_data(); sub get_data { do {local $/;<DATA>} }

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)