Hi codeKnight,

In addition to what Anonymous Monk said, I would comment that this conditional statement:

elsif(($#ARGV+1) < 1) {
is a bit strange. First, it would be simpler to test:
elsif ($#ARGV < 0) {
(Note that if no argument is passed, $#ARGV will be -1.)

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):

elsif (@ARGV < 1) { # or, if you prefer an explicitly stated scalar context: elsif (scalar @ARGV < 1) {
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 conditional
if(! -e "$Dir") {
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.

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";

In reply to Re: Help with exercise by Laurent_R
in thread Help with exercise by codeKnight

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.