Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
while (<>) { ... } or while (my $line = <>) { ... }?

It depends... in general, the former for short scripts the latter for larger projects.

-w or use warnings;?

It depends... usually -w because I've got to make things work on 5.005 (and .004 for that matter) much of the time. Again, if it is a larger project and I can control the version of perl, I prefer use warnings especially since they'll provide better granularity if needed.

sub CONSTANT () { ... } or use constant CONSTANT => ...;?

use constant CONSTANT => ...;but I confess to secretly wishing that we all still had to use references to literals... ;-)

my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;?

It depends... I use the list assignment by default but there are exceptions. I usually shift $self as dws does. Sometimes I don't need all the args, in which case I may shift only when I need the next one. Rarely, but on occasion, I even find a reason to use $_[0] and friends directly.

for (@array) { ... } or foreach (@array) { ... }?

I use for consistently. Whatever is inside the parens can speak for itself. I used to use foreach but I've had one time too often when I've changed (;;) to (@a) and forgot to change the keyword. I've done the reverse too and its kind of embarrassing when someone finds a foreach(;;) in your code. Sure it works but there's no real excuse for it. I found it was easier to just use for all of the time.

print 'foo'; or print('foo');??

No parens unless necessary. print( ($a + $b) * $c, "\n" ); is a case where it might be necessary. I use parens with printf() though.

'simple string'; or "simple string"?

Single quotes unless it's in a print in which case I usually use doubles because, when I don't, I always end up needing to change them. If I need double quotes elsewhere, I tend to prefer qq().

glob '*' or <*>?

Neither recently. I have used <*> but I'd rather not remember those days. I'm with dws on this one too. opendir() et al is "The Right Way™".

readline *FOO or <FOO>?

It depends... Ha! Just kidding. I always use: <FOO> ... Perl should look like Perl.

for (keys %foo) { $_ and $foo{$_} } or while (my ($key, $value) = each %foo) { $key and $value }?

It depends... I don't use each very often but there have been and will be exceptions.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Style, style, style by sauoq
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 chanting in the Monastery: (7)
As of 2021-06-24 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (124 votes). Check out past polls.

    Notices?