Ah, after a bit of time, I realized the interplay here is more subtle than I was originally thinking.

My general rule is that, when loading a module conditionally, you have to work to avoid relying upon compile-time magic. The most obvious case of this would be to avoid code like:

if( $threads ) { require threads::shared; threads::shared->import(); share @foo; # No parens used }

Because the "share @foo;" line would cause a compile-time error (if you did 'use strict;') even when that line would never be run.

An even more glaring case is the compile-time magic of the \& sub prototype that allows:

use Exception::Spiffy 'catch'; catch { ... };

For conditional usage, you'd need to do it more like:

require Exception::Spiffy; Exception::Spiffy->import('catch'); &catch( sub { ... } );

But there is a more subtle interaction with the \[$@%] prototype of threads::shared::share(). For example, this code will likely work:

if( $threads ) { require threads::shared; threads::shared->import(); share( \@foo ); }

But it also might not! If some module you loaded for whatever reason loaded something that loaded something that happened to load threads::shared, then that code would fail to compile, complaining:

Type of arg 1 to threads::shared::share must be one of [$@%] (not refe +rence constructor)

So, to be sure your code will work, you need to write it as:

if( $threads ) { require threads::shared; threads::shared->import(); &share( \@foo ); # The ampersand is important }

- tye        


In reply to Re^5: Problem conditionally making variables shared if using threads (amp) by tye
in thread Problem conditionally making variables shared if using threads by billgdev

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.