Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Deleting corrupted perl files - modifying the perl runtime

by kylespitz (Initiate)
on Jul 28, 2008 at 22:01 UTC ( [id://700680]=perlquestion: print w/replies, xml ) Need Help??

kylespitz has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I have a challenge for all you perl monks.

Let's assume I have thousands of corrupted perl files, like so:

Good file:
print "hello world!\n";

Corrupt file:
pr;int "hello world!\n";

except that they are each hundreds of lines long and I have some that work and many more that don't. I don't care about salvaging the broken files, all I'm trying to do (ha) is modify the perl compiler so that it deletes or lists any file that is broken. If I have a list, then I can use a script to delete the files. If the compiler can delete the file itself then that is fine too. Also, (here's the fun part) if the file isn't broken then I would like the script to run.

I'm asking you monks here because although I've tried to modify the compiler (I replaced a line that printed an error message with a delete command), there are so many errors that perl checks for and tries to fix on its own (one of the many things I love about perl) that I can't find and replace every instance within the compiler (/usr/bin/perl).

Any ideas? Thanks in advance for any docs you might point me to or tips on modifying the compiler itself.

  • Comment on Deleting corrupted perl files - modifying the perl runtime

Replies are listed 'Best First'.
Re: Deleting corrupted perl files - modifying the perl runtime
by moritz (Cardinal) on Jul 28, 2008 at 22:09 UTC
    So, what exactly identifies a corrupted file? An inserted semicolon on the third column of each line? Or just some random characters?

    If you are sure that all non-corrupt perl scripts actually compile, you can write something like this on the shell:

    for i in *; do perl -c "$i" || rm "$i"; done

    (assuming a bash-like shell). Please make a backup first ;-)

      Personally, I've learned to not do rm -- "$i" but to use the following idiom:

      for i in *; do perl -c "$i" || echo rm -- "$i"; done

      This allows me to inspect what would happen if I were to run the command. When I'm confident that the output is what I want, I either delete the echo from the line or pipe the whole output into another shell:

      for i in *; do perl -c "$i" || echo rm -- "$i"; done |bash

      I employ a similar technique before issuing a manual DELETE command on a database.

Re: Deleting corrupted perl files - modifying the perl runtime
by TGI (Parson) on Jul 28, 2008 at 23:25 UTC

    Why modify the compiler? Have you thought about using eval to check the code?


    TGI says moo

      I personally believe that ther's a strong smell of XY Problem (link provided for the benefit of the OP) here...

      --
      If you can't understand the incipit, then please check the IPB Campaign.
Re: Deleting corrupted perl files - modifying the perl runtime
by ysth (Canon) on Jul 29, 2008 at 00:18 UTC
Re: Deleting corrupted perl files - modifying the perl runtime
by pc88mxer (Vicar) on Jul 28, 2008 at 22:52 UTC
    If the bad scripts are detectable by the perl compiler, why not just run perl -cw script and inspect the (error) output?

    What kind of corruption did the files undergo? How about restoring from a backup?

Re: Deleting corrupted perl files - modifying the perl runtime
by MidLifeXis (Monsignor) on Jul 29, 2008 at 17:32 UTC

    Hmmm, let's see here, I need to write something in perl. Where can I find perl. Oh here is one.

    $ perl MyScriptThatIAmWriting.pl

    Syntax error at line 999.  File deleted.

    Arghh!!!!

    --MidLifeXis

Re: Deleting corrupted perl files - modifying the perl runtime
by ggvaidya (Pilgrim) on Jul 30, 2008 at 04:48 UTC
    Why don't you use find (1) or File::Find to find all your Perl files, feed them into perl one by one using the '-c' option to see if they compile, and make a list of all of those which don't? You can limit the search to folders where these corrupted files are, and can vet that list manually before condemning them to the bitbucket.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700680]
Approved by moritz
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found