Well, I wan't to share my code style too, but with less rules, since I think that simple is better, and I will explain some:

  • 2 spaces for ident.
         Because you win horizontal space, is easier, and 2 is just the sufficient to identify the difference of levels.
  • No table (\t) in the code, including index.
        Table doesn't work well in all the editors and OS!
  • Open block in the same line: if (...) {
  • Do not declare else/elsif in the same line of previous close block!:
        if (...) {
        ....
        }
        else { }
  • Blocks, with 1 group of statement, or small blocks, will be in one line:
        if (...) { $x++ ;}
  • Always put the ";", even when is the last in the block: (use like the previous code).
        Because if you put the ";" always, you can add codes after it without care to add the ; in the prevoius. If you cut the ";" and need to put new codes after it, the probability to forget to check the previous line is big, and this make bugs!
  • In foreach use for the scalar the same name of the array plus "_i":
         foreach my $array_i ( @array ) {}
        ## Note that I have an editor that make this for me! ;-P
  • Separate the code in subs, and if make sence build it in OO, or better a Perl Module.
  • Spaces between strings and ",": (a , b, c). But not between string and "("! But for variables use the space: ( %hash , a , b)
  • Spaces when the code open and close alot "(","{": join("x", length( $hash{ $hash2{k}{k2} } )) ;
  • Put ";" separated of the last code: length($foo) ;
        Because if you want to add or change the code ";" is already the position, and avoid the wrong cut of chars:
        length($foo) if $foo ;
  • Use $i,$j,$k... for index, $c for count, and $s for "tmp" string.
  • 1 for true, undef for false, not 0!
  • Use $var shift only for objects, and variables that will be used a lot, or when directly access $_[0] doesn't bring speed!
  • Always Class->method, never method Class (this includes "new"!). (I agree with this too)
  • For q or qq, use "`": $var = q`fooo` ;
        Since "`" is not much used for strings! Better than ~,/, or anoy other.
  • Always use parens: tie( %hans , 'pack' , arg ) ;
  • For returned data always use: return() ;
  • Before each sub make a comment box: (My editor make this for me, useful to mark the point and open a area for comments to:)
        #######
        # FOO # Some comment
        #######

        sub foo {
        ...
        }

  • Well, I think that for now is just this!

    Graciliano M. P.
    "The creativity is the expression of the liberty".


    In reply to Re: Style, *again* by gmpassos
    in thread Style, *again* by Juerd

    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.