Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

interpolating a function call in double quotes

12 views
Skip to first unread message

Larry Wall

unread,
Apr 30, 1994, 11:51:04 PM4/30/94
to
Tom's been asking for a way to interpolate function results into a string.
Well, I just discovered this cute little trick in Perl 5:

print "howdy ${\(&dude)}\n";
sub dude { "doody" }

Pity it needs the parens. If you're willing to make the subroutine an
accessory to the crime, you can say:

print "howdy ${&dude}\n";
sub dude { \"doody" }

Hmm, we can move the initial paren over and dump the &.

print "howdy ${\dude()}\n";
sub dude { "doody" }

Or if we predeclare the subroutine, we can get the parens out.

sub dude;
print "howdy ${\dude}\n";
sub dude { "doody" }

And then there's the corresponding list interpolation:

print "howdy @{[&dude]}\n";
sub dude { "doody", "time" }

I think I like it.

Larry

0 new messages