Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Style, style, style

by strat (Canon)
on Sep 08, 2002 at 10:52 UTC ( #195999=note: print w/replies, xml ) Need Help??


in reply to Style, style, style

I never use while (my $line = <FH>) because while (<FH>) is "shorthand" for: while (defined( $_=<FH>)), and the defined can sometimes be important if your last entry in a file is a 0 without \n afterwards. And normally, I don't want to miss a single zero at the end which could even happen with <> if you pipe in a file.
I don't like <> without a filehandle in larger codes, I prefer doing it the long way round with open(FH, ... while (<FH>) or the like. But with short scripts (or throw-away-scripts) <> often seems ok.

-w or use warnings: with perl5.0, I prefer -w, with perl >= 5.6 I use warnings because it is not global. But for production code, i remove -w or use warnings with a comment because I don't want an inocent user be confronted with strange "error messages" they don't understand.

I prefer use constant CONSTANT => ...; because it makes clear that I want to use a constant and not a sub (although internally it all is about the same).

my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;?
I use both, but prefer the first solution, because then I've got an entry in a subroutine at the very beginning, which may keep the code a bit more readable. But if you don't know how many parameters are coming, I sometimes use shift in a loop (with commenting a lot why to do so).

In bigger codes, I prefer using print as a function (print(...)), but I don't know why. One reason may be that with a good syntax highlighning editor, you can easier find matching parantheses...

glob '*' or <*>: since I have to work a lot with perl5.005_03, I prefer using the module File::DosGlob. With perl >= 5.6, I prefer glob

readline *FOO or <FOO>?: I always use <FOO>, because in my eyes, it is more idiomatic. But for a beginner, readline might be easier to understand in an existing code

foreach vs. while each: It just depends on what I need. If I need some way of sorting, I have to use foreach. If not, then I nearly always use each, because with bigger datastructures, it might be faster, and $key and $value are imho better namens than $key and $foo{$key}.

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Replies are listed 'Best First'.
Re^2: Style, style, style
by Aristotle (Chancellor) on Sep 08, 2002 at 12:24 UTC
    while (<FH>) is "shorthand" for: while (defined( $_=<FH>)), and the defined can sometimes be important if your last entry in a file is a 0 without \n afterwards. And normally, I don't want to miss a single zero at the end which could even happen with <> if you pipe in a file.
    Good worry, but no need to lose sleep since Perl's behaviour has been adapted to do the right thing in the real world a while ago.
    $ perl -MO=Deparse -e'1 while my $line = <>;' '???' while defined(my $line = <ARGV>); -e syntax OK $ perl -v This is perl, v5.6.1 built for i386-linux
    I'm not sure exactly when it changed; perldoc perldelta for 5.6.1 doesn't mention it and I can't seem to find any mention in older versions of the POD found on perldoc.com either. Of course even on older Perls, you can use the my construct by simply adding the defined yourself..
    while(defined(my $line = <>)){ # ... }

    Makeshifts last the longest.

      A most obscure bug discusses the "implied defined" feature, its history, and a bug in its implementation that we caught.

      -Blake

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-06-22 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (106 votes). Check out past polls.

    Notices?