Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Help with exercise

by NetWallah (Canon)
on Sep 23, 2017 at 17:21 UTC ( [id://1199966]=note: print w/replies, xml ) Need Help??


in reply to Re: Help with exercise
in thread Help with exercise

In keeping with your recommended best practices of error checking at each step, the main code should be
chdir $dir or die "ERROR: Could not change directory to $dir +"; mkdir "FirstSubDir" or die "ERROR creating FirstSubdir:$!"; mkdir "SecondSubDir" or die "ERROR creating SecondSubDir:$!";

                All power corrupts, but we need electricity.

Replies are listed 'Best First'.
Re^3: Help with exercise
by Random_Walk (Prior) on Sep 24, 2017 at 07:40 UTC

    of course once we get into checking the returns, we may find chdir does all the testing we need already ...

    use strict; use warnings; use Cwd; my $dir = shift; chdir $dir or die "Couldn't chdir $dir: $!\n"; print "OK, in ". getcwd . "\n";

    And the outut

    # ./change.pl Downloads/ OK, in /home/Random/Downloads # ./perl change.pl foo Couldn't chdir foo: No such file or directory # ./mkdir bar # ./chmod 000 bar # ./perl change.pl bar Couldn't chdir bar: Permission denied

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re^3: Help with exercise
by Laurent_R (Canon) on Sep 24, 2017 at 07:47 UTC
    Yes, agreed ++. Here, I wanted to keep it simple in the context of an exercise.

    Although, to tell the full truth, I am not sure I would really check the chdir return status after I have checked that $dir contains a valid directory entry in the current directory (I know it might still fail for various reasons, such as missing privileges, but this is quite unlikely in my context and, sometimes, there is a trade-off between code complexity and coding efficiency). Or I might probably use the autodie module in such a context with a lot of system calls.

    Update: or, as suggested by Random_Walk, we might go the other way around and let chdir do the checks.

      BTW, mkpath from Path::Tiny does it all, less or more:

      "...Errors will be trapped and an exception thrown. Returns the list of directories created or an empty list if the directories already exist, just like make_path."
      karls-mac-mini:monks karl$ perl -MPath::Tiny -E 'say path (q(/foo/bla) +)->mkpath' /Users/karl/Desktop/bla karls-mac-mini:monks karl$ perl -MPath::Tiny -E 'say path (q(/foo/bla) +)->mkpath' karl-mac-mini:monks karl$

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-18 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found