Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > function within qq{}

Reply
Thread Tools

function within qq{}

 
 
cate
Guest
Posts: n/a
 
      02-02-2010
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)
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      02-02-2010
cate wrote:
> 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").}


perldoc -q "How do I expand function calls in a string"



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
 
Reply With Quote
 
 
 
 
cate
Guest
Posts: n/a
 
      02-02-2010
On Feb 2, 10:18*am, "John W. Krahn" <some...@example.com> wrote:
> cate wrote:
> > 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").}

>
> perldoc -q "How do I expand function calls in a string"
>
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity. * * * * * * * -- Damian Conway


@{[mysub(1,2,3)]}
good grief. Thanks
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-02-2010
>>>>> "c" == cate <> writes:

c> On Feb 2, 10:18*am, "John W. Krahn" <some...@example.com> wrote:
>> cate wrote:
>> > 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").}

>>
>> perldoc -q "How do I expand function calls in a string"
>>

c> @{[mysub(1,2,3)]}
c> good grief. Thanks

and that is considered a poor construct. i never use it (or its scalar
cousin) nor do i recommend it. when i review code, i downgrade when i
see that used. it is simpler and better to just assign to a variable
before the string and then interpolate it. also it will likely be faster
as you don't need the reference/dereference. also it will only call your
code in a list context (even the scalar form!) which may be a
problem. assigning to your own variable allows you to control the
context as well.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      02-02-2010
cate <> 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)


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

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

See perldoc perlref section Using References, item 4.

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-02-2010
>>>>> "JB" == John Bokma <> writes:

JB> cate <> 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. this is one of the reasons i don't
recommend using this trick. and a trick it is. it isn't really meant to
be used like that but perl's syntax supports interpolating of
dereferenced expressions. the interpolate module supports this in a
better way and so does perl6.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-02-2010
>>>>> "BM" == Ben Morrow <> writes:

BM> Quoth "Uri Guttman" <>:
>> >>>>> "JB" == John Bokma <> writes:

>>

JB> cate <> 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.


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

yes, but the "${\foo()}" idiom looks like it will be in scalar
context. that is the issue. you need to put a scalar() in there. whether
it is a true bug or not, i say this is one reason to not use this
trick. using a variable is simpler, probably faster and safer.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-02-2010
>>>>> "BM" == Ben Morrow <> writes:

BM> Quoth "Uri Guttman" <>:
>> >>>>> "BM" == Ben Morrow <> writes:

>>

BM> It's not a bug. \($x, $y, $z) is perfectly valid, and returns a list of
BM> refs. (This also means that \(@a) is not the same as \@a: one of the few
BM> cases where parens *are* necessary to denote a list.)
>>
>> yes, but the "${\foo()}" idiom looks like it will be in scalar
>> context. that is the issue. you need to put a scalar() in there. whether
>> it is a true bug or not, i say this is one reason to not use this
>> trick. using a variable is simpler, probably faster and safer.


BM> Oh, I agree there. The right answer is of course some sort of template
BM> system: sprintf will do for starters, and there are plenty on CPAN.

and what is wrong with my suggestio of a simple variable assignment
before the string is built? that is all the OP needs.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      02-03-2010
"Uri Guttman" <> writes:

>>>>>> "JB" == John Bokma <> writes:

>
> JB> cate <> 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.


I stand corrected, thanks (Ben too). I haven't used this construct for
ages, don't really like it.

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
 
Reply With Quote
 
Eric Pozharski
Guest
Posts: n/a
 
      02-03-2010
with <> Uri Guttman wrote:
*SKIP*
> yes, but the "${\foo()}" idiom looks like it will be in scalar
> context. that is the issue. you need to put a scalar() in there. whether
> it is a true bug or not, i say this is one reason to not use this
> trick. using a variable is simpler, probably faster and safer.


s/probably/definetely/

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark qw{ cmpthese timethese };

sub abc {
return "abc";
}
sub def {
my $t = "def";
return "*$t";
}
my( $x, $y );

cmpthese timethese -5, {
code00 => sub { $x = "@{[ abc ]}"; },
code01 => sub { $x = "${\( def )}"; },
code02 => sub { $y = abc; $x = "$y"; },
};

__END__
Benchmark: running code00, code01, code02 for at least 5 CPU seconds...
code00: 6 wallclock secs ( 5.17 usr + 0.00 sys = 5.17 CPU) @ 559916.63/s (n=2894769)
code01: 7 wallclock secs ( 5.09 usr + 0.00 sys = 5.09 CPU) @ 667528.49/s (n=3397720)
code02: 7 wallclock secs ( 5.27 usr + 0.00 sys = 5.27 CPU) @ 941641.56/s (n=4962451)
Rate code00 code01 code02
code00 559917/s -- -16% -41%
code01 667528/s 19% -- -29%
code02 941642/s 68% 41% --

p.s. "faster" is strawman, anyway


--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a function be called within a function ? Peter Moscatt Python 4 04-18-2005 09:33 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
pointing to a member function/method within another function uli C++ 3 05-13-2004 11:02 AM
adding attributes to a function, from within the function Fernando Rodriguez Python 6 11-03-2003 05:04 PM
generator function within a generator function doesn't execute? TheDustbustr Python 1 07-25-2003 10:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57