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??

G'day Leudwinus,

"I am still trying to wrap my head around ..."

You're using terms — pointer, address, memory location — which I suspect you've picked up from one or more other languages. You are then attempting to apply those terms to Perl, assuming they have the same meaning. I think this may be the source of your problems.

I'd suggest the first thing to do would be to look at perlintro; in particular, the "Perl variable types" section. At the end of that section you'll find the gentlest of introductions to references with a list of other links to more information; I'd suggest checking out perlreftut first.

Consider the following:

$ perl -E ' my $x = 5; say $x; my $y = \$x; say $y; say $$y; $$y += 3; say $x; ' 5 SCALAR(0x60008a1c8) 5 8
  • $x has the value 5.
  • $y is assigned a reference to $x.
  • $y has the value SCALAR(0x60008a1c8).
  • Dereferencing $y (with $$y) gives you back $x.
  • Incrementing $$y by 3 is the same as incrementing $x by 3.
  • $x now has the value 8.

You can reference and dereference to great depths if you want; as in this exaggerated example:

$ perl -E 'my $x = 5; say $x; my $y = \\\\$x; $$$$$y += 3; say $x' 5 8

The construct \(...), where ... is some list, evaluates to a list of references to each element of the list:

$ perl -E 'say for \(qw{1 2 3})' SCALAR(0x60008a730) SCALAR(0x60008a7d8) SCALAR(0x60008a748)

You can take references to other data types:

$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \@x; say $y' 1 2 3 ARRAY(0x60008a8e8)

and dereference them:

$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \@x; say $y; say for + @$y' 1 2 3 ARRAY(0x60008a828) 1 2 3

You can take references to references:

$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \\@x; say $y' 1 2 3 REF(0x600003e80)

and dereference them one level at a time:

$ perl -E 'my @x = qw{1 2 3}; say for @x; my $y = \\@x; say $y; say $$ +y; say for @$$y' 1 2 3 REF(0x600003e80) ARRAY(0x60008a868) 1 2 3

I suggest you play around with examples like these to get a better understanding of how all of this works.

Also note that I didn't use, or indeed need, terms such as pointer, address or memory location.

You used strict and warnings in your OP which is very good. I suggest you do the same with oneliners. Here's a common alias I use; you might want to set up something similar for yourself (although, perhaps, one a little less involved).

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'

That will pick up things like this:

$ perl -E '$x =5' $ perle '$x =5' Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) ... $ perl -E 'my $x = 5; say @$x' $ perle 'my $x = 5; say @$x' Can't use string ("5") as an ARRAY ref while "strict refs" in use ...

— Ken


In reply to Re: Pointers and References by kcott
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 having an uproarious good time at the Monastery: (3)
As of 2021-06-13 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (55 votes). Check out past polls.

    Notices?