Perl parameters are passed by reference - see perlsub. However, when you do

my ($param1, $param2) = @_

$param1 and $param2 get a copy of each element in @_, effectively turning a pass by reference into a pass by value. If you need to access the original variable passed in rather than a copy of its value, you can use $_[0], $_[1], etc.

But I don't think passing by reference vs. value is much of an issue in the code sample above. Perl "objects" are references in their own right. If you pass $q to your function, then $queue and $q both point to the same object, not a copy of each other.

Additionally, new isn't a key word in Perl. It is a method that must be defined for each class. new Thread::Queue is simply a different spelling for Thread::Queue->new(). There is nothing magical about the name "new" that turns it into a constructor. It acts like a constructor because, by convention, this method returns a blessed reference to some data that it declared and initialized. For more information on Perl's approach to objects, please see perltoot and perlobj. You may also want to take a look at perlref for a better understanding of references in Perl.

All the above applies to function calls and variables within a thread. In Perl, all variables are, by default, thread local (see thread). Passing references between threads will cause thread death unless the data is explicitly marked as shared - see threadtut and threads::shared for more information.

Best, beth


In reply to Re: creating thread : are parameter objects passed via reference ? by ELISHEVA
in thread creating thread : are parameter objects passed via reference ? by cbrauner

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.