For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2006 > my, my...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author my, my...
Jorge Almeida

2006-12-15, 7:59 am

I thought I already knew a few things about Perl, but I just found out I
don't:
#!/usr/bin/perl -w
use strict;
use diagnostics;
my $a="27";
doit();
sub doit{
print "$a\n";
}

The output of this program is:
27

Just what I would expect if line 4 had "our" instead of "my".
What am I missing?
TIA.

--
Jorge Almeida
Charles K. Clarkson

2006-12-15, 7:59 am

Jorge Almeida <mailto:jalmeida@math.ist.utl.pt> wrote:

: #!/usr/bin/perl -w
: use strict;
: use diagnostics;
: my $a="27";
: doit();
: sub doit{
: print "$a\n";
: }
:
: The output of this program is:
: 27
:
: Just what I would expect if line 4 had "our" instead of "my".
: What am I missing?

$a, in this case, is a file-scoped lexical. It is available
anywhere in the file. Sometimes a file scoped lexical is called
a global variable, though that can be confusing because global
has other meanings as well.

It is usually best to avoid using variables which are not
local to a sub routine inside a sub routine, though there are
cases where this it is desirable and good programming practice.


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

http://www.clarksonenergyhomes.com/

Don't tread on my bandwidth. Trim your posts.

John W. Krahn

2006-12-15, 7:59 am

Jorge Almeida wrote:
> I thought I already knew a few things about Perl, but I just found out I
> don't:
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;
> my $a="27";
> doit();
> sub doit{
> print "$a\n";
> }
>
> The output of this program is:
> 27
>
> Just what I would expect if line 4 had "our" instead of "my".
> What am I missing?


my() and our() have the same scoping so they will behave the same. my()
variables are scoped from the point they are declared up to the end of the
current file/block. Since your variable is not inside a block it is visible
throughout the rest of the file. By the way, you shouldn't use $a or $b as
they are "special" variables.

perldoc -f sort
perldoc perlvar



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Jeff Pang

2006-12-15, 7:59 am


>I thought I already knew a few things about Perl, but I just found out I
>don't:
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;
> my $a="27";
> doit();
> sub doit{
> print "$a\n";
> }
>
>The output of this program is:
>27
>
>Just what I would expect if line 4 had "our" instead of "my".
>What am I missing?


What are you missing?You can write it as your way with no problem usually.
But don't do this under mod_perl,otherwise you'll get lost really.

You may write them like:

use strict;
my $ab=27; # don't use $a,$b as variable's name since both $a and $b are used by perl's 'sort' function by default.
doit($ab); # pass $ab to the subroutine distinctly

sub doit {
my $s = shift;
print "$s\n";
}


--
Books below translated by me to Chinese.
Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/
Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/
Jorge Almeida

2006-12-15, 7:59 am

On Fri, 15 Dec 2006, Jeff Pang wrote:

>
> What are you missing?You can write it as your way with no problem usually.
> But don't do this under mod_perl,otherwise you'll get lost really.
>
> You may write them like:
>
> use strict;
> my $ab=27; # don't use $a,$b as variable's name since both $a and $b are used by perl's 'sort' function by default.
> doit($ab); # pass $ab to the subroutine distinctly
>
> sub doit {
> my $s = shift;
> print "$s\n";
> }
>

My only problem was that the variable in the subroutine was not declared
with my nor our, not on purpose. I didn't know that you could do that
when using strict. I always use my or our, so I hadn't encounter this
behaviour yet.

Thank you.
--
Jorge Almeida
Adriano Ferreira

2006-12-15, 7:59 am

On 12/15/06, John W. Krahn <krahnj@telus.net> wrote:
> Jorge Almeida wrote:
>
> my() and our() have the same scoping so they will behave the same. my()
> variables are scoped from the point they are declared up to the end of the
> current file/block. Since your variable is not inside a block it is visible
> throughout the rest of the file. By the way, you shouldn't use $a or $b as
> they are "special" variables.


More differences will show up if you use packages. The my() variables
will have the scoping from the point they are declared to the end of
the package. The our() variables have the same scoping, but may be
reached by other packages.

$ perl -e 'use strict; use warnings;
package foo;
our $VAR = 0;
sub p_var { print "$VAR\n" }
package main;
$foo::VAR = 1; # this is the same variable above
foo::p_var;
'
1

