Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Is this the most elegant way to code directory lookup?

by Hue-Bond (Priest)
on Sep 29, 2006 at 14:12 UTC ( [id://575531]=note: print w/replies, xml ) Need Help??


in reply to Is this the most elegant way to code directory lookup?

Some comments:

unless (-d $srcdir && -d $destdir )

It's better not to use unless, unless for simple conditionals ;^).


opendir(INDIR,$srcdir) || die

Better use lexical filehandles. Also, I don't like to use parentheses, it makes the code more perlish:

opendir my $indir, $srcdir or die "blah: $!";

unless (-d || $_ eq "." || $_ eq "..") {

Same as above. Quick, what does this do?

unless ($bar =~ /bar/ or $baz !~ /qux/ and defined $undef)

`tar -cz $_ -f $_.tar.gz`;

Use backticks when you are going to use the output of the command. This is not the case, so you better use system.

Update: Oh, there's a recent thread about unless, be sure to check it out.

--
David Serrano

Replies are listed 'Best First'.
Re^2: Is this the most elegant way to code directory lookup?
by BrowserUk (Patriarch) on Sep 29, 2006 at 14:39 UTC
    Quick, what does this do?
    unless ($bar =~ /bar/ or $baz !~ /qux/ and defined $undef)

    Exactly the same as

    if( not ($bar =~ /bar/ or $baz !~ /qux/ and defined $undef) )

    There. So much clearer now...not.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      BrowserUk++

      A not, a regex, an or and an and. Not only not clear but almost impossible for me to get my head round.

      I resort to extreme lengths to try and get it straight.

      if( not ( ( $bar =~ /bar/ or ( $baz !~ /qux/ and defined $undef ) ) ) )
      But I find that almost always these constructions are the result of a poor algorithm. As the old saying goes "get your algorithm right and the code writes itself". While we're here, that is closely followed by "don't try to code yourself out of a problem, start again".

      It works for me! :-)

      There. So much clearer now...not.

      Your ability to make something less clear doesn't negate everyone else's ability to make something more clear.

      if( ! -d && $_ ne "." && $_ ne ".." ) {

      Yes, that prevents a noticeable mental pause that the "unless" version induced. [ Even better, it avoids the mental tension between how "unless" is usually used in English vs. how it is used in Perl and the fact that the unexplicit "not" implied by "unless" is too easy to mentally drop. ]

      - tye        

        So the condition to which I was referring:

        if( not ($bar =~ /bar/ or $baz !~ /qux/ and defined $undef) )

        can be tranformed into

        if( ! -d && $_ ne "." && $_ ne ".." ) {

        I guess I'll have to review DeMorgan's law (Tye's version) plus some of this.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      You made Hue-Bond's point. The statement was hard to understand without some major transformations. For example, the following is clearer:
      if ( $bar !~ /bar/ and ( $baz =~ /qux/ or not defined $undef ) )

      But let's not stop there. Reduce the number of things the reader has to keep in mind by breaking the statement into two.

      if( $bar !~ /bar/ ) if ( $baz =~ /qux/ or not defined $undef )
        You made Hue-Bond's point.

        No. You made my point.

        An overly complex conditional is an overly complex conditional regardless of whether it appears as part of an if statement or an unless statement. Or as you more succinctly put it:

        The statement was hard to understand without some major transformations.

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found