Faster way to execute contents of a perl script?

Discussion in 'Perl Misc' started by Brandon Hoppe, Mar 10, 2005.

  1. Hi,

    I'm trying to find a faster way to do this. Right now I have a perl script that contains
    function definitions. I have second perl script that contains calls to these functions.

    Now the first perl script will execute the second perl script hundreds of times, in order
    to call these functions.

    So I have a loop that repeatadly changes global variables and then executes the second
    perl script with a do $file; call.

    This works and all, but its not very fast. It takes about 1 1/2 seconds to execute the
    second script, so over hundreds of runs, you can see why it takes forever.

    I was wondering if there's a faster way to repeatedly re-call all these functions. Like
    reading the second script into an array and then "executing" the array. I doubt this is
    possible but I was thinking something along this line, to get the file into memory to
    execute over and over again which should be faster than to be reading from disk everytime.

    Brandon
    Brandon Hoppe, Mar 10, 2005
    #1
    1. Advertising

  2. In article <d0q1s7$1h9$>,
    Brandon Hoppe <> wrote:
    :I'm trying to find a faster way to do this. Right now I have a perl script that contains
    :function definitions. I have second perl script that contains calls to these functions.

    :Now the first perl script will execute the second perl script hundreds of times, in order
    :to call these functions.

    :So I have a loop that repeatadly changes global variables and then executes the second
    :perl script with a do $file; call.

    Don't do that. Use 'use' instead to bring the functionality in once, and then
    just call upon the routines at need. Don't worry, the global variable values that
    will be picked up will be those at execution time, not those at compile time.

    Read the documentation on 'use'; also I suggest you might as well go as far as
    formatting it into a "module" -- perldoc perlmod

    --
    "This was a Golden Age, a time of high adventure, rich living and
    hard dying... but nobody thought so." -- Alfred Bester, TSMD
    Walter Roberson, Mar 10, 2005
    #2
    1. Advertising

  3. Jim Gibson wrote:
    > In article <d0q1s7$1h9$>, Brandon Hoppe
    > <> wrote:
    >
    >
    >>Hi,
    >>
    >>I'm trying to find a faster way to do this. Right now I have a perl script
    >>that contains
    >>function definitions. I have second perl script that contains calls to these
    >>functions.
    >>
    >>Now the first perl script will execute the second perl script hundreds of
    >>times, in order
    >>to call these functions.
    >>
    >>So I have a loop that repeatadly changes global variables and then executes
    >>the second
    >>perl script with a do $file; call.
    >>
    >>This works and all, but its not very fast. It takes about 1 1/2 seconds to
    >>execute the
    >>second script, so over hundreds of runs, you can see why it takes forever.
    >>
    >>I was wondering if there's a faster way to repeatedly re-call all these
    >>functions. Like
    >>reading the second script into an array and then "executing" the array. I
    >>doubt this is
    >>possible but I was thinking something along this line, to get the file into
    >>memory to
    >>execute over and over again which should be faster than to be reading from disk everytime.

    >
    >
    > There is no need to repeatedly 'do' the second file containing
    > subroutines. After this has been done once, the subroutines may be
    > called multiple times by name. Just make sure all of the code you wish
    > to execute is really in a subroutine and not on the main branch in the
    > second file.


    The second file doesn't contain the subroutines, it contains CALLS to the subroutines.
    That's why I have to keep 'do'ing the second file so that it calls the subroutines for me.
    Brandon Hoppe, Mar 10, 2005
    #3
  4. Walter Roberson wrote:
    > In article <d0q1s7$1h9$>,
    > Brandon Hoppe <> wrote:
    > :I'm trying to find a faster way to do this. Right now I have a perl script that contains
    > :function definitions. I have second perl script that contains calls to these functions.
    >
    > :Now the first perl script will execute the second perl script hundreds of times, in order
    > :to call these functions.
    >
    > :So I have a loop that repeatadly changes global variables and then executes the second
    > :perl script with a do $file; call.
    >
    > Don't do that. Use 'use' instead to bring the functionality in once, and then
    > just call upon the routines at need. Don't worry, the global variable values that
    > will be picked up will be those at execution time, not those at compile time.
    >
    > Read the documentation on 'use'; also I suggest you might as well go as far as
    > formatting it into a "module" -- perldoc perlmod
    >


    Perhaps an example will help to visualize the problem:

    #MAIN PERL SCRIPT
    #!/usr/local/perl

    while(<INPUT>) {
    # read some value from INPUT
    $VALUE = $_;

    do "file.pl";
    }

    sub function1 {
    # do something
    }

    sub function2 {
    # do something
    }


    #FILE.PL SCRIPT
    if($VALUE eq "A") {
    &function1(34, 44, 33);
    &function2(22, 33, 44);
    }
    if($VALUE eq "B") {
    &function1(34, 44, 33);
    &function2(22, 33, 44);
    }


    As you can see, the main perl script has the subroutine definitions. Now based on some
    logic, the main script will execute either scriptA or scriptB, which just has calls to the
    subroutines.

    Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
    or compile them since that would change their behavior. This main perl script is a QUALITY
    CONTROL script that verifies that Scripts A and B have valid data.

    I hope I've given enough information as to what I'm needing to do.
    Brandon Hoppe, Mar 10, 2005
    #4
  5. In article <d0q7ff$4p9$>,
    Brandon Hoppe <> wrote:
    :perhaps an example will help to visualize the problem:

    :As you can see, the main perl script has the subroutine definitions. Now based on some
    :logic, the main script will execute either scriptA or scriptB, which just has calls to the
    :subroutines.

    :Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
    :eek:r compile them since that would change their behavior.

    I'm not sure why you say that compiling them would change their behaviour?

    Perhaps you could proceed as follows:

    sub make_sub($$) {
    my ($subname, $filename) = @_;
    open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
    local $\;
    $subfilecontent = <SUB>;
    close( SUB );
    eval "sub $subname() { $subfilecontent }";
    }


    This creates a subroutine on the fly that is does whatever the
    given file would do, by importing the content of that file.
    --
    Usenet is like a slice of lemon, wrapped around a large gold brick.
    Walter Roberson, Mar 10, 2005
    #5
  6. Walter Roberson wrote:
    > In article <d0q7ff$4p9$>,
    > Brandon Hoppe <> wrote:
    > :perhaps an example will help to visualize the problem:
    >
    > :As you can see, the main perl script has the subroutine definitions. Now based on some
    > :logic, the main script will execute either scriptA or scriptB, which just has calls to the
    > :subroutines.
    >
    > :Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
    > :eek:r compile them since that would change their behavior.
    >
    > I'm not sure why you say that compiling them would change their behaviour?
    >
    > Perhaps you could proceed as follows:
    >
    > sub make_sub($$) {
    > my ($subname, $filename) = @_;
    > open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
    > local $\;
    > $subfilecontent = <SUB>;
    > close( SUB );
    > eval "sub $subname() { $subfilecontent }";
    > }
    >
    >
    > This creates a subroutine on the fly that is does whatever the
    > given file would do, by importing the content of that file.


    Bingo! I was trying to do this earlier but couldn't figure out how to create a new
    subroutine to include everything in the subfile.

    Only thing is that I had to change this line:

    $subfilecontent = <SUB>;

    to

    @subfilecontent = <SUB>;

    Thanks!
    Brandon Hoppe, Mar 10, 2005
    #6
  7. In article <d0qajd$lcj$>,
    Walter Roberson <-cnrc.gc.ca> wrote:
    :perhaps you could proceed as follows:

    :sub make_sub($$) {
    : my ($subname, $filename) = @_;
    : open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
    : local $\;

    That should be local $/; instead.

    : $subfilecontent = <SUB>;
    : close( SUB );
    : eval "sub $subname() { $subfilecontent }";
    :}
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers
    Walter Roberson, Mar 10, 2005
    #7
  8. Walter Roberson wrote:

    > In article <d0q7ff$4p9$>,
    > Brandon Hoppe <> wrote:
    > :perhaps an example will help to visualize the problem:
    >
    > :As you can see, the main perl script has the subroutine definitions. Now based on some
    > :logic, the main script will execute either scriptA or scriptB, which just has calls to the
    > :subroutines.
    >
    > :Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
    > :eek:r compile them since that would change their behavior.
    >
    > I'm not sure why you say that compiling them would change their behaviour?
    >
    > Perhaps you could proceed as follows:
    >
    > sub make_sub($$) {
    > my ($subname, $filename) = @_;
    > open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
    > local $\;
    > $subfilecontent = <SUB>;
    > close( SUB );
    > eval "sub $subname() { $subfilecontent }";
    > }
    >
    >
    > This creates a subroutine on the fly that is does whatever the
    > given file would do, by importing the content of that file.


    Be aware that this approach will do odd things if the script has global
    variables (either package scoped or file scoped lexicals).

    The first step to aleviate this is to do as Apache::Registry does and
    compile each such subroutine script in it's own package. You also
    probably want at least some ruimentary error reporting.

    eval "sub $subname() { package MyImportedScripts::$subname;\n#line 1
    \"$filename\"\n $subfilecontent }; 1" or die $@;

    (Obiously MyImportedScripts should really be a subpackage of a package
    namespace your application already defines).

    There are still remaining issues - such as the 'will not remain shared '
    problem. (See entry in perldiag for explaination).
    Brian McCauley, Mar 11, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajat
    Replies:
    3
    Views:
    693
    Jorgen Grahn
    Jan 8, 2010
  2. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    552
    Jan Kaliszewski
    Jan 7, 2010
  3. kderaedt

    Execute shell script from a perl script

    kderaedt, Jun 26, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    171
    Andres Monroy-Hernandez
    Jun 26, 2003
  4. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    137
    Paul Lalli
    Jan 5, 2005
  5. Replies:
    20
    Views:
    704
    Gunnar Hjalmarsson
    Jan 18, 2005
Loading...

Share This Page