in reply to Help with exercise
In addition to what Anonymous Monk said, I would comment that this conditional statement:
is a bit strange. First, it would be simpler to test:elsif(($#ARGV+1) < 1) {
(Note that if no argument is passed, $#ARGV will be -1.)elsif ($#ARGV < 0) {
Second, if you want to test the number of arguments passed to the program, it is clearer to evaluate @ARGV in scalar context (which gives the number of items in that array):
Last, but not least, this conditional statement will never be true because, if you don't pass any argument, this statement will not be executed: the program will die with the first conditionalelsif (@ARGV < 1) { # or, if you prefer an explicitly stated scalar context: elsif (scalar @ARGV < 1) {
In fact, it is quite strange to check if an argument is passed after having checked whether the value of the argument is a valid directory.if(! -e "$Dir") {
So if you want to check that at least one argument is being passed to the program, you should do it first.
Also, it is a bit useless to check for the existence of $Dir and then whether it is a valid directory (unless you want to print a different message in that case); you can check directly that it is a valid directory.
So, this program could be rewritten as:
use strict; use warnings; die "Please supply an argument to the program" if @ARGV < 1; my $dir = shift; die "Please try again with a valid directory.\n" unless -d $dir; # Change to user selected location chdir $dir; mkdir "FirstSubDir"; mkdir "SecondSubDir";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help with exercise
by NetWallah (Canon) on Sep 23, 2017 at 17:21 UTC | |
by Random_Walk (Prior) on Sep 24, 2017 at 07:40 UTC | |
by Laurent_R (Canon) on Sep 24, 2017 at 07:47 UTC | |
by karlgoethebier (Abbot) on Sep 24, 2017 at 16:11 UTC |