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

.



Relevant Pages

  • Tie and Lexical Filehandles or IO::File?
    ... to tie lexically scoped handles or IO::File objects. ... read perltie and perlref, browsed the Perl FAQ and Googled both the groups ... sub PRINT ... an undef scalar appears to make no difference. ...
    (comp.lang.perl.misc)
  • Re: Tie and Lexical Filehandles or IO::File?
    ... beginning to grasp the basics of Perl OOP, ... to tie lexically scoped handles or IO::File objects. ... sub PRINT ... an undef scalar appears to make no difference. ...
    (comp.lang.perl.misc)
  • weird behavior when passing hashes to subroutine
    ... I was working on a program today and made a sub routine that I ... was passing a few scalars and also a hash to as arguments. ... my $bar = 'soda is yummy!'; ... and found that if I reverse the order so the scalar is passed before ...
    (comp.lang.perl.misc)
  • Re: MIME coding & flags for sending a .csv or Excel file (Question)
    ... Is the scalar function always use to return values in a sub? ... You should really use 'my' instead or 'our' to declare variables ...
    (perl.beginners)
  • Re: Extract Numeric values from string
    ... VG> I have string which contain numbers... ... the algorithm uses that to find the next space or the next ... sub fast_get_number ... return unless scalar @matches; ...
    (comp.lang.perl.misc)