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 Significance of "my" b4 a scalar/array/association..?

 



jaeman
Novice

Sep 28, 2003, 8:08 AM

Post #1 of 6 (474 views)
the Significance of "my" b4 a scalar/array/association..? Can't Post

Hello ppl, i was just wondering what the significance of "my" b4 a scalar/array/association..? i see it alot with scripts that are reading data from a form, is it crutial..?

my $scalar = "what does my mean..";

$scalar = "what does my mean..";

...jaeman
Tongue



davorg
Thaumaturge / Moderator

Sep 28, 2003, 3:14 PM

Post #2 of 6 (470 views)
Re: [jaeman] the Significance of "my" b4 a scalar/array/association..? [In reply to] Can't Post

Did you try reading the documentation for "my"? That might help you.

There's also an article called Coping with Scoping by Mark Jason Dominus which addresses the topic in more detail.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Sep 28, 2003, 6:44 PM

Post #3 of 6 (467 views)
Re: [jaeman] the Significance of "my" b4 a scalar/array/association..? [In reply to] Can't Post

in sort of beginner terms, using strict and "my" forces you to use good coding conventions, using "my" without strict is meaningless as far as I know. And it really helps to spot any errors in a really big script or one that is being coded by many people, like open source scripts where anyone can add sections of code to an existing script. But as far as the value of the variable goes, there is no difference, except in some very technical ways I am not qualifed to explain.
-------------------------------------------------


jaeman
Novice

Sep 28, 2003, 11:29 PM

Post #4 of 6 (464 views)
Re: the Significance of "my" b4 a scalar/array/association..? [In reply to] Can't Post

Thanks for that, i did look over the doc on "my" but i wasn't able to understand it fully, kevinR gave a good easy to understand answer which was what i was after, thanks to all, jaeman
Tongue



davorg
Thaumaturge / Moderator

Sep 29, 2003, 8:13 AM

Post #5 of 6 (461 views)
Re: [KevinR] the Significance of "my" b4 a scalar/array/association..? [In reply to] Can't Post


In Reply To
using "my" without strict is meaningless as far as I know.


Erm.. no. "my" _always_ creates a lexical variable instead of a package variable. And it _always_ controls the scope of the variable created.

As an example. Here's a short program (without "strict") which works completely differently when you use "my".


Code
#!/usr/bin/perl 

foreach $x (1 .. 3) {
@square = ($x, $x * $x);
push @squares, \@square;
}

foreach (@squares) {
print "$_->[0] squared is $_->[1]\n";
}


And a version that uses "my"


Code
#!/usr/bin/perl 

foreach $x (1 .. 3) {
my @square = ($x, $x * $x);
push @squares, \@square;
}

foreach (@squares) {
print "$_->[0] squared is $_->[1]\n";
}


Try running them both. And then read the MJD article that I referenced earlier in this thread.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Sep 29, 2003, 11:50 AM

Post #6 of 6 (459 views)
Re: [davorg] the Significance of "my" b4 a scalar/array/association..? [In reply to] Can't Post

Erm.. no. "my" _always_ creates a lexical variable instead of a package variable. and it _always_ controls the scope of the variable created. Thanks for the clarification Blush
-------------------------------------------------

 
 


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

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