Reputation: 11
I am using this code below in Perl What is the scope of the variable and what does it mean when you say $:: while declaring a variable?
use strict;
use warnings;
use Readonly;
my Readonly::Scalar $::variable_name => 'value';
Thanks in Advance!
Upvotes: 1
Views: 81
Reputation: 126722
$::variable_name
is identical to $main::variable_name
, and you should use the latter for clarity.
You meant either
Readonly::Scalar $main::variable_name => 'value';
which has global scope since $main::variable_name
can be accessed from anywhere
or
Readonly::Scalar my $variable_name => 'value';
which has the same scope as a normal my
declaration.
Since package variables are generally despised, the latter is preferable; although I realise that it may be useful to be able to access constant values globally
Upvotes: 5
Reputation: 4709
From perlfaq7:
If you know your package, you can just mention it explicitly, as in
$Some_Pack::var
. Note that the notation$::var
is not the dynamic$var
in the current package, but rather the one in the "main" package, as though you had written$main::var
.
use vars '$var';
local $var = "global";
my $var = "lexical";
print "lexical is $var\n";
print "global is $main::var\n";
Upvotes: 5