CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to append single quote to both ends of $ variable

 



steve798
Novice

Dec 8, 2017, 11:25 AM

Post #1 of 3 (7022 views)
how to append single quote to both ends of $ variable Can't Post

I want to use single quotes to both ends of $dataval but getting an error invalid identifier error on $dataval variable. How should I correct the statement below?


Code
my $dataval = $dbh -> prepare(qq(select col1, col2 from sometable)) 
..
...
my $genextract = $dbh->prepare(qq(select col1, col2, col3 from sometable WHERE col2 = "'"$dataval"'")


The query should read like below

Code
select col1, col2, col3 from  sometable WHERE col2 = 'dataval'



steve798
Novice

Dec 8, 2017, 12:34 PM

Post #2 of 3 (7019 views)
Re: [steve798] how to append single quote to both ends of $ variable [In reply to] Can't Post

I used join to concatenate the quote and referenced this $newvar in the query.


Code
my$newvar = join '', "'",$dataval ,"'";



BillKSmith
Veteran

Dec 8, 2017, 1:56 PM

Post #3 of 3 (7013 views)
Re: [steve798] how to append single quote to both ends of $ variable [In reply to] Can't Post

Within the quote-like operators, the quote characters do not have special meaning;


Code
use strict; 
use warnings;
use DBI;
...;
my $dbh;
...;
my $dataval = $dbh->prepare(qq(select col1, col2 from sometable));
...;
...;
my $genextract = $dbh->prepare(
qq(select col1, col2, col3 from sometable WHERE col2 = '$dataval'));


You have a worse problem which I cannot fix without knowing more about your task.

The method 'prepare' returns a 'statement handle'. It does not make sense to try to interpolate a handle as if it were a string.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives