Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How to find code written by smart people

by tphyahoo (Vicar)
on Aug 21, 2006 at 14:39 UTC ( #568554=perlmeditation: print w/ replies, xml ) Need Help??

System calls tend to make a little uneasy.

Today I needed to do mkdir in the middle of something unwieldy and I thought, uh oh, how do I error check against this.

perlfunc mkdir wasn't real helpful, and perlmonks didn't pop anything up right away either.

My shortcut was to use the google "site:" syntax to search stonehenge.com repository of merlyn articles

There, in the third result, was my answer. Since google gives such nice summaries, with the key words bolded for easier viewing, I saw it right away: -d $CORRUPT or mkdir $CORRUPT, 0755 or die "Cannot mkdir $CORRUPT: $!";

So, that's all.

If there's ever a bit of idiom that you *almost* know, but can't quite nail, and you're too shy to ask on the chatterbox, do a site query on stonhenge.com or (who else is good for this?) and sometimes the answer pops right out.

Cheers :)

***************

UPDATE: Actually, though, what I really wanted was

#!/usr/bin/perl use strict; use warnings; my $log_dir="/root/blee"; my $result = system("mkdir -p $log_dir"); print "result: $result\n"; -d $log_dir or system("mkdir -p $log_dir") && die "couldn't make $log_ +dir: $!"; print "error: should have died already\n";

Because system returns 0 on success, whereas perl mkdir returns 0 on failure.

So much for magic bullets ;)

Comment on How to find code written by smart people
Select or Download Code
Re: How to find code written by smart people
by diotalevi (Canon) on Aug 21, 2006 at 14:48 UTC

    See also File::Path which provides mkpath(). Use it when the you have nested directories to create like foo/bar/baz and you think that any of foo/bar or foo might need to also be created.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: How to find code written by smart people
by explorer (Chaplain) on Aug 21, 2006 at 15:09 UTC
Re: How to find code written by smart people
by jplindstrom (Monsignor) on Aug 21, 2006 at 16:46 UTC
    Personally I like to do it like this1:

    use File::Path; mkpath($dir); -d $dir or die("Could not create ($dir)\n");

    It depends a little on what we're really after here but in my experience it's generally not very interesting whether the directory could be created or not. What's interesting is that it's there so we can do stuff with it.

    So that's what we test for.

    /J

    1) Encapsulated into ensure_dir($dir).

Re: How to find code written by smart people
by shmem (Canon) on Aug 21, 2006 at 22:49 UTC
    If there's ever a bit of idiom that you *almost* know, but can't quite nail, and you're too shy to ask on the chatterbox,
    there's always /msg in the chatterbox,
    do a site query on stonehenge.com
    besides querying that most honorable site, to
    (who else is good for this?)
    any of the honorable monks here, of which one might answer right away or take the quest and, with less shyness, ask in the chatterbox or even post to SoP.

    --shmem

    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How to find code written by smart people
by BerntB (Deacon) on Aug 22, 2006 at 07:58 UTC
    If there's ever a bit of idiom that you *almost* know, but can't quite nail, and you're too shy to ask on the chatterbox,
    I stretched my arm out and got "Programming Perl" from my Perl book pile. (The only problem here is the sheer size of the index, which adds seconds of page turning.) Also, "perldoc -f mkdir" worked fine for me.

    But I'd have missed the "File::Path" comment here. Hmmm... the Chatterbox seems to be the best place, all things considered.

Re: How to find code written by smart people
by radiantmatrix (Parson) on Aug 25, 2006 at 14:22 UTC

    System calls should make you a little nervous. It seems from the context of your post that you chose a system call because Perl's mkdir doesn't do what mkdir -p does on a Unix system. Here's a more-Perlish version that will work on most platforms (not just Unix):

    #!/usr/bin/perl use strict; use warnings; use File::Spec::Functions qw(splitpath catdir rel2abs); # mkdir -p emulator, call with this.pl /path/to/create my $path = rel2abs(shift @ARGV); my $place = ''; foreach my $dir ( splitpath($path) ) { $place = catdir($place, $dir); (-d $place) || (mkdir $place, 0755) or die "Problem making $place: + $!"; }
    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet

      Why not use File::Path?

      $ ls foo ls: foo: No such file or directory $ perl -MFile::Path -e 'mkpath "foo/bar/baz"' $ ls -d foo/bar/baz foo/bar/baz/

      Update: Err, forget it. It's already been pointed out.

      --
      David Serrano

        Because of this line in File::Path's POD:

        It returns a list of all directories (including intermediates, determined using the Unix '/' separator) created.

        That raises a concern that I'd have to pass mkpath a UNIX-style path -- the snippet I posted will accept any form supported by File::Spec::Functions. I like that better.

        <radiant.matrix>
        A collection of thoughts and links from the minds of geeks
        The Code that can be seen is not the true Code
        I haven't found a problem yet that can't be solved by a well-placed trebuchet

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-08-20 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (111 votes), past polls