Welcome to WebmasterWorld Guest from 54.167.46.29

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Beginner's question on coding with perl

     
7:18 pm on Jan 20, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 10, 2004
posts:171
votes: 0


I've been programming in half a dozen languages for some time now and I recently picked up perl. I have no formal training in perl so forgive my ignorance if this question sounds stupid. My question concerns the use of "my" when defining variables. In all the perl programs I've written I never used "my" for anything. I assume it's a way of defining local variables that are not available outside the local environment. What is the purpose of defining local variables like this? And... should I be doing this?
7:26 pm on Jan 20, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2003
posts:408
votes: 0


Try googling:

defining local variables my

You will find some good information on how to use 'my' and why to use it.

7:29 pm on Jan 20, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 8, 2004
posts:1679
votes: 0


Once you waste a few good hours debugging your software only to find out that bug was due to local variable with the same name as global, thus masking global var's value that you actually needed, you will appreciate necessity to always "use strict" in Perl, and therefore use "my" to declare variables explicitly.
7:34 pm on Jan 20, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 10, 2004
posts:171
votes: 0


I guess that answers my question!
4:11 pm on Jan 21, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 30, 2003
posts:428
votes: 0



always "use strict" in Perl, and therefore use "my" to declare variables explicitly

If there were one thing every perl newbie should learn, that is it.

Sean

4:14 pm on Jan 21, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 18, 2003
posts:1925
votes: 0


use strict
and
my
saves hours of development time on daily basis :)
6:26 pm on Jan 24, 2005 (gmt 0)

Preferred Member

joined:Apr 13, 2001
posts:372
votes: 0


Pick up the Oreilly Programming Perl book and look at the scoping functions in chapter 29 which lists all the functions. That chapter alone has pushed me far into PERL usage.
3:21 am on Feb 1, 2005 (gmt 0)

Full Member

10+ Year Member

joined:July 23, 2003
posts:227
votes: 0


Definitely strongly agree with "use strict".

Also, "use warnings" (or perl -W) and provide prototypes for your subroutines.

10:52 pm on Feb 1, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 10, 2004
posts:171
votes: 0


I've got <i>Programming Perl</i> third edition. I'll read chapter 29 tonight. I'm almost afraid to become enlightened because I'll have to go and rewrite all my scripts. I'd better get started.
11:15 pm on Feb 1, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 18, 2003
posts:1925
votes: 0


Actually, once you learn a few perl tricks, you will love it! And you will write scripts much faster. There will be no way back. Perl helps me every day!