Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Using array refs to interpolate a function itno a string?

by chorg (Monk)
on Dec 13, 2000 at 20:01 UTC ( [id://46431]=perlquestion: print w/replies, xml ) Need Help??

chorg has asked for the wisdom of the Perl Monks concerning the following question:

Hey Howdy y'all,

I want to interpolate a function result into a string.

# f($x) returns a value # I want to print this string print "Listen f($x), I'll call the organ bank if you dont stop";
Now, I've seen a way to do this using anonymous array refs, but I can't remember where. Anyone? I think it's somewhere in the Camel, but my frenzied flipping has netted me no success. Anyone?
_______________________________________________
"Intelligence is a tool used achieve goals, however goals are not always chosen wisely..."

Replies are listed 'Best First'.
Re: Using array refs to interpolate a function itno a string?
by chipmunk (Parson) on Dec 13, 2000 at 20:17 UTC
    Here's what you're looking for: print "Listen @{[ f($x) ]}, I'll call the organ bank if you don't stop"; The @{} gets you interpolation, and the [ ] creates an array ref that is dereferenced by @{}.

    You can also use a scalar ref, but it doesn't look quite as nice: print "Listen ${\( f($x) )}, I'll call the organ bank if you don't stop"; Another nice way to get interpolation is with MJD's Interpolation module, which uses a tied hash that returns the key: print "Listen $P{ f($x) }, I'll call the organ bank if you don't stop"; The nice thing about this module is that the tied hash can do formatting on the key before returning it, like adding commas to numbers.

Re: Using array refs to interpolate a function itno a string?
by jptxs (Curate) on Dec 13, 2000 at 20:09 UTC
    print takes a list so you could make life easy by:
    use strict; use warnings; sub f { $_[0] = $_[0] . '_modified' } my $x = 'value'; print 'Here is the modified value ', f($x), '. Print is a list, eh?', + "\n\n"; __END__ jptxs:/home/jptxs $ perl functionPrint Here is the modified value value_modified. Print is a list, eh? jptxs:/home/jptxs $

    Can't say I've ever used array refs to interpolate functions into strings - I always do something like the above. But I'm sure some other monk would know.

    "A man's maturity -- consists in having found again the seriousness on +e had as a child, at play." --Nietzsch +e
Re: Using array refs to interpolate a function itno a string?
by kilinrax (Deacon) on Dec 13, 2000 at 20:11 UTC
    Try wrapping the function in '@{[ ... ]}':
    perl -e 'print qq(time: @{[scalar localtime]});'
Re: Using array refs to interpolate a function itno a string?
by Dominus (Parson) on Dec 14, 2000 at 09:10 UTC
    An alternative solution that is cooler in some ways is available with the Interpolation module that you can get from CPAN.
Re: Using array refs to interpolate a function itno a string?
by repson (Chaplain) on Dec 14, 2000 at 10:59 UTC
    Personally I don't think interpolation is the solution here.
    I think interpolation is mostly useful where it makes a string clearer:
    print "Hello " . $name . ".\n"; # not bad print "Hello ", $name, ".\n"; # hope no-one set $, print "Hello $name.\n"; # nice and clear print "Hello " . f($x) . ".\n"; # this is usually what I like print "Hello ", f($x), ".\n"; # has the advantage of print a list +of return values from the function without needing join (which is goo +d and bad) print "Hello @{[ f($x) ]}.\n"; # feel free to sit and stare inexper +ienced Perlers and maintainers
    In the first case interpolation is nice, but in the second a list or concatenation is a little clearer. And anyway the early stages of complimation seperate variables from strings (look using `perl -MO=Deparse foo.pl` if you don't belive me).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://46431]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found