The result of this node will most likely be "it depends on the programmer; it is a personal preference", but I would still like to see how others tend to organize their code. The issue I am talking about is where to put the declarations for lexical variables (declared with my()), whether we are talking about file-scoped lexicals (not contained within any block), or lexicals of any block (subroutine, bare block, loop construct, conditional block, etc). As the simple example for this meditation, I will use an example which uses file-scoped lexicals.

The first style is to declare all your lexicals at the beginning of the scope in which they are found. The advantage to this style is that you can see at first glance which variables are meant to be used within the scope you've just entered. The disadvantage is that when you finally stuff the variable with data, you can't tell at first glance whether this is the first time data is being assigned to the variable, or if we are redefining it (replacing the current contents with new data). Our example demonstrating this first style:

#!perl -w use strict; use IO::Socket; use IO::Select; # advantage: we know all the variables that are used # within this particular scope. my ( $server, $handles, $sessions ); $server = IO::Socket::INET->new( Listen => 1, LocalAddr => 'localhost:8000' ) or die "socket failed: $!"; $handles = IO::Select->new( $server ); # disadvantage: at first glance, we can't tell if this is # the first time we are assigning to $sessions. You'd have # to scroll up from this point to see if we've already # put data into $sessions. $sessions = {}; { my $socket; while ( $socket = $server->accept() ) { $sessions->{$socket} = { peerhost => $socket->peerhost() }; } }

The second style is to not declare all lexicals at the beginning of the scope in which they are found, but to declare them when they are to first come into existance. The advantage is that you can immediately see where the variable first comes into play in the flow of the program, since the place where it is declared is also the first time it is given contents. The disadvantage is that it is not as easy to determine which variables are used within a particular scope, since we are scattering variable declarations everywhere. The example code rewritten with the second style:

#!perl -w use strict; use IO::Socket; use IO::Select; my $server = IO::Socket::INET->new( Listen => 1, LocalAddr => 'localhost:8000' ) or die "socket failed: $!"; my $handles = IO::Select->new( $server ); # advantage: we know this is the first time $sessions # is being assigned to. Any '$sessions = ...' later in the # program are obviously replacing the contents of $session. my $sessions = {}; while ( my $socket = $server->accept() ) { $sessions->{$socket} = { peerhost => $socket->peerhost() }; }

My personal preference tends to lean towards #2 as it is generally easier to follow the program flow. There are times that #1 seems more convenient, such as when you'd like to label each variable with its function. But then there is always POD to do that with if so desired. So I ask of you, the fellow monk, to share your personal preference, as well as any advantages (pros) and disadvantages (cons) you see pertaining to each style.

updates: Added a disadvantage to style #2. Modified code for style #1 by making it more equivalent to style #2 -- I moved the $socket declaration to a separate scope containing the while loop. Updated code to reflect tilly's note. I didn't realize I'd declared a hashref and then tried to access a hash. Oops :)


In reply to A question of style - declaring lexicals by saskaqueer

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.