Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Declaring a Variable [for BEGINNERS]

by gaal (Parson)
on Dec 27, 2006 at 19:44 UTC ( #591924=note: print w/ replies, xml ) Need Help??


in reply to Declaring a Variable [for BEGINNERS]

This example does not declare variables in the beginning of the script: that is, these variables are only visible inside the scope of the sub CheckAllPairs. This is called "lexical" scoping, and extends from a "my" declaration to the end of the scope of the my. Perl also has global variables, which you usually use the "our" declarator to introduce, but as a rule of thumb lexicals are better for most purposes.

Using strict is almost always a good idea, yes. Having so many variables like in that example looks like a lot, but maybe it's warranted by what the code is doing. If you want to declare several lexicals all at once rather than on separate lines, you can do it the following way, but it's mostly a stylistic thing:

my ($subj_num, $region, $region1, $region2, $time); # put as many as y +ou like here my ($abs_time, $max_peak_time) = (50, 2050) # you can initiali +ze multiple vars like this.


Comment on Re: Declaring a Variable [for BEGINNERS]
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://591924]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2014-11-24 09:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (137 votes), past polls