in reply to Re: text has single and double quotes and anychar. how to assign this text?
in thread text has single and double quotes and anychar. how to assign this text?

Thanks. And another question. If I have a variable somewhere inside a string, which I want to interpolate? How can I envelope?
my $str = 'smth'; my $var = '/\Q"'[]{}()/ or ${str}';
  • Comment on Re^2: text has single and double quotes and anychar. how to assign this text?
  • Download Code

Replies are listed 'Best First'.
Re^3: text has single and double quotes and anychar. how to assign this text?
by GotToBTru (Prior) on Aug 20, 2015 at 13:55 UTC

    The interpolation will happen with the heredoc. Or, make up your own convention, something that will not appear in the text itself, and use s///.

    $variable = 'there'; $string = <<END; Put my variable $variable and !here! END print $string . "\n"; $string =~ s/!here!/$variable/; print $string . "\n";

    Output:

    Put my variable there and !here! Put my variable there and there
    Dum Spiro Spero
Re^3: text has single and double quotes and anychar. how to assign this text?
by Eily (Monsignor) on Aug 20, 2015 at 13:56 UTC

    You can use the double-quote-like here-doc format:

    my $string = <<"END"; # Note here I have used double quotes In this string " and ' and all chars work normally, but backslashes mu +st be escaped, and $var is interpolated. END
    If you have interpolation, you'll have to escape your backslashes no matter what (and you'll need to escape other characters depending on your delimiter), there's no "simple" way to have perl interpolate in some places and take the string litteraly in others. So the next best thing might be to use substitution on the litteral string to replace variables by their value, escape all backslashes in your string, or divide it into several parts and use concatenation: my $string = 'Here \ is allowed'.q<Here I can have '>."This interpolates $var".