--- Naveen Parmar <[EMAIL PROTECTED]> wrote:
> 1) How do you define global & lexical variables in Perl?
> 2) Is the arrow (->) a commonly used Perl operator?
>
> TIA,
> - NP
Global variables may be defined in several different manners.
1. Fully qualified with package name:
package Foo;
$Foo::some_global = 7; # Foo:: is the package in which $some_global resides
This is considered bad because it won't allow you to trap speeling (sic) errors:
$Foo::some_golbal += 12; # whoops!
2. Use the new 'our' keyword. perldoc -f our
package Foo;
use strict;
our $some_global = 7;
This is okay, because when used in conjunction with the 'strict' pragma, you can catch
typos at
compile time:
package Foo;
use strict;
our $some_global = 7;
$some_golbal += 12; # won't compile
Note that $some_global is still in package Foo and is the same thing as
$Foo::some_global.
However, I don't care to use the 'our' keyword because that lexically scopes your
global.
Needless to say, lexically scoped globals are just begging for unobvious bugs. Is the
following
legal? Since 'our' is supposed to be lexically scoped, it's not immediately clear:
package Foo;
use strict;
our $some_global = 7;
{
our $some_global += 12;
}
$some_global += 12;
That sets $some_global to 31, even though the inner scope of $some_global isn't the
same as the
outer scope and it's been redeclared. This will confuse many. Trying to remember the
lexical
scope of a global variable is, well, confusing.
Change that to 'my' (which is how you lexically scope a variable, and it sets it to 19:
package Foo;
use strict;
my $some_global = 7;
{
my $some_global += 12;
}
$some_global += 12;
print $some_global;
But if we take off the first 'our' declaration:
package Foo;
use strict;
{
our $some_global += 12;
}
$some_global += 12; # won't compile
print $some_global;
That will throw an exception. For proper namespace globals, use the third method of
declaring a
global.
3. use vars qw/ $foo @bar @baz /; # perldoc vars
package Foo;
use strict;
use vars qw/$some_global/;
$some_global = 7;
$some_golbal += 12; # won't compile
As mentioned earlier, declaring a lexical variable is as simple as using 'my' (see
perldoc -f my):
package Foo;
use strict;
my $lexical = 7;
$lexical += 12;
$lecixal -= 2; # won't compile
All this aside, if you're using globals, you may want to rethink your design. They
can introduce
all sorts of subtle bugs, so they are best avoided. If you must use them, predeclare
them with
'use vars'.
Note that global variables in Perl are actually tied to a particular namespace, so
they are not
truly global in the sense that you might be thinking for other languages. The closest
you can get
to true globals in Perl are the built-in variables which are always assumed to be in
package
main::
In other words:
$_ is the same as $main::_
@_ is the same as @main::_
@INC is the same as @main::INC
As for the arrow notation (->), yes, this is used all the time. You can use this for
dereferencing:
foo ( { bar => 3, baz ="quux" } ); # passing an anonymous hash reference
sub foo
{
my $data = shift;
foreach ( keys %$data )
{
print "key: $_\nvalue: $data->{$_}\n\n";
}
}
You will also see this frequently for method calls:
use strict;
use CGI;
my $query = CGI->new;
my $user = $query->param( 'user' ) || '';
Cheers,
Curtis "Ovid" Poe
=====
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A
__________________________________________________
Do You Yahoo!?
Great stuff seeking new owners in Yahoo! Auctions!
http://auctions.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]