Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Perl doesn't have "pointers". Sure, under the hood they are everywhere, but it's not useful to think about pointers in Perl. The reason we talk about references is that there is some extraordinarily useful under the hood baggage associated with references. Perl uses reference counted memory management. A reference to something else is one type of data that a scalar variable can contain. You can have references to pretty much anything and you can use ref to tell you what you have a reference to. That sums up to, you hardly ever need to think about memory management in Perl and most things "just work".

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).

$ 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.

A more useful example is:

use warnings; use strict; use v5.10; do { my $variable = 22; my $refVar = \$variable; $$refVar = 25; say "Look at that! \$variable now equals $variable"; my $two = 2; my ($sum, $diff) = sum_and_diff(5, $two); say "the sum of 5 and $two is $sum"; say "and the difference is $diff"; }; sub sum_and_diff { my ($lhs, $rhs) = @_; return $lhs + $rhs, $lhs - $rhs; }

Prints:

Look at that! $variable now equals 25 the sum of 5 and 2 is 7 and the difference is 3

Useful because it is a reminder that Perl doesn't need "output parameters" because it can return multiple values from a sub. References in Perl are most useful in building interesting structures which are a mixture of arrays, hashes and scalar values.

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

In reply to Re: Pointers and References by GrandFather
in thread Pointers and References by Leudwinus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-06-13 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?