frankus has asked for the wisdom of the Perl Monks concerning the following question:

I used to be a C coder using pointers to make my code cool, scary and most of all quick.
I stoopidly assumed references would do the same trick.
It took 5 hours of effort to say reference and not pointer.
They're cool they know what type they are they can do nice stuff
But I've now been told they slow things a little in Perl...

Is someone pulling my leg?

This is prose and a serious question
  • Comment on Whats a da pointer? I know what my (p)references are.

Replies are listed 'Best First'.
Re: Whats a da pointer? I know what my (p)references are.
by Corion (Patriarch) on Jul 18, 2000 at 18:32 UTC

    Pointers (in C) and references (in Perl) are mostly the same, and they can be used for speeding up stuff in the same manner in both languages. A reference allows you to pass data to a subroutine without copying that data into separate memory - this is where you can gain speed if your data is big and does not need to be copied.

    References can also be used for most of the scary stuff that comes from the good ole time when an int was a pointer and a pointer was an int (and with 32-bit machines, this time has returned) - you can have references to subroutines etc. - and also here, it might be faster to call a reference to a subroutine instead of going through a map or some other way of decision making.

    About the only thing that can't be done with references is C-style pointer arithmetic, but that's mostly needed for string manipulation - and Perl has its own set of commands, operators and functions for string manipulation derived from utilities better suited at string manipulation than C :).

    DISCLAIMER: The author has never written a line of C and refuses to take any responsibility for failure of analysis and existence of working code before optimizing stuff for any dimension. Always benchmark !
Re: Whats a da pointer? I know what my (p)references are.
by atl (Pilgrim) on Jul 19, 2000 at 14:05 UTC
    Beeing someone who has written quite some lines of C code I'd say this description as a whole is correct, although I never thought of pointers and references as beeing almost the same. But that's more a philosophical point of view. When it comes to the machine level, the general statement holds.

    Concerning the performance issue, the correct answer is: it depends. If you have to walk through a long inheritance tree to check whether or not one object has been derived from the other or not, this is going to cost something. But then again, you are using features (type safety) that aren't available for pointers.

    The point is, just as with memomry management, that you save a lot of time for coding and debugging, and you trade that for a (often marginal) degradation in performance.

    Hey, my first writeup here. Don't judge to harsh ... ;-))

    Cheers

    Andreas