Your "code" to compare is inside double quotes so things like $_[0] are going to be interpolated in before the value is passed in to cmpthese. You'd want to use single quotes, or better provide a code ref.
sub cmpstr {
my( $arg ) = @_;
cmpthese( -2,
'string multiplier', sub { print strMulti( $arg ) }.
'string concat', sub { print strConcat( $arg ) },
);
}
That aside be aware that your two subs do different things; strConcat will return a single string consisting of three copies of the argument, while strMulti will return three copies of the argument as a list. I misread as pointed out below (I was seeing parens in there ($_[0])x3 that aren't there . . .).
Edit: Thirdly, don't prefix subroutine calls with an ampersand. There's rare corner cases where you need to do that (disabling prototypes) but in general it's unneeded and makes your code harder to read (and looks like you're still writing perl4).
The cake is a lie.
The cake is a lie.
The cake is a lie.
-
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.
|