Connecting Tech Pros Worldwide Help | Site Map

Difference between $::variable and $variable

Member
 
Join Date: Jan 2007
Posts: 55
#1: Oct 19 '07
I want to know the difference between,

1 . Declarations : $::varible and $varible

2 . Calling of functions :
$::functions_output .= ::flatten_list($item[1]); and
$function_output = flatten_list($item[1]);
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Oct 19 '07

re: Difference between $::variable and $variable


The "::" is used to differentiate namespaces. In the absense of a package name before ::, it points to 'main', which is the perl program you initiated by name. I can see no reason to use that syntax. As far as I know, there is no difference between these two lines:

$::functions_output .= ::flatten_list($item[1]);
$function_output = flatten_list($item[1]);

Some test code:

Expand|Select|Wrap|Line Numbers
  1. $foo  = 'test';
  2. print $main::foo,"\n";
  3. print $::foo,"\n"; # same as above
  4. $foo .= ::foo();
  5. print $foo;
  6. sub foo {
  7.     return 'foo';
  8. }     
  9.  
the above code will not work if the "strict" pragma is used, making it particularly useless as far as current standards of perl programing are concerned.
Reply