Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

my $a and our $a

by sandyago (Initiate)
on Jan 06, 2016 at 06:08 UTC ( [id://1152043]=perlquestion: print w/replies, xml ) Need Help??

sandyago has asked for the wisdom of the Perl Monks concerning the following question:

Could you please explain my and our with an simple example???

Replies are listed 'Best First'.
Re: my $a and our $a
by Athanasius (Archbishop) on Jan 06, 2016 at 06:53 UTC

    Hello sandyago,

    I think this is well explained in Ovid’s tutorial 'our' is not 'my'. Read that through a few times, study it, then come back if you need clarification.

    BTW, $a is a poor choice of variable name here, as $a and $b are special (global) variables used by sort. It’ll be less confusing to use something like $x instead.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: my $a and our $a
by Marshall (Canon) on Jan 06, 2016 at 07:15 UTC
    First NEVER use $a or $b as a program variable as these have special meaning to Perl in terms of sorting.

    Almost all of your variables should be "my whatever"...that means 99.9%. These are lexically scoped variables within a single compilation unit. The addresses of these variables are not fixed, they are allocated on the stack. The same name can be reused again and again within different loops. "my $name" in one loop is not the same as "my $name" in another loop.

    An "our" variable is different. This is like a C static. This variable is global and goes into a special symbol table. These variables can be accessed by other modules (there is no way to do that with a "my" variable).

    "our" variables should be used very sparingly and for good reasons.

Re: my $a and our $a
by vinoth.ree (Monsignor) on Jan 06, 2016 at 08:00 UTC

     my is a way to declare, none package variables, which are private,non-global variables separate from any package. So that the variable cannot be accessed in the form of $PackageName::variable

    our variables are, package variables, global variables and not private, they can be accessed outside the package with the qualified namespace, as $PackageName::variable

    Declaring a variable with our allows you to predeclare variables in order to use them under use strict without getting typo warnings or compile-time errors. Since Perl 5.6, it has replaced the obsolete use vars, which was only file-scoped, and not lexically scoped as is our.

    For example, the formal, qualified name for variable $x inside package main is $main::x. Declaring our $x allows you to use the bare $x variable without penalty (i.e., without a resulting error), in the scope of the declaration, when the script uses use strict or use strict "vars". The scope might be one, or two, or more packages, or one small block.

    Example:
    use strict; for (1 ..2){ # Both variables are lexically scoped to the block. our ($o); # Belongs to 'main' package. my ($m); # Does not belong to a package. # The variables differ with respect to newness. $o ++; $m ++; print __PACKAGE__, " >> o=$o m=$m\n"; # $m is always 1. # The package has changed, but we still have direct, # unqualified access to both variables, because the # lexical scope has not changed. package Fubb; print __PACKAGE__, " >> o=$o m=$m\n"; } # The our() and my() variables differ with respect to privacy. # We can still access the variable declared with our(), provided # that we fully qualify its name, but the variable declared # with my() is unavailable. print __PACKAGE__, " >> main::o=$main::o\n"; # 5 print __PACKAGE__, " >> main::m=$main::m\n"; # Undefined. # Attempts to access the variables directly won't compile. # print __PACKAGE__, " >> o=$o\n"; # print __PACKAGE__, " >> m=$m\n"; # Variables declared with use vars() are like those declared # with our(): belong to a package; not private; and not new. # However, their scoping is package-based rather than lexical. for (1 .. 9){ use vars qw($uv); $uv ++; } # Even though we are outside the lexical scope where the # use vars() variable was declared, we have direct access # because the package has not changed. print __PACKAGE__, " >> uv=$uv\n"; # And we can access it from another package. package Bubb; print __PACKAGE__, " >> main::uv=$main::uv\n";

    All is well. I learn by answering your questions...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1152043]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found