Professional Web Applications Themes

Dynamic Execution question - PERL Beginners

Hello all! I am new to Perl and have a question. I have to see about converting my Python code to Perl for the project I am working on and I need to know how to go about doing a very specific part of my code. The flow of the code is as follows: Using an hash (perl term, dict in Python terms), select a string from the hash to get the name of a module to load dynamically. Load the module into the interpreter dynamically Using another string in the same hash, execute a function in the dynamically loaded ...

  1. #1

    Default Dynamic Execution question

    Hello all!

    I am new to Perl and have a question. I have to see about converting my
    Python code to Perl for the project I am working on and I need to know how
    to go about doing a very specific part of my code.

    The flow of the code is as follows:

    Using an hash (perl term, dict in Python terms), select a string from the
    hash to get the name of a module to load dynamically.
    Load the module into the interpreter dynamically
    Using another string in the same hash, execute a function in the dynamically
    loaded interpreter using the value in the string as the name of the
    function, passing in a set of arguments.

    The python code is basicly as follows:
    'entry' is the hash (dict) that contains the module and function names.

    try:
    mod = __import__(entry['module'])
    except ImportError, msg:
    log('Failed to load module %s: %s' %
    (entry['module'],msg))
    sys.exit(0)
    if entry['test'] not in mod.__dict__:
    log('Failed to run test %s in module %s: Does not exist'
    % (entry['test'],entry['module']))
    sys.exit(0)
    while 1:
    try:
    rtn = mod.__dict__[entry['test']](entry)
    except:
    import traceback, StringIO
    r = StringIO.StringIO()
    r.write('<HR>There was a software failure in
    "%s.%s".</HR>' % (entry['module'],entry['test']))
    r.write('Please report this entire message to
    XX<BR>')
    r.write('<PRE>')
    traceback.print_exc(file=r)
    r.write('</PRE>\n')

    Thank you very much for any help you can provide!!

    Lance Ellinghaus
    TWAI Operations Integration/Special Projects
    Work Phone: 214-922-6458
    Work Cell: 972-877-0409
    Nextel: 142*52*5511
    Home Phone: 940-271-1274
    Email: com


    Lance Guest

  2. #2

    Default Re: Dynamic Execution question

    Ellinghaus, Lance wrote: 

    I am Python beginner so if I miss something in the code below be sure to
    ask.....
     

    Ok, are you good with loading your hash?
     

    In general this is a pretty easy thing to do in Perl....
     

    The try/catch idiom is built with 'eval' in Perl. See example below. The
    trickiest part about this is that 'use' must take a bareword, and
    accessing into a hash is not a bareword, which means you have to either
    use the string execution form of 'eval' or switch to a 'require'.

     

    Again we can use 'eval' to do the "running" and reporting of errors... I
    am not entirely sure I understand the '(entry)' in the above Python
    code, sure it isn't wrong just my being a newbie...
     

    For example, you can mess with the values assigned in %hash below to
    cause failures to see the try/catch model work:

    -- UNTESTED --

    #!/usr/local/bin/perl
    use strict;
    use warnings;


    # setup our values
    my %hash = ('module' => 'CGI', 'method' => 'new');


    # attempt to load module, catch failures
    eval "use $hash{'module'};";
    if ($) {
    die "Failed to load module $hash{'module'}: $\n";
    }


    # check to see if we can call 'method' this step can be
    # skipped in Perl, the failure will be caught in the below eval
    unless (UNIVERSAL::can($hash{'module'}, $hash{'method'})) {
    die "Failed to run test $hash{'method'} in module $hash{'module'}:
    Does not exist\n";
    }


    # execute the code, catch a failure
    my $result = eval {
    my $method = $hash{'method'};
    $hash{'module'}->$method();
    };
    if ($) {
    die "There was a software failure in
    $hash{'method'}.$hash{'module'}: $\n";
    }
    print "Return result good: $result\n";


    In the above the temporary setting of $method might be avoidable but I
    couldn't do it quickly. You should be aware of a few caveats mentioned
    in these docs,

    perldoc -f use
    perldoc -f eval
    perldoc -f UNIVERSAL

    Specifically about AUTOLOADED methods.

    Not sure of your intentions, if you are writing a test suite to test
    Perl code there are better ways to go about this.

    HTH,

    http://danconia.org
    Wiggins Guest

  3. #3

    Default Re: Dynamic Execution question

    Wiggins d'Anconia wrote: 
     

    UNIVERSAL is not a function, drop the 'f' in the above.

    perldoc UNIVERSAL

    Sorry for the noise...

    http://danconia.org
    Wiggins Guest

Similar Threads

  1. - Dynamic controls question
    By nickname in forum ASP.NET Building Controls
    Replies: 1
    Last Post: September 11th, 08:14 PM
  2. Get execution plan of dynamic query
    By Алексей Ш. in forum PostgreSQL / PGSQL
    Replies: 1
    Last Post: January 9th, 05:05 PM
  3. Replies: 4
    Last Post: September 4th, 07:52 PM
  4. Dynamic List Question
    By Kory in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 8th, 06:45 PM
  5. DYNAMIC SQL SELECTION QUESTION
    By ITAPORT06 in forum Oracle Server
    Replies: 3
    Last Post: July 6th, 05:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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