in reply to Generic Composite Interface

for each new modul i want to use through the composite interface i include a new use statement.
I don't know why you find that surprising. A "use" is a declaration by you that this file needs more functionality. Certainly, you can hide some of that with autoloads and such, but why not be explicit?

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: ?Re: Generic Composite Interface
by chhe (Sexton) on Mar 23, 2002 at 19:30 UTC
    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
      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