Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Simple Perl Backup help

by moritz (Cardinal)
on Aug 03, 2012 at 19:26 UTC ( #985331=note: print w/replies, xml ) Need Help??

in reply to Simple Perl Backup help

If your perl is of version 5.10 or later, you can add

use 5.010;

at the start of your script, and substitute all your  print "something\n"; lines with say "something". Looks like a small code, but it does make it more fun to read and write.

As for your original question, you can write something like

if ($input =~ /^(y|yes)$/) { say "Backup is beginning..."; last; } elsif ($input =~ /^(n|no)$/) { die "Program is shutting down.\n"; } else { say "This is not a valid answer."; say "Please enter 'yes' or 'no'"; }

This uses regexes to test for several values at once, laading to fewer branches with duplicate code. You could also write

if ($input eq 'y' || $input eq 'yes') { say "Backup is beginning..."; last; }

if you don't want to use regexes.

Finally you should check the return value of system. Just because the backup file exists doesn't mean the backup was successfull. There could be a full file system, file system corruption, permission errors or other problems -- in which case tar will return a non-zero return code, which you should catch. See system for the full documentation on how to do that.

Replies are listed 'Best First'.
Re^2: Simple Perl Backup help
by laxman526 (Initiate) on Aug 03, 2012 at 19:32 UTC

    Thanks moritz! This is really good information. Thanks for your timely response.

    I am definitely working to wrap my head around regexes. I will take that into consideration.

    Also, thanks for the input on error checking. I never took that into consideration either. I will get to work on that as well.

    Back to coding....thanks again.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985331]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-04-24 06:42 GMT
Find Nodes?
    Voting Booth?