-1

Our:

our $ref = "test";
my $var = "ref";
print "$$var";   #the output will be test

My:

my $ref = "test";
my $var = "ref";
print "$$var\n"; #the output is blank
Joel Berger
  • 20,180
  • 5
  • 49
  • 104
  • The referenced question is different in that it is a general question about lexical and package variables. This question asks specifically about soft references. – Borodin Mar 19 '13 at 17:22
  • 2
    This poorly titled question about soft references is not answered by the above referenced question. Answer: Soft References always reference a variable in a Package/Global namespace. Lexical (my) variables need not apply. Soft References are also considered harmful and are blocked by `use strict vars`. Most times you are tempted to use Soft References you should consider using a hash instead. – tjd Mar 19 '13 at 17:45
  • 1
    Ok, the question is different and I guess therefore I MIGHT support reopening. The problem is that this is generally seen as bad form, the OP should not be attempting this (should be using strict) until (s)he can answer this question IMO. – Joel Berger Mar 19 '13 at 17:46
  • Bad form: Reopening a question or closing it as a pseudo-duplicate? – tjd Mar 19 '13 at 17:56
  • 2
    Your question answers your question perfectly. – ikegami Mar 19 '13 at 18:32
  • 1
    @tjd, sorry my comment was badly phrased. Symbolic references are considered bad form. Rather than teach the OP to shoot him/herself in the foot, I advocate leaving this question closed. People who are likely to benefit from symbolic references already know how to appropriately use package variables. – Joel Berger Mar 19 '13 at 19:23
  • @JoelBerger: That a construct is bad practice is a dreadful reason to close a question in the first place, and hence a dreadful reason not to reopen it. The correct answer should be given with a proviso, and that cannot be done for a closed question. – Borodin Mar 19 '13 at 19:58
  • @Borodin, agreed. I would not now vote to close it, now that we have figured out what the question was intended to ask. As is I'm not inclined to reopen it. If it is reopened, so be it. – Joel Berger Mar 19 '13 at 20:10
  • @JoelBerger: That makes little sense. Should this question be open or closed? – Borodin Mar 19 '13 at 21:06
  • 1
    My point about the validity of the question is in fact moot. It should be closed as an exact duplicate of http://stackoverflow.com/questions/2338369/why-cant-i-use-a-perl-variables-value-to-access-a-lexical-variable-name and several others. – Joel Berger Mar 19 '13 at 21:20

1 Answers1

1

The difference is that our will set up a package variable, while my sets up a lexical variable.

What that means is that variables declared with our can be accessed outside of the current scope.

use strict;
use warnings;
{
  our $g = 5;
}
{
  print our($g), "\n";
}
5

While lexical variables exist only in a given scope.

{
  # stored in this block
  my $l = 5;
  {
    # accessible from this block
    print $l, "\n";
    $l = 6;

    # new variable stored in this lower block
    my $l = 7;
  }
  print $l, "\n";
}
{
  # yet another new variable
  print my($l), "\n";
}
5
6
Use of uninitialized value $a in print at -e line 1.



When you were trying to access $$var you were using a symbolic reference; which only work on package/global variables:

our $g = 5;

my $symbolic_ref = 'g';

{
  no strict 'refs';
  # these are symbolic refs
  print $$symbolic_ref, "\n";
  print ${ *{$symbolic_ref} }, "\n";
  print ${ *{$symbolic_ref}{SCALAR} }, "\n";
}

# access it through the magic %:: variable
print ${ $::{$symbolic_ref} }, "\n";
print ${ $main::{$symbolic_ref} }, "\n";
5
5
5
5
5

That is opposed to regular references.

my $v = 5;

my $ref = \$v;

print $$ref, "\n";
print ${ $ref }, "\n";
5
5

There is rarely any reason to use a symbolic reference.

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129