http://www.perlmonks.org?node_id=167151

Hi,

I found myself using glob and readline (the words) more and more. Not only to avoid some annoying problems with the shared angle bracket syntax, but also because I think it's clearer. I also tend to use qx() instead of backticks, and and or instead of && and || (not only for precendence reasons), etcetera.

How do you feel about this? Do you think <*.txt> is better than glob '*.txt'? Is <$handle> prefered over readline $handle? And what about backticks: do you think ' and ` look different enough to use backticks?

I'd like to know how other monks think about this TIMTOWTDI/style thing :)

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re: Pronounceable syntax or beloved punctuation?
by tadman (Prior) on May 17, 2002 at 00:56 UTC
    I think glob and readline are great at specifying exactly what you are doing, and both are resistant to casual errors, especially when your glob and filehandle can look so close:
    my $line = <$fh>; # Filehandle? my $file = <*fs>; # Glob?
    Much better to be specific, like you say:
    my $line = $fh->readline(); # Filehandle! my ($file) = glob("*fs"); # Glob!
    The angle brackets and the whole DWIM thing are for "programmer efficiency" and not necessarily "program clarity". In some cases, you would want to avoid them entirely.
Re: Pronounceable syntax or beloved punctuation?
by Mr. Muskrat (Canon) on May 17, 2002 at 03:46 UTC
    I prefer glob '*.txt' to <*.txt>. Why? I think it's easier to understand what is going on.
    And the use of readline, well, it really depends on the situation. Sometimes, it just makes more sense to use <$fh>.
    My 2¢.

    Who says that programmers can't work in the Marketing Department?
    Or is that who says that Marketing people can't program?
(jeffa) Re: Pronounceable syntax or beloved punctuation?
by jeffa (Bishop) on May 17, 2002 at 15:07 UTC
    1. <*.txt> vs. glob '*.txt':
      Depends. A neat trick i lifted from merlyn is to use File::Find to find your target files and push them to @ARGV via:
      push @ARGV, $File::Find::name if /MAGIC_CONDITION/;
      then you can modify their contents via the empty angle brackets: <>. I don't think you can do that with glob ...
    2. readline over angle brackets:
      I tend to use angle brackets - guilty of lazy typing. ;)
    3. backticks
      I try to avoid them anytime i can. When i first started using Perl i used them a lot. Too much. Then i found system and exec, and IO::Pipe among other CPAN modules that can replace backticks.
    All in all, i think a good exercise is to force yourself to practice The Other Way. Not necessarily in your production code, but in the throw-away scripts that you write from time to time. Balance is good. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      you can modify their contents via the empty angle brackets: <>. I don't think you can do that with glob ...

      Well, this is the one case where I prefer angle brackets (in this special case often refered to as "the diamond operator") over the readline function.

      You were right about this not being able with glob, as empty angle bracket expressions are handled by readline :) <> eq <ARGV> eq readline *ARGV (but only if the three lines (records) are stringologically equal).

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        Actually, you don't need * in readline *ARGV, readline ARGV is enough.

Re: Pronounceable syntax or beloved punctuation?
by Sifmole (Chaplain) on May 17, 2002 at 11:34 UTC
    I guess I have been reading code too long because when I read &&, ||, !, etc they read right off -- but sometimes I have to catch myself when I seed "and" and/or "or".

    As for the globbing and readline. I use glob/readline when I am not looping and use the angle brackets when I am. Makes sense to me that way.