Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

localising variables 1

Status
Not open for further replies.

fortytwo

Technical User
Apr 18, 2000
206
GB
I saw this piece of code in subroutine and the comment said something about localising the variable.<br><br>local($check_referer) = 0;<br><br>What does localising a variable do? <p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br>
 
Hi Will,<br><br>You often see this <b>kind</b> of thing at the beginning of a subroutine.<br><br><FONT FACE=monospace><b>local ($_) = @_;</font></b><br><br>This code gives you a local version of <FONT FACE=monospace><b>$_</font></b> in your subroutine. You can change the contents of <FONT FACE=monospace><b>$_</font></b> there without having to worry about clobbering <FONT FACE=monospace><b>$_</font></b> in your main program or other subroutines.<br><br>Use <FONT FACE=monospace><b>local</font></b> for things like <FONT FACE=monospace><b>$_</font></b>, predefined Perl variables.<br><br>Use <FONT FACE=monospace><b>my</font></b> for your own variables that you want to be private to that subroutine, like this.<br><br><FONT FACE=monospace><b>my ($param1, $param2) = @_;</font></b><br><br>to pick up the subroutines parameters or:<br><br><FONT FACE=monospace><b>my ($i, $a, $x)=(0,0,0);</font></b><br><br>to create and initialise some local variables or just<br><br><FONT FACE=monospace><b>my $local_var;</font></b><br><br>to create a local variable.<br><br>Anyway - long answer so hope it helps.<br><br>Regards<br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top