$ perl -e 'use strict; use warnings;
package foo;
my $VAR = 0;
sub p_var { print "$VAR\n" }
package main;
$foo::VAR = 1; # this is NOT the same variable above
foo::p_var;
'
Name "foo::VAR" used only once: possible typo at -e line 6.
0

There is more to tell the truth. For instance, the lexical variables
created by my() can't be localized. You may read about it at:

perldoc -f my
perldoc -f our
perldoc perlsub (section "Private variables via my()")
Zentara

2006-12-15, 7:59 am

On Fri, 15 Dec 2006 10:51:09 +0000 (WET), jalmeida@math.ist.utl.pt
(Jorge Almeida) wrote:

>I thought I already knew a few things about Perl, but I just found out I
>don't:
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;
> my $a="27";
> doit();
> sub doit{
> print "$a\n";
> }
>
>The output of this program is:
>27
>
>Just what I would expect if line 4 had "our" instead of "my".
>What am I missing?


The program section called main is automatically defined.
This might be helpful;

#!/usr/bin/perl
use warnings;
use strict;

our $a= 27; #comment one of these out
my $a = 27; # and see difference

doit();
doit1();

sub doit{
print "$a\n";
}

sub doit1{
my $a = 42;
print "$a\n";
print $main::a,"\n";
}

__END__




--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Adriano Ferreira

2006-12-15, 7:59 am

On 12/15/06, Jorge Almeida <jalmeida@math.ist.utl.pt> wrote:
> On Fri, 15 Dec 2006, Adriano Ferreira wrote:
>
>
> But they can be redefined with "my" inside a routine belonging to the
> same package, right?


Yes. While another "our" declaration in the same file (even within
blocks or subs) will cause a warning about redeclaring -- because
there is only one package variable. Lexical variables are different
and may correspond to the lexical scopes you open up.


> Thanks.
> --
> Jorge Almeida
>

Jose Pinto

2006-12-15, 6:59 pm

my was to define a var like local.

If you have a function and if you define like local, outside that =
function this var was empty (does not exist).

BUT if you define a var like local on main script (and that is your =
case), your var will not work like a local var (it will be seen by all =
functions).

Regards

Jose Pinto

-----Original Message-----
From: Jorge Almeida [mailto:jalmeida@math.ist.utl.pt]=20
Sent: sexta-feira, 15 de Dezembro de 2006 10:51
To: beginners@perl.org
Subject: my, my...

I thought I already knew a few things about Perl, but I just found out I
don't:
#!/usr/bin/perl -w
use strict;
use diagnostics;
my $a=3D"27";
doit();
sub doit{
print "$a\n";
}

The output of this program is:
27

Just what I would expect if line 4 had "our" instead of "my".
What am I missing?
TIA.

--=20
Jorge Almeida

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Chad Perrin

2006-12-15, 6:59 pm

On Fri, Dec 15, 2006 at 10:51:09AM +0000, Jorge Almeida wrote:
> I thought I already knew a few things about Perl, but I just found out I
> don't:
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;
> my $a="27";
> doit();
> sub doit{
> print "$a\n";
> }
>
> The output of this program is:
> 27
>
> Just what I would expect if line 4 had "our" instead of "my".
> What am I missing?
> TIA.


Simply put:

my() is used to declare lexical variables. These variables apply in
their current scope, including nested scopes. They do not apply in
enclosing scopes.

This occasionally seems confusing to people coming from languages that
treat certain types of local (but not lexical) variables as "lexical",
even though they technically aren't. Python comes to mind, with
variable scope that is sometimes defined as lexical by default even
though technically it's a special case of dynamic local scope that just
usually acts very similarly to lexical scoping.

Hope that helps.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
print substr("Just another Perl hacker", 0, -2);
Jeff Pang

2006-12-17, 6:58 pm


>My only problem was that the variable in the subroutine was not declared
>with my nor our, not on purpose. I didn't know that you could do that
>when using strict. I always use my or our, so I hadn't encounter this
>behaviour yet.
>


There is a good article about Perl's variable scope.
I think you maybe need to take some time to read it seriously:

http://perl.plover.com/FAQs/Namespaces.html.en

--
Books below translated by me to Chinese.
Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/
Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/
Jorge Almeida

2006-12-17, 6:58 pm

On Sun, 17 Dec 2006, Jeff Pang wrote:

>
> There is a good article about Perl's variable scope.
> I think you maybe need to take some time to read it seriously:
>
> http://perl.plover.com/FAQs/Namespaces.html.en
>

Good link.
Thanks.
--
Jorge Almeida
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com