Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Answer: Calling a method within a double-quoted string?

by btrott (Parson)
on Jun 10, 2000 at 04:15 UTC ( #17474=categorized answer: print w/replies, xml ) Need Help??

Q&A > object-oriented programming > Calling a method within a double-quoted string? - Answer contributed by btrott

For the DBI case, the correct answer is definitely placeholders.

However, in the general case, consider Interpolation. It's pretty nifty. It lets you define "arbitrary interpolation semantics".

From its man page:

For example, you can say use Interpolation money => \&commify_with_dollar_sign, E => 'eval', placename => 'ucwords'; And then you can write these: print "3 + 4 = $E{3+4}"; # Prints ``3 + 4 = 7'' $SALARY = 57500; print "The salary is $money{$SALARY}"; # Prints ``The salary is $57,500.00''
For your need, you could use this:
use vars qw/$dbh/; use DBI; $dbh = DBI->connect('baz', 'foo', 'bar', 'Oracle'); use Interpolation quoteit => sub { $dbh->quote(@_) }; print "select * from foo where bar = $quoteit{baz}";
Which is quite handy. Still, in your particular case, placeholders are better.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
NodeReaper recites The Raven over the P.A. again

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2017-07-21 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (318 votes). Check out past polls.