Jaap has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

My program uses a SAX handler like this:
use XML::SAX; use MySAXHandler; my $handler = new MySAXHandler; my $parser = XML::SAX::ParserFactory->parser(Handler => $handler); $parser->parse_string($xmlSource);
Now i need to access some of the data that the MySAXHandler has stored in it's $self->{'nodes'} from my main program.

I can think of 3 ways to do this:
1. Access $parser->{'nodes'} directly
2. Make some getThis and getThat functions in MySAXHandler
3. Export a datastructure from MySAXHandler to the main program.

Speed is important here but i would like to do it The Right Way™. What is the best way to get to that data from my main program?

Replies are listed 'Best First'.
Re: [OO] export data from Module
by Old_Gray_Bear (Bishop) on Apr 29, 2004 at 16:21 UTC
    I'd write the get_this() and get_that() methods. It provides data isolation (you have control over what is 'exported') and documentation.

    I tend to view Modules (Objects) as three headed thingies:

    1. Housekeeping -- new(), DESTROY(), _init(), etc
    2. Low level manipulation -- parse(), get_xxx(), set_xxx()
    3. API -- all of the other accessor methods that extract portions of the Object data, massage it a bit, and send it off to the End Consumer.
    The API is what really documents your Object, between the methods supplied and the POD. An additional benefit is that as you develop the API, additional things will occur to you ('I can do this as well if I only....'; Increased Functionality -- that's what I tell my boss). Unless you do something really radical using AUTOLOAD, the 'speed hit' for an additional accessor routine will be almost unmeasurable.

    There is some penalty at load time (use/require MyModule), but that is a one-time cost, amortized over the life span of the Module Instance. It's relatively big for batch programming -- say 1-2% of the run time, each time; but it's negligable for long running apps (read a mod_perl handler or its ilk). (This is the "I don't care that it takes fifteen more seconds at Apache start-up, the last time we re-initialized Apache was four MONTHS ago, and every third request uses that API!" argument.)

    On balance, I'd go with the accessors.

    ----
    I Go Back to Sleep, Now.

    OGB

      Thanks chromatic and Old_Gray_Bear for the replies.
      It's unanimous, i'm going the getThis() and getThat() route.
Re: [OO] export data from Module
by chromatic (Archbishop) on Apr 29, 2004 at 16:12 UTC

    #2. Encapsulate internal data behind a stable and pleasant API.