Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: ?Re: Generic Composite Interface

by chhe (Sexton)
on Mar 23, 2002 at 19:30 UTC ( [id://153826]=note: print w/replies, xml ) Need Help??


in reply to •Re: Generic Composite Interface
in thread Generic Composite Interface

Agreed, maybe i was'nt precise enough i my statement:

I do not find the fact that i include a "use" declaration for new functionality suprising, this is i what i normally expect and want.

Also i maybe did'nt provide enough information about the the context of the "application" i was writing: a "little" monitoring daemon. The function,which instanciates the objects, does this by reading from file and instanciates them by parsing the input from the file, something like the following:

sub load { my ($self, $filename) = @_; open (LOGGERS, "< " . $filename); my ($line); while ($line = <LOGGERS>) { chomp $line; if (!$line) { last;} my @parms = split /:/, $line; my $loggertyp = shift @parms; my $logger = ${loggertyp}->new(); $logger->systemname($self->systemname); $logger->subject($self->subject); while (@parms) { my $parmtyp = shift @parms; my $parmvalue = shift @parms; ${logger}->${parmtyp}($parmvalue); } $self->loggers($logger); } close(LOGGERS); }

It actually knows nothing about "functionality" it makes available.

In this context it would be a great thing, if i just could write a new perl module, change the file and the tell the daemon through some signal, to newly load the file and the "daemon" executes new functionality,granteed the whole thing is well tested etc. With the "use" declarations, i additionally have to stop the "daemon" and change the source code of this module. So that's the reason, that in this context, i would prefer not be be explicit.

Chris

Replies are listed 'Best First'.
•Re: Re: ?Re: Generic Composite Interface
by merlyn (Sage) on Mar 23, 2002 at 20:24 UTC
    This is your problem here:
    my $logger = ${loggertyp}->new();
    If you're gonna call new, you're gonna have to ensure that the package is loaded. See how the LWP moudules do it for the various protocol handlers. It's a mere matter of coding up a proper require dynamically. No big deal, but you have to do it, true.

    Or, you can add something like the following subroutine (untested):

    sub UNIVERSAL::AUTOLOAD { my ($pack, $meth) = $AUTOLOAD =~ /(.*)::(.*)/; die "do you really want me to pull in $pack for $meth?\n" unless $me +th eq "new"; eval "require $pack"; die $@ if $@; die "$pack didn't define $meth" unless defined &$AUTOLOAD; goto &$AUTOLOAD; }

    -- Randal L. Schwartz, Perl hacker

      Thanx, i think got it:

      With UNIVERSAL::AUTOLOAD one can intercept calls to a perl object, which have failed

      With "require" one can dynamically "use" modules, depending for example on some input, like in my case reading the Objectname from the input

      Really neat stuff, but i agree that most application would'nt want or need to use this stuff...

      I wonder, how perl compares here with other languages....

      But anyway, thanx for the help

      Chris
        Really neat stuff, but i agree that most application would'nt want or need to use this stuff...
        I wonder, how perl compares here with other languages....
        In what way are you comparing Perl with other languages? In that most applications won't need dynamic dispatch, dynamic code loading, huge amounts of introspection?

        Or in Perl's need to use such tricks? Or that Perl can in fact do such tricks? Or that Perl has a different sort of ratio of programs that need such tricks to those that don't? And would that be less, or more?

        Please narrow the boundaries of your desire for comparison. {grin}

        -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found