On Aug 11, 10:41 pm, [EMAIL PROTECTED] (Tmc) wrote:
> Being new to perl can someone explain why my is in form of variables
> and what is the purpose?

You should have a read of: http://perl.plover.com/FAQs/Namespaces.html

Basically, there are two kinds of variables in Perl.  Global
variables, which simply exist without being declared, and lexical
variables, which are declared with "my".  When you use a variable that
hasn't been declared as a lexical, Perl assumes you're talking about a
global.  This creates hard-to-find bugs when you accidentally typo a
variable name.

If you type:
use strict;
at the top of your program, Perl will force you to "fully qualify" all
of your global variables.  That is, to name them with their package
name in addition to the actual variable name.  So the variable $foo in
package main must actually be called $main::foo.   Therefore, it is
standard best practice to 'use strict;' and to only use lexical
variables (ie, declared with my).  That way, if you accidentally typo
a variable name, Perl can no longer assume you're talking about a
different (global) variable, because that variable would have to be
fully qualified, and Perl can therefore give you a compile-time error,
so you can find your bug.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to