Re: function within qq{}




Quoth "Uri Guttman" <uri@xxxxxxxxxxxxxxx>:
"JB" == John Bokma <john@xxxxxxxxxxxxxxx> writes:

JB> cate <catebekensail@xxxxxxxxx> writes:
>> I know you can do this, I just can't find it.
>> How do you call a sub within a qq{} construct
>>
>> qq{The common term for H2O is chem("H2O").}
>>
>> Thank you
>>
>> (As soon is this post is complete... I'll find it) :-)

JB> perl -e '
JB> sub chem {
JB> my $term = shift;
JB> return "*$term*";
JB> }
JB> print qq{The common term for H2O is ${\( chem("H2O") ) }};
JB> '

JB> use @{...} for list expressions, ${...} for scalar.

partly incorrect.

perl -le 'sub context {return "array" if wantarray} ; print "scalar
${\context()}"'
scalar array

that is an obscure bug that has been around a long time. the scalar form
still calls the sub in list context.

It's not a bug. \($x, $y, $z) is perfectly valid, and returns a list of
refs. (This also means that \(@a) is not the same as \@a: one of the few
cases where parens *are* necessary to denote a list.)

Ben

.