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

All, How do I convert a double quoted string to a single quoted string ? A trivial example is converting : $str = "Some String" to $str = 'Some String' ? Rgds AR
  • Comment on Converting double quoted string to single quoted string

Replies are listed 'Best First'.
Re: Converting double quoted string to single quoted string
by DamnDirtyApe (Curate) on Aug 11, 2002 at 07:02 UTC

    Regexp::Common has a mechanism that simplifies this greatly.

    #! /usr/bin/perl use strict ; use warnings ; $|++ ; use Regexp::Common qw( delimited ) ; my $str = do { local $/; <DATA> } ; print "\nBefore\n-----\n", $str ; $str =~ s/$RE{delimited}{-delim=>'"'}{-keep}/'$3'/gs ; print "\nAfter\n-----\n", $str ; exit ; __DATA__ "Welcome to my humble abode," I said, spreading my arms. "You mean, you live here? In the monastery?" "Absolutely. Come, I'll show you about. Everything you need is located right here, from tutorials to friendly advice."

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Converting double quoted string to single quoted string
by BrowserUk (Patriarch) on Aug 11, 2002 at 12:24 UTC

    It might be worth pointing out that, given yout trivial example, $str contains identical information regardless of which form of quotes are used.

    That is to say

    $str1 = "Some String"; $str2 = 'Some String'; if( $str1 eq $str2 ) { print ">>$str1<< is identical to >>$str2<<\n"; }

    Gives:

    >>Some String<< is identical to >>Some String<<

    So, without resorting to something esoteric like source filtering, the only way to change the quotes (in your example), is to do so manually with your editor!

    So changing how the strings are quoted only becomes meaningful if the strings you are programmically changing contain embedded quotes. eg.

    $str1 = "'Some String'"; $str2 = '"Some String"'; if( $str1 ne $str2 ) { print ">>$str1<< is different to >>$str2<<\n"; } ... >>'Some String' is different to >>"Some String"<<

    Therefore, your question only really makes sense if the source of the contents of the strings is external to your program as DamnDirtyApe demonstrated.

    Is this what you meant?

Re: Converting double quoted string to single quoted string
by graff (Chancellor) on Aug 11, 2002 at 13:28 UTC
    If you have some text data (one or more text files) containing double-quote characters, and you just want to make sure that all double-quotes are replaced by single quotes, this will do it:
    while (<>) { tr/\"/\'/; print; }
    (That could be done as a one-liner on a command line with perl -pe 'tr/\x22/\x27/' in.txt > out.txt -- using hex numbers to avoid having the quotation marks misinterpreted by the command-line shell.)

    I would not recommend using this approach on a text file that contains a perl script. Consider what would happen with a code snippet like the following:

    $name = "my name"; print "---$/ What is $name ? \t I won't tell.\n---";
    In that form, with double quotes, it produces:
    --- What is my name ? I won't tell. ---
    If we convert all double-quotes to single-quotes, it won't run because of a syntax error -- until we figure out that we have to escape the apostrophe in won't with a backslash. But when that's fixed, the output we see is:
    ---$/ What is $name ? \t I won't tell.\n---

    Single quotes in perl scripts are used to assign literal string values, without interpolating variables or special characters like \n, \t, etc.