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

See the here-doc notation in Quote and Quote like Operators:

my $string = <<'END';# only END alone on a line can be the delimiter I can say whatever I want here, even ' " \ $var @Hi, or END END print $string;

Edit: note that a final "\n" will be included, so you may have to chomp your string to remove it.

  • Comment on Re: text has single and double quotes and anychar. how to assign this text?
  • Download Code

Replies are listed 'Best First'.
Re^2: text has single and double quotes and anychar. how to assign this text?
by rsFalse (Chaplain) on Aug 20, 2015 at 13:27 UTC
    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}';

      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

      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".