In trying to stick with
use strict;use warnings I have had to add a whole bunch of my's and locals. (My dirty secret is I have yet to use local...but I digress)
I'm not one for doing something, "because everyone does it that way."
What started my questioning is the following slice of code
open( HANDLE, "<file");
while( my($line) = <HANDLE> )
{
print $line;
}
There are probably a few people out there that have no idea why the above segment is wrong (besides error checking, or doing something truly useful in the body of the loop).
I learned to code using C/C++ so it is stuck in my head to use parens everyplace, it just makes things safer...well in C++ at least :)
Here it evaluates $line in a list context, and eats the file so that I only get to process the first line, then exit the loop. Very much not what I wanted.
I have many scripts that I have coded with a line like below at the top (or at the top of a sub..etc..)
my($var);
my($othervar) = 43;
Based on my background this looks good to me, but now that I have done some research I don't think it is correct. The correct lines should be:
my $var;
my $othervar = 43;
That does assume we don't want to declare multiple items...
my($var1, $var2, $var3);
1. So my questions are, what other nuances are there with my that have either been Gotcha's or could be?
2. Am I wasting memory/speed/other when I declare using parens on a single scalar (building a list only to throw it away?)
While I have you...what is the differences between my and local? my gives "lexical" scope, local gives "dynamic" scope. In the perldoc -f local it says:
"You really probably want to be using `my' instead,
because `local' isn't what most people think of as
"local"."
Assuming I'm most people, what do I think it means? What does it really do? Why DO you want to use it?
I would assume you want to keep variables in the smallest scope possible, so the memory/stack is given back to the system as soon as it is not needed.
Thanks for the enlightenment!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.