Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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"


In reply to Re: Style, style, style by strat
in thread Style, style, style by Juerd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2021-06-22 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (105 votes). Check out past polls.

    Notices?