in reply to Generic Composite Interface

merlyn is right. Personally, while maybe not the most elegant way to do so, having to include a new use statement for each new module has a self-documenting quality to it. But it is indeed error prone.

What you could do is scan a particular directory for .pm files in a BEGIN block like so:

use strict; use Data::Dumper; BEGIN { # scan the current directory eval{require $_} for <*.pm>; }; my @class = ( Foo->new, Bar->new, Baz->new, ); print Dumper $_ for @class;
This works (assuming that the corresponding modules exist and are free of compilation errors) , but without precautions it will really be more error prone than just using the modules explicitly. Neat stuff though .... Perl rules! :)

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)

Replies are listed 'Best First'.
Re: (jeffa) Re: Generic Composite Interface
by chhe (Sexton) on Mar 23, 2002 at 19:40 UTC

    I think that's what i am looking for. So the:

    BEGIN { # scan the current directory eval{require $_} for <*.pm>; };

    substitutes the "use" declarations?

    I absolutely agree, this should'nt the recommended or normal style to add new functionality, but i context of my application it certainly helps.

    Chris

      jeffa

      From 'perldoc -f use':
      Imports some semantics into the current package
      from the named module, generally by aliasing
      certain subroutine or variable names into your
      package.  It is exactly equivalent to
      
         BEGIN { require Module; import Module LIST; }
      
      except that Module must be a bareword.
      
      Turns out that evaling the way i did is not very useful, better to stick with the traditional for loop syntax like so to take advantage of 'exceptions':
      BEGIN { for (<*.pm>) { eval {require $_}; warn $@ if $@; # but it's prolly going to die anyway! } };
      So you can more easily find which module(s) and what line(s) caused the error(s). Also, defining a path to the directory that contains the modules would be a good thing to do. Good luck! :)
      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)