Loading and using modules using eval

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Loading and using modules using eval

    Hi all,

    I have this piece of code:

    my @modules = (
    'Module1', 'Module2', 'Module3',
    'ModuleX', 'ModuleY', 'Blah',
    );
    my $param1 = "whatever";
    my $param2 = 0;
    for my $module (@modules) {
    $param2++;
    eval("require $module");
    if ($@) {
    print "$module Not found\n";
    } else {
    eval("&${module}::process('$param1', '$param2')");
    }
    }

    And it's working, but I wonder if there's any cleaner/better way to do this.
    The main idea is to have a XML file like this:

    <xml>
    <file source="file1.txt" module="TextFile" parameters="1"/>
    <file source="file2.csv" module="TextFile" parameters="2"/>
    <file source="file3.xml" module="XMLFile" parameters="this and that"/>
    </xml>

    To tell me I have to do:

    &TextFile::process('file1.txt', '1');
    &TextFile::process('file2.csv', '2');
    &XMLFile::process('file3.xml', 'this and that');

    Got it? ;-)

    Thanks!

    Papo Napolitano Guest

  2. Similar Questions and Discussions

    1. Loading Perl Modules from same directory as script
      Well, after only 6 hours of hacking around trying to do something that should be obvious in perl - but isn't - i seem to have found two solutions :...
    2. Loading Modules - Yipee!!
      Owen. If you were standing next to me I'd give you a big kiss (no I'm not really like that, just a figure of speech) It worked, how, why I'm still...
    3. Loading Modules - More
      On Tue, 23 Dec 2003 09:53:09 +1300 Support <support@emlgroup.co.nz> wrote: See my previous message, you need to make a a directory "Date" in...
    4. Loading Modules
      Hi All I have a perl module I want to use in a perl script and make available by the ' use mymodule.pm' call. Plus I would like to place it the...
    5. eval'
      I am retrieving the following string from the database. " and upper(gsa_template_data_header) >= '".$start_selection."' and...
  3. #2

    Default Re: Loading and using modules using eval


    On Jan 23, 2004, at 1:23 PM, Papo Napolitano wrote:
    [..]
    > <xml>
    > <file source="file1.txt" module="TextFile" parameters="1"/>
    > <file source="file2.csv" module="TextFile" parameters="2"/>
    > <file source="file3.xml" module="XMLFile" parameters="this and
    > that"/>
    > </xml>
    >
    > To tell me I have to do:
    >
    > &TextFile::process('file1.txt', '1');
    > &TextFile::process('file2.csv', '2');
    > &XMLFile::process('file3.xml', 'this and that');
    [..]

    Why not try something a bit more vanilla
    where one does the

    use SomeModuleHere;

    for all the modules you want to use. Then you
    can use the no strict refs option IF you
    really want to do the strictly functional approach.

    I do not think that

    eval("&${module}::process('$param1', '$param2')");

    will do what you want it to do.

    IF the Text::process and XML::process functions are things
    that you are building out you may want to think about the
    idea of doing the perl oo-ish aproach, as

    $foo->showMe($line);

    will work without requiring the no strict refs.

    ciao
    drieux

    ---

    Some code to play around with would be:

    #!/usr/bin/perl -w
    use strict;
    my ($foo, $line) ;
    while(<DATA>){
    chomp;
    /^([\w:]+)\s+(.*)/;
    ($foo, $line) = ($1,$2);
    # this assumes that the Package is External
    # require "$foo.pm" if (!exists($INC{"$foo.pm"}));
    # the way that will work by indirection
    #$foo->showMe($line);
    my $code = "${foo}::showMe";
    no strict 'refs';
    $code->($line);
    # does not invoke the code
    #eval{ &${foo}::showMe($line) };
    if ($@)
    {
    print "Error: had \$foo -> $foo\n\t\$line -> $line\n$@\n#-----\n";
    }

    }
    print "and now a Procecural call:\n" ;

    Foo::showMe("Procedural\n$line\n");

    BEGIN {
    package Foo::Bar;

    #------------------------
    #
    sub showMe
    {
    #my $me = shift if ( $_[0] eq __PACKAGE__);
    my ($line) = @_;
    print $line;
    print "\n" unless $line =~ /\n/gim;

    } # end of showMe

    1;

    package Foo;

    #------------------------
    #
    sub showMe
    {
    #my $me = shift if ( $_[0] eq __PACKAGE__ or
    # ref($_[0]) eq __PACKAGE__ );
    my ($line) = @_;
    print "foo sees:\n\t $line\n";

    } # end of showMe


    1; # so that the 'use Foo::Bar'
    # will know we are happy
    } # end begin

    __DATA__
    Foo word up
    Foo::Bar not that one.
    Foo This is a Happy Line

    Drieux Guest

  4. #3

    Default Re: Loading and using modules using eval (SOLVED)

    ----- Original Message -----
    From: "drieux" <drieux@wetware.com>
    To: "Perl Beginners Mailing List" <beginners@perl.org>
    Sent: Friday, January 23, 2004 19:53
    Subject: Re: Loading and using modules using eval

    > IF the Text::process and XML::process functions are things
    > that you are building out you may want to think about the
    > idea of doing the perl oo-ish aproach, as
    >
    > $foo->showMe($line);
    >
    > will work without requiring the no strict refs.
    >
    > ciao
    > drieux
    Your method is much better, easier on the eyes and working flawlessly ;-)
    Thanks!

    Papo Napolitano Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139