in reply to A conversation with a C programmer

Did you need to rock his boat that much? You could have easily answered his question and made him feel good about Perl with

next unless $file =~ /xls$/;
he'd understand that.

-derby

Replies are listed 'Best First'.
Re^2: A conversation with a C programmer
by Anonymous Monk on Mar 15, 2006 at 19:56 UTC
    Along those lines, it probably would have been a nice gesture to let the C programmer know that
    next if($file eq "."); next if($file eq "..");
    can be condensed to
    next if $file =~ /^\.{1,2}$/;
    Isn't that more Perlish, and help the programmer see more of the usefulness of regexes without making him too lost in what is going on? But now that I think about it, the whole snippet can just be compressed to something like
    opendir(TARGET, $target); while($file = readdir(TARGET)) { &process_new_file($file) if $file =~ /\.xls$/; }
    right? Do you think that would rock the boat too much?
      opendir my $dh, $target; /\.xls$/ and process_new_file($_) while readdir $dh;

      I started out writing this facetiously, but now I kind of like it! It reads very nicely. YMMV =)

      I think
      next if $file =~ /^\.\.?$/;
      or even
      next if $file =~ /^\.+$/; # no longer recommended. see below
      is much nicer than
      next if $file =~ /^\.{1,2}$/;

      But that's just me.

      UPDATE/note: yes, the second one will also match files named with any quantity of dots as long as that's the entire filename. In practice, I've never seen a file that started with "..", but I agree it's an issue.

        yes, the second one will also match files named with any quantity of dots as long as that's the entire filename. In practice, I've never seen a file that started with "..", but I agree it's an issue.

        I have, but only on a machine that's been broken into. It is, or at least used to be, common to hide malicious software inside directories called ... -- of course, these days all the l33t kiddies are using loadable kernel modules to hide their directory trees anyway, so it may not be as relevant.

        --
        @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

        But that's just me.

        *nod*, I don't at all like your second preference.