If you're on a Unix/Linux/Mac, type perldoc perl into a terminal window, and see all the documentation you already have installed on your machine. ( I dunno how to view it on MSDos, but I'd try typing the same thing in a cmd shell. ) or you can go to CPAN or ask google to find 'perl pod'. perlsyn is the section which actually discusses syntax.

Briefly, there is almost never a reason to use the C style for loop. If you want to iterate over a list, let Perl handle the hard part:

for my $elem ( @$mysql ) { my $field1 = $elem->{field}; }

If you want to keep track of the index number, use the range operator, let Perl handle the hard part:

for my $idx ( 0..@$mysql ) { my $field1 = $mysql->[$idx]{field1}; }

Notes: If you specify an array in a scalar context, Perl decides you want to know how many elements there are in the array. The range operator is definitely a scalar context, it expects a number on the left and a number on the right. I frequently use scalar @array, so others will decipher what I'm doing more easily; some people think the superfluous 'scalar' is bad ( Hi Mike ).

Some people differentiate between 'for' and 'foreach'; they must love typing. I use 'for' for both, since Perl considers them synonyms.

You don't need to quote hash tags, you can use the bare text; similarly, the bare characters before a fat arrow are automatically taken as a string:

use 5.010; $tag = 'Homer'; my %h = ( $tag => 'woohoo!', Fred => 'YABA DABA DOO!' ); say Dumper(\%h); # outputs: # $VAR1 = { # 'Homer' => 'woohoo!', # 'Fred' => 'YABA DABA DOO!' # };

As Occam said: Entia non sunt multiplicanda praeter necessitatem.


In reply to Re: Style and Preference on Loops by TomDLux
in thread Style and Preference on Loops by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.