Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Compiling and executing multiple files.

by valavanp (Curate)
on Aug 30, 2006 at 05:16 UTC ( #570309=perlquestion: print w/replies, xml ) Need Help??
valavanp has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl monks, I have multiple files in a folder. I want to complile and execute all the files if there are any errors in that files it has to be stored in a text file and give me the result of the files. In command prompt only i have tried compliling the files one by one. Since i have about 800 files i need some solution for this. Thanks for your reply. Regret if any grammatical mistakes.
  • Comment on Compiling and executing multiple files.

Replies are listed 'Best First'.
Re: Compiling and executing multiple files.
by chargrill (Parson) on Aug 30, 2006 at 05:38 UTC

    Compile and execute? Generally pretty much the same thing, unless you mean you want to run perl -c <filename> before each run.

    Though not strictly a perl question, something like this ought to get you started:

    $ for i in `ls *.pl`;do perl -c $i > $i.log 2>&1; echo "compiled, now +running" >> $i.log; perl $i >> $i.log 2>&1;done

    Note: not thoroughly tested.

    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
Re: Compiling and executing multiple files.
by cdarke (Prior) on Aug 30, 2006 at 08:32 UTC
    Are the files perl source, or are you compiling something else?
    Which operating system are you running on?
    The solution provided above will work on *nix systems only
    (strictly speaking `ls *.pl` is an unnecessary child, *.pl should be enough).
      i think you should try `make`
      Yes all files are perl source. I am running it on SUSE linux OS. The solution which chargrill given is in shell prompt. Is there any way i can do it inside a perl script. Please regret me if i am wrong it's just a suggestion. Also when i execute the below code: for i in '*.pl'; do perl -c $i>$i.log 2>&1; echo "compiled, now running" >> $i.log; perl $i >> $i.log 2>&1;done I am getting the result for only one file not for other filees.
        Of course (BTW, you put single quotes around *.pl, which won't expand to filenames in the shell). TMTOWTDI, and the elegant way would be to redirect STDOUT and STDERR in Perl, and use fork/exec, but I guess you do not want to go that far. Simplistically it is not all that different from shell:
        for my $script glob('*.pl') { system ("perl $script>$script.log 2>&1"); }

        I havn't done a separate compile phase, chargrill showed how to do that if you need it. I am 'cheating' by allowing the shell to do the redirection, which purists will not like, but it is simpler this way.
        You can programatically pick up the exit value from the call by testing the return value from system(), or $? (die returns 255 or the last value of $!).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://570309]
Approved by chargrill
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2017-03-29 19:58 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.