Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Change double quote to single quotes

by rookie_monk (Novice)
on Sep 02, 2010 at 17:32 UTC ( [id://858614]=perlquestion: print w/replies, xml ) Need Help??

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

Good afternoon Perl gurus, I have a string that I pull from an entry widget which has backslashes (which are not literals so I am assuming it's double quoted). I can't figure out how to change the string from a double quoted string to a single quoted string so I can have the backslashes when I do my reg ex. Maybe there is an easier way to do this. Here is an example of what I am trying to do:
my $GetEnt = $ent4->get(); #Which is "test_\1_\2\_3" print "$GetEnt\n"; #This give me some funny symbols for \1 \2 and \3 #I need the string to keep the backslashes #Here's what I tried but it doesnt do what I want my($text2) = "test_\1_\2_\3"; my($text3) = q{'$text2'}; print "$text3\n";
Any help would be greatly appreciated. Thanks in advance. -Paul

Replies are listed 'Best First'.
Re: Change double quote to single quotes
by sundialsvc4 (Abbot) on Sep 02, 2010 at 21:36 UTC

    What I usually do is to construct the string ... and then put it into a statement like: die "The string is '$s'"

    Yup ... the poor program spits out a copy of the string as it actually is, then falls over, stone dead.   (Oops...)   The point is, now you can see for yourself exactly how Perl interpreted the string literal that you just typed in, and you can fiddle with it until it’s what you intended for it to be.

    Because Perl’s handling of strings and interpolation and etc. is so powerful, there are many ways to write “a string constant that is acceptable to the compiler,” that is syntactically valid but not exactly what you meant to say.  I find that this approach is the most reliable way for me to SWIM = Say What I Mean.

    (It goes without saying that you should also use strict and use warnings.)

Re: Change double quote to single quotes
by dasgar (Priest) on Sep 02, 2010 at 17:48 UTC

        my($text2) = "test_\1_\2_\3";

    In the line above, the backslashes are being interpolated since they are in double quotes. To avoid the interpolation, modify it like:

    my($text2) = 'test_\1_\2_\3';  # Single-quotes don't interpolate

    or

    my($text2) = "test_\\1_\\2_\\3"; # Escaping the backslashes

    As for the beginning portion, you might want to use Data::Dumper to verify that $GenEnt is really holding what you think it is. That would be my first step in debugging the problem.

      The 2nd part
      my($text2) = "test_\1_\2_\3";
      is just to test if I could convert from double quoted string to single quoted string. I know how to avoid interpolation, but I dont know how to converted once its interpolated. Thanks for the help. -Paul

        In my opinion, you need to find out where the interpolation is happening and then do something in your code and/or data entry to prevent the interpolation. Otherwise, you're trying to look a string, figure out if anything has been interpolated, find the interpolated characters, and then figure out how to undo/reverse the interpolation. That's a lot of guess work. Perhaps someone has figured out a good method for doing that, but that's above my level and skill knowledge to be able to do.

        I agree with JavaFan in that the interpolation is happening in the code. Meaning that your code either interpolates and then stored the interpolated string into the variable or your code interpolates the variable's content when you use it.

        If you can provide code for the widget (and the 'get' method in particular), perhaps someone can help identify where the interpolation is happening. If the interpolation is happening inside code from a module, then you might need to escape the backslashes (\\) in the data entry, which would interpolate into a single backslash.

Re: Change double quote to single quotes
by JavaFan (Canon) on Sep 02, 2010 at 18:17 UTC
    Good afternoon Perl gurus, I have a string that I pull from an entry widget which has backslashes (which are not literals so I am assuming it's double quoted).
    This doesn't make any sense. The only place where "double quoted" vs. "single quoted" matters is how it appears in the source code (string literals). It's a compiler issue. If you "pull a string from a widget", it's just that. A string. Not a literal. There's no quoting.
      OK, but my problem is that the backslashes are not literally backslashes like how I would like them to be. I would like the string to contain the backslashes. Thanks for your input. -Paul

        In a string value (not a string literal), each backslash is simply a backslash. There are no "literal" vs "special" backslashes in a string value. The problem is that, when interpolating a string into a regex, any backslashes get interpreted as "escaping" backslashes.

        Unfortunately, you chose not to show us any of the code where you interpolate the string into the regex. (The code you did show us has almost nothing to do with your real problem but rather is an attempt to demonstrate what you think the problem might be.)

        When interpolating into a regex, /...$str.../ will interpret the value of $str as a regex (where each backslash escapes the next character, likely changing its meaning). /...\Q$str\E.../ will interpret the value of $str as a plain string. It accomplishes this by doing quotemeta($str). That is, it adds a bunch of backslashes before giving the string to the regex parser.

        If you only want the backslashes to be interpreted as "literal", not the other regex meta characters like dot, star, plus, parens, square brackets, "{1,5}", etc, then just put backslashes only in front of the existing backslashes:

        $str =~ s-\\-\\\\-g; # Change each \ into \\ ... /...$str.../ ...

        - tye        

        I think you want quotemeta at some point.

        E.g. my $GetEnt = quotemeta $ent4->get();

Re: Change double quote to single quotes
by BrimBorium (Friar) on Sep 02, 2010 at 19:52 UTC
    #Here's what I tried but it doesnt do what I want my($text2) = "test_\1_\2_\3"; my($text3) = q{'$text2'}; print "$text3\n";

    ... because your first value gets already interpolated. But I guess you should check what is really returned by the widget with Data::Dumper. Did you also try Entry option -textvariable to avoid get()? e.g.

    $zoom_factor_entry = $draw_bar -> Entry ( "-width" => 2, "-textvariable" => \$zoom_step, ) -> pack( "-side" => 'left', "-padx" => 3);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://858614]
Approved by NiJo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-20 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found