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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My 1

Status
Not open for further replies.

Eloff

Programmer
Aug 29, 2001
78
CA
What does my do? Age: 17
School: Alberta Distance Learning Center
Location: British Columbia, Canada
If at first you dont't succeed, try, try again. - programmer's motto.
 
It declares a variable whose value will expire in when the current block ends.

IE:

sub MySub {
my $Variable = "hi";
print "in the block, \$Variable is $Variable\n";
}

print "Now, outside the block, \$Variable is $Variable";

Since the $Variable expires once the block is done, the output will be the following:

in the block, $Variable is hi
Now, outside the block, $Variable is
 
as i learned it,



"Use the keyword 'my' to declare your variables. Using the keyword 'local' creates a variable whose scope includes any called subroutines..."

perl, cgi, and javascript - 2000 Sybex



i understood it like javascript variables inside and outside of functions - declaring the variable inside would be like using 'my', it's value is specific to that function, not all functions

(sorry to quote from a book, but i'm no expert and you helped me earlier...)

hope this helps.
 
Thanks, that makes sense. Age: 17
School: Alberta Distance Learning Center
Location: British Columbia, Canada
If at first you dont't succeed, try, try again. - programmer's motto.
 
#!/usr/local/bin/perl

$var = 'this is a global variable';
&my_what;

print "$var\n";

sub my_what
{
my $var = 'This var is visible only inside this sub';
print "$var\n";
} If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top