This reference stuff carried over, of course, from C. Both are useful in certain situations, you simply must recognize them. Really, the speed issue should be an afterthought. When you pass by reference, you are passing almost the original object to the function. You can use this variable as if it had been called from the encapsulating function. When you return, the variable will remain what you set it in the calling function since any alteration in the function will have effect on the original variable. When you pass by reference, you are actually passing the variable's pointer address in memory.

Now, when you pass by value, you are passing the actual variable's value when you called the function from the caller. This value is taken and a personal copy for that function and that function only is created. Any changes made to that copy do not affect any other variable in the system.

Now, to your issue. In C, some variables are actually smaller than passing a pointer (reference), so the passing by value was actually preferable over passing by reference! But in Perl, there are no data types- even strings are held in the same variable. A typical string (keeping it simple) will easily eat up more than 16 or 32 bits (a typical pointer size). While internally, the pass is made with a pointer, perl will create a dynamic memory copy at some point before it continues in your code. If you do not need another copy to work on, pass by reference. In C, there is no penalty for pointers (references). In Perl, the penalty can become substantial- especially if you are derefencing in a loop- especially in a main or very important loop! In all cases, you should

Lastly, comes the speed check. As others have mentioned, Perl dereferencing takes time, so a Benchmark would be a good way to fly. On the other hand, sometimes it's obvious or irrelevant. Is the function only called once or twice in the program? Then the testing really won't reveal any mind-boggling speed increase. If the function is mission-critical and you really need that speed increase, go for it. Anyway, if you're using Perl, you're really not writing any OpenGL routines, so you should be all set with whatever you choose. If the shoe fits, wear it. One help may be to make sure that the user always has something to see. That means piping data straight to the user as it comes up (or in buffers thereof). That way, even if your program isn't fast, it'll seem fast!
Q: Could a variable assignment bypass the perl dereference mechanism, as in:
my $var=$bar->foo(); #deref here &var; #yet another deref?
? thanx.
AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.

In reply to Re: passing refs to subs by AgentM
in thread passing refs to subs by jptxs

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.