Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi Ken,

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.

Guilty as charged! I was trying to replicate in Perl the following program from a C tutorial on functions and pointers:

#include <stdio.h> int sum_and_diff (int a, int b, int *res) { int sum; sum = a + b; *res = a – b; return sum; } void main (void) { int b = 2; int diff; printf ("The sum of 5 and %d is %d\n", b, sum_and_diff (5, b, &diff)); printf ("The difference of 5 and %d is %d\n", b, diff); }

Perhaps I just need to focus on one language at a time! And thank you for the detailed example and explanation!

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

I'm just reading this now but came to the same conclusion earlier today when I was reading some of the other responses to this thread.

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

Agreed. What I take away from your examples is that when I try to print or output a variable that contains a reference, I can clearly see what that is in reference to. For example:

SCALAR(0x60008a730) --> reference to a scalar ARRAY(0x60008a828) --> reference to an array REF(0x600003e80) --> reference to a reference

This helps you understand how to dereference it:

$$x # dereference reference to scalar (if $x is a scalar) @$x # dereference reference to array (if $x is reference to an array) @$$x # dereference reference to reference of an array

I hope I got that last bit right!

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

Duly noted! And thanks for the alias tip. I will have to park that one away for the time being because as useful as Perl one-liners are, I don't think I'm quite ready to use them that frequently.

Gratias tibi ago
Leudwinus


In reply to Re^2: Pointers and References by Leudwinus
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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-19 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found