Re: shift or die unless
by apl (Monsignor) on Dec 03, 2008 at 12:46 UTC
|
You could also use Getopt::Long to gather your parameters. If the desired one is undefined, you could then do your date testing. | [reply] |
Re: shift or die unless
by jettero (Monsignor) on Dec 03, 2008 at 12:17 UTC
|
Yeah, that's fine, but you have to shift @ARGV rather than @_. Also, I think you need to use (time - (stat $arg)[9])>7 rather than -M, but I could be wrong.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
I did not know that. I'm shocked actually. Here I've been naming @ARGV all this time ... Huh.
| [reply] |
|
|
| [reply] [d/l] [select] |
Re: shift or die unless
by dsheroh (Monsignor) on Dec 03, 2008 at 14:50 UTC
|
Personally, I think you're trying to do too much in that one line. It's not immediately clear when I look at it whether it means
($arg = shift || die "takes one arg") unless -M $file >7;
or$arg = shift || (die "takes one arg" unless -M $file >7;)
It would, IMO, be much better written as two separate statements. | [reply] [d/l] [select] |
|
|
Only if you think unless is an operator? The second won't even compile.
| [reply] [d/l] |
|
|
It wasn't meant to be runnable code, just to indicate logical grouping.
More verbosely: It is not immediately obvious whether that line means "check the date of the file and, if it's less than a week old, shift or die" or "if shift returns a false value and the file is less than a week old, then die".
| [reply] |
|
|
|
|
|
Re: shift or die unless
by graff (Chancellor) on Dec 04, 2008 at 04:46 UTC
|
You described your goal like this:
the script will run whether the arg is there or not and if its not, check the modification date of a file and if its a week old, run a certain sub.
I don't see any indication of trapping an error condition, so why do you have: die "takes one arg"? If I understand your description, the idea is:
- If there is something in @ARGV, do something with that.
- else, if $file is older than 7 days, call a subroutine
- else exit (nothing to do, except may explain to the user why nothing was done).
If you feel compelled to put all that in one statement, I'd feel compelled to ask why. Spreading it out a bit does not create any problems, and does clarify the intent:
if ( @ARGV ) {
do_something( shift );
}
elsif ( -M $file > 7 ) {
do_something_else();
}
else {
warn "Nothing to do ($file is recent, and you gave no arg to work
+on)\n";
warn "Usage: $0 [optional_arg_that_does_what_exactly]\n";
}
# default exit status will be "0" (success)
If the "nothing to do" condition should really be treated as a failure, use die instead of warn with the "Usage: ..." message. You would also use die (or Carp::croak) in your subroutines, or check their return values and die after the call, if necessary. | [reply] [d/l] [select] |
Re: shift or die unless
by JavaFan (Canon) on Dec 03, 2008 at 16:38 UTC
|
$arg = shift or -M $file > 7 or die "takes one arg";
| [reply] [d/l] |