user3446683
user3446683

Reputation: 69

How to enclose a string within quotes using Perl?

I have a string like this : abc,bcd,def but I need this string to be enclosed by single quotes, like this 'abc,bcd,def'.

How can i get that using perl??

Thanks!

Upvotes: 0

Views: 753

Answers (2)

Hubert Schölnast
Hubert Schölnast

Reputation: 8527

Try it this way:

my $stringWithSingelQuotes = "'abc,bcd,def'";
print $stringWithSingelQuotes;

Result: 
'abc,bcd,def'

or

my $stringWithSingelQuotes = "'".'abc,bcd,def'."'";
print $stringWithSingelQuotes;

Result: 
'abc,bcd,def'

you can even mix singel and double quotes:

my $stringWithMixedQuotes = "'".'abc,bcd,def'.'"';
print $stringWithMixedQuotes;

Result: 
'abc,bcd,def"

Upvotes: 2

Matteo
Matteo

Reputation: 14940

Assuming you have the string to be quoted in a variable (e.g., $string) you can just use the double quotes to build the new one (e.g., "'$string'").

use strict;
use warnings;

my $string = 'abc,bcd,def';
my $newstring = "'$string'";

print "$newstring\n";

If you want assign the value to a variable you can use double quotes

    my $string = "'abc,bcd,def'";

or q{}

    my $string = q{'abc,bcd,def'};

Upvotes: 3

Related Questions