what is the meaning of $$var in perl

pvinodp
pvinodp used Ask the Experts™
on
what is the meaning of $$var in perl,,
i have used only $var or my $var=shift etc..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014
Top Expert 2015
Commented:
perldoc perlref
...
       Using References
...
       1.  Anywhere you'd put an identifier (or chain of identifiers) as part
           of a variable or subroutine name, you can replace the identifier
           with a simple scalar variable containing a reference of the correct
           type:

               $bar = $$scalarref;
               push(@$arrayref, $filename);
               $$arrayref[0] = "January";
               $$hashref{"KEY"} = "VALUE";
               &$coderef(1,2,3);
               print $globref "output\n";

           It's important to understand that we are specifically not
           dereferencing $arrayref[0] or $hashref{"KEY"} there.  The
           dereference of the scalar variable happens before it does any key
           lookups.  Anything more complicated than a simple scalar variable
           must use methods 2 or 3 below.  However, a "simple scalar" includes
           an identifier that itself uses method 1 recursively.  Therefore,
           the following prints "howdy".

               $refrefref = \\\"howdy";
               print $$$$refrefref;

       2.  Anywhere you'd put an identifier (or chain of identifiers) as part
           of a variable or subroutine name, you can replace the identifier
           with a BLOCK returning a reference of the correct type.  In other
           words, the previous examples could be written like this:

               $bar = ${$scalarref};
               push(@{$arrayref}, $filename);
               ${$arrayref}[0] = "January";
               ${$hashref}{"KEY"} = "VALUE";
               &{$coderef}(1,2,3);
               $globref->print("output\n");  # iff IO::Handle is loaded
1.5 Symbolic References

Normally, a construct such as $$var indicates that $var is a reference variable, and the programmer expects this expression to return the value that was pointed to by $var when the references were taken.

What if $var is not a reference variable at all? Instead of complaining loudly, Perl checks to see whether $var contains a string. If so, it uses that string as a regular variable name and messes around with this variable! Consider the following:
$x = 10; $var = "x"; $$var = 30; # Modifies $x to 30 , because $var is a symbolic # reference !

When evaluating $$var , Perl first checks to see whether $var is a reference, which it is not; it's a string. Perl then decides to give the expression one more chance: it treats $var 's contents as a variable identifier ( $x ). The example hence ends up modifying $x to 30.

It is important to note that symbolic references work only for global variables, not for those marked private using my .

Symbolic references work equally well for arrays and hashes also:
$var = "x"; @$var = (1, 2, 3); # Sets @x to the enumerated list on the right

Note that the symbol used before $var dictates the type of variable to access: $$var is equivalent to $x , and @ $var is equivalent to saying @ x .

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial