Okay, I had to make the title grab you, and Brittany Seems to have that effect, so sue me!

SO I have a bad habit of deleting the wrong directory/file, I wanted to solve that in a perly way, so I made myself a little script to prevent this without letting myself know that I was messin up first. I haven't posted in a while, so I thought I'd give something up. Its not even good code, LOL.
Its not much, but it does the job, and if you have ever deleted the wrong directory/file, then you know how frustrating it can be. I thought this was appropriate for CUFP, so go easy if you disagree. I'm new to using system calls, so please tear it apart and tell me if it could have been done better.

#!/usr/bin/perl -w use strict; use vars qw($temp $read); $temp=$ARGV[0]; if($temp = $ARGV[0]){ &go } else { &err } ######## sub go { ######## print "\nDo you want to remove this file completely? Or Put it in a tem +p dir?\n"; print "(c for Completely t to Archive):"; chomp($read=<STDIN>); if($read =~ m/^[tT]/){ system("mv", "$temp","/var/tmp/deftemp") | +| warn "Error : $!" } else { &go2 } } ######### sub go2 { ######### if($read =~ m/^[cC]/){ system("rm","-R", "$temp") } else { &err } } ######### sub err { ######### print "Invalid Input!\n"; } exit 0;


P.S. I also realize this could be prevented with using the
rm -i
option, but I thought it would be fun to do it in Perl.

-- Yes, I am a criminal. My crime is that of defyance.

Replies are listed 'Best First'.
Re: Oops I did it again.....
by mrbbking (Hermit) on May 12, 2002 at 17:06 UTC

    This looks like a great learning experience. So what if there's another way to achieve the same thing? (...as you noted.)
    My comments, for what they're worth:

    You never use $dest, after declaring it with use vars.

    If you're using Perl 5.6+, you might want to use 'our', instead of 'use vars'. With Perl 5.6, 'our' obsoleted 'use vars'.
    Then again, you might not. 'use vars' is more compatible. So many Ways To Do It!

    You store ARGV[0] in $temp twice. (Did you mean == in the if statement?)

    Your 'confirmation' questions are wordy, and there is no default. Convention is to make the less destructive option the default.
    Maybe set $read to 'T' when declared (i.e. our $read='T') and only change it to 'C' on user request.

    You check the 'mv' system call for success, but you don't check the 'rm' call

    You have an 'err' sub, but that sub does not call exit. So, even if something goes wrong, your program exits with '0', indicating success.

    Suggestion: Wait for more suggestions from others. Once you've got a few, pick the ones that make sense to you, and re-write it. Save a copy of what you have now so that you can compare them.

    Update: Fixed formatting. Also, ++ to Anonymonk for the our pointer.
    s!!password!;y?sordid?binger?; y.paw.mrk.;;print chr 0x5b ;;; print;print chr(0x5b+0x2);;;;;
      Great suggestions!! ++ to mrbbking This was a simple hack that I posted here for suggestions, and I can't thank you enough for yours. I'll wait on some more and put it all together, and post it at the end..

      -- Yes, I am a criminal. My crime is that of defyance.
Re: ( Move or Delete Files )
by Zaxo (Archbishop) on May 12, 2002 at 20:36 UTC

    For a far more Perly experience, see rename and unlink. They have the advantage of not forking another process. Caveat, rename does not usually work across file system boundaries. File::Copy is a better hammer, from the standard distrubution.

    Update: Maybe File::Path does what you want. It allows creation and removal of directory trees.

    After Compline,
    Zaxo

      I actually thought about unlink. My question is, will it descend into a dir, and recursively delete? I consulted the unlink reference here, and tested after you made this suggestion, and found that it doesn't(possibly due to su restrictions). rmdir() would work for this, but it will not support deleting a directory recursively, any other suggestions?

      -- Yes, I am a criminal. My crime is that of defyance.

        No, unlink doesn't recurse.

        You can, however, use it in tandem with File::Find to get the appropriate effect (or turn to File::Path's rmtree, which does this for you).

            --k.