Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: Pointers and References

by Leudwinus (Scribe)
on Nov 25, 2020 at 15:13 UTC ( #11124199=note: print w/replies, xml ) Need Help??

in reply to Re: Pointers and References
in thread Pointers and References

So, in answer to question 1: no, it contains a reference (which might be an address of something, but you don't {and shouldn't} need to know what).

Got it. Thanks for that clarification!

$ in Perl as a sigil means "give me a scalar value". In answer to 2: $$ means give me the scalar referred to by the scalar xxx. Because you can assign values to a scalar when you use $$ in an assignment you are assigning to the scalar referred to by the scalar. No pointers to be seen here.

Ok, I think I understand what you are saying. Taking your explanation one step further, I was wondering if I could use a "triple dollar sign" ($$$). Not that this would be idiomatic or anything but apparently you can:

my $variable = 22; my $pointer = \$variable; say "The address of \$variable, which contains the value $variable,"; say "is $pointer"; $$pointer = 25; say "Look at that! \$variable now equals $variable"; my $double_pointer = \$pointer; $$$double_pointer = 50; say "Look at that! \$variable now equals $variable";
which results in
The address of $variable, which contains the value 22, is SCALAR(0x801e64540) Look at that! $variable now equals 25 $double_pointer = REF(0x801e644b0) Look at that! $variable now equals 50
...Perl doesn't need "output parameters" because it can return multiple values from a sub.

I didn't know that! That is very cool!

References in Perl are most useful in building interesting structures which are a mixture of arrays, hashes and scalar values.

I guess I just need to keep re-reading the excellent documentation and trying this out. I implicitly understand the power of references but still struggling with applying them.

Replies are listed 'Best First'.
Re^3: Pointers and References
by GrandFather (Saint) on Nov 25, 2020 at 20:10 UTC

    Please, in the context of Perl at least, expunge "pointer" from your head. Perl references are simply not pointers. Yes, there are pointers under the hood, but thinking of them as pointers will just make your brain hurt more.

    Yes, you can nest scalar references, but it is extraordinarily unusual to do so. Again, don't think about pointers. In other languages it can be useful to use multiple levels of indirection. In Perl you can end up with convoluted data structures containing a wild mix of references to nested data of different types, but that seldom turns into nested references to a simple scalar.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124199]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2023-05-28 14:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found