Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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.


Comment on Re: Simple Perl Backup help
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985331]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2015-07-02 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls