Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Making escape (\) ordinary in double quotes

by sg (Pilgrim)
on Apr 09, 2013 at 04:36 UTC ( #1027641=perlquestion: print w/replies, xml ) Need Help??
sg has asked for the wisdom of the Perl Monks concerning the following question:


Rather than writing:

{ my $foo = "Ignore the \\slash, expand $boo."; }

is it possible to do something like:

{ # do something to get perl # to treat '\' as an ordinary character # so that one may write: my $foo = "Ignore the \slash, expand $boo."; }


Replies are listed 'Best First'.
Re: Making escape (\) ordinary in double quotes
by AnomalousMonk (Canon) on Apr 09, 2013 at 09:34 UTC

    sprintf does not alter the interpolation behavior of a double-quoted string, but it does provide all the interpolation capability of double-quoting – and more! See the many format specifiers in the docs.

    >perl -wMstrict -le "my $foo = 'Foo'; my $bar = 'Bar'; ;; my $x = sprintf 'ignore \slash, expand %s, %s.', $foo, $bar; print qq{'$x'}; " 'ignore \slash, expand Foo, Bar.'
Re: Making escape (\) ordinary in double quotes
by 2teez (Priest) on Apr 09, 2013 at 06:10 UTC

    You can use q/STRING/

     my $foo  =  q{Ignore the \slash, expand} . $boo;

    You can check Quote-Like-Operators

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Making escape (\) ordinary in double quotes
by Rahul6990 (Beadle) on Apr 09, 2013 at 05:31 UTC
    You can concatenate the variable along with the string containing '\' in (') single quote. Ex

    my $foo  =  "Ignore the \slash, expand '.$boo;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027641]
Approved by 2teez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2016-12-11 00:48 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (168 votes). Check out past polls.