CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
the my function

 



rockcone01
Novice

Jun 5, 2015, 11:20 AM

Post #1 of 5 (1132 views)
the my function Can't Post

I want to ask that about my function, such as:
my $_;
my @_;
etc.
I'm still not clear of it. Does this mean that the defined variable/array after my, will only be effective in the curly parenthesis {}? Otherwise, why don't we just use $_, @_, etc. to define a variable? I'm a beginner so I have many uncertainties, thanks.


aaron_baugher
Novice

Jun 5, 2015, 3:55 PM

Post #2 of 5 (1128 views)
Re: [rockcone01] the my function [In reply to] Can't Post

'my' declares a variable to be lexically local to the enclosing block (curly brackets), or if it's not inside a block, the enclosing file. When the block/file ends, the variable goes away.

Since the variables you're talking about here are global, they already exist, so my makes a separate copy of them which is local to the enclosing block. When the block ends, any further references to the variable refer to the global one. For instance:


Code
$_ = 3; 
print $_; # prints 3
{
my $_ = 4;
print $_; # prints 4
}
print $_; # prints 3



rockcone01
Novice

Jun 5, 2015, 4:01 PM

Post #3 of 5 (1127 views)
Re: [aaron_baugher] the my function [In reply to] Can't Post

Thanks. so if I don't want the variable goes away, I should not set the my before?
For other local/intermediate variables, is it better to set my? because it may takes space/memory?


aaron_baugher
Novice

Jun 5, 2015, 5:31 PM

Post #4 of 5 (1126 views)
Re: [rockcone01] the my function [In reply to] Can't Post

Right. If you want to keep using the same copy of a global variable like $_, don't create a local copy with my, just use it.

For regular, non-global variables, you usually want to create them with my within the narrowest possible context. It limits them lexically, which helps prevent bugs and waste of memory. It also lets you 'use strict', which will alert you if you misspell a variable.


shawnhcorey
Enthusiast


Jun 6, 2015, 5:59 AM

Post #5 of 5 (1121 views)
Re: [rockcone01] the my function [In reply to] Can't Post


In Reply To
Thanks. so if I don't want the variable goes away, I should not set the my before?
For other local/intermediate variables, is it better to set my? because it may takes space/memory?


The complete to `my` is `our`. This tells the compiler that the variable is one in the current package. See perldoc -f our. `our` variables are persistent; they never go away.

Another way to get persistent variables is to use their fully qualified names. For example, this:

Code
package SomeName; 
our $var;

$var = 1;


is the same as:

Code
$SomeName::var = 1;


See perldoc perlmod for details.

Fully qualified variables are often used for configuration variables. For example, `Data::Dumper` uses `$Data::Dumper::Sortkeys` to control whether it should sort the keys of any hash it displays. See perldoc Data::Dumper.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives