There are two different concepts of references in Perl. There are two
different kinds of variables in Perl.

By far the more useful and important kind of reference to know is the hard
reference (the ones you deal with when you use \$var, as well as anonymous
structures like [1, 2, 3] or {a=>1, b=>2}). You need these, for example, if
you want deep data structures like a hash containing a list in one of its
elements; it can't really contain a list but it can have a list reference as
one of its values. Here's a short intro to them:
http://perldoc.perl.org/perlreftut.html

The other kind of reference in Perl is the soft, or symbolic, reference.
Those have to do with * and names -- and it's nearly always a bad idea to
use them. Here's a series of rants explaining why, if you're interested:
http://perl.plover.com/varvarname.html


Most of the time you will be using lexical variables -- the ones that get
declared with "my". These obey scoping rules for visibility, and (unless you
employ heavy machinery like PadWalker), cannot be accessed from a different
lexical scope, or when hidden. So:

{
  my $foo = 42;
  {
    my $foo = 54;
    # $foo #1 can't be accessed here, though it still exists.
  }
  # $foo #2 can't be accessed here, and will have been destroyed if nobody
took a reference to it.
}
# $foo #1 can't be accessed here, and will have been destroyed if nobody
took a reference to it.

When I say "can't be accessed", I mean that the literal "$foo" doesn't refer
to that variable. If someone had arranged for $bar = \$foo, then yes, you
can deref and access bar.

The other kind of variable are package globals. There is only one variable
called $main::x ever -- if you tried hiding as with the lexical $foo above,
it won't work -- the memory would be overwritten.

The package's symbol table doesn't actually contain simple scalars, it
contains things called typeglobs. These are strange beasts and you usually
don't need to worry about them unless you want to master things like manual
symbol export. In general though, they have slots for a scalar, an array,
and a hash -- and a few others! -- which might shed some light on what
happened in your latest example, which you sent before this reply went out.

If you want more, the gory details are actually available in the standard
documentation but are a little scattered: you want perlmod and perlref (and
possibly perlsub).

Running your code with "use strict" and then really understanding the error
messages you get and the changes you have to do to make it still behave this
way can also be useful.

On Sun, Apr 17, 2011 at 4:52 PM, Ronen Angluster <[email protected]>wrote:

> Hi all!
> happy passover!
>
> i've been wondering and could not find anyone who could explain
> to me what is the difference between \$var to *var...
> consider the following:
> *$x=1;*
> *$z=\$x;*
> *$y=*x;*
> *print "$y\n$z\n";*
> would print:
> **main::x*
> *SCALAR(0x1951ac8)*
>
> but if we:* print "$$y\n$$z\n";*
> the output would be:
> *1*
> *1*
>
> so, why use 1 against the other? what does it mean that the pointer is to
> the class and
> not to the memory space of the variable?
>
>
> Ronen
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <[email protected]>
http://gaal.livejournal.com/
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to