3 ms·
> do not need to worry about escaping quotes in strings because of the {} syntax Perl has strong support for that: https://perldoc.perl.org/perlop#Quote-Like-O
by adrianmonk 3y ago
> do not need to worry about escaping quotes in strings because of the {} syntax
Perl has strong support for that: https://perldoc.perl.org/perlop#Quote-Like-Operators https://perldoc.perl.org/perlop#Quote-Like-Operators
You can basically use any character as a delimiter except whitespace.
All of these lines are equivalent:
$str = 'Hello, world.';
$str = q{Hello, world.};
$str = q[Hello, world.];
$str = q(Hello, world.);
$str = q<Hello, world.>;
$str = q|Hello, world.|;
$str = q!Hello, world.!;
$str = q#Hello, world.#;
$str = q%Hello, world.%;
$str = q=Hello, world.=;
$str = q 7Hello, world.7;
Note that when you use a letter or number (actually any character that's legal in an identifier), you need a space after the "q".
You can also do it with other types of quotes, like double quotes (that support variable interpolation). These are equivalent:
$var1 = 'abc'; $var2 = "Value of var1 is $var1.";
$var1 = 'abc'; $var2 = qq[Value of var1 is $var1.];
And you can also do it with regular expression matches:
if (/pattern/) { whatever(); }
if (qr|pattern|}) { whatever(); }
and substitutions:
s/pattern/replacement/;
s|pattern|replacement|;
sed also has something similar:
echo abc | sed -e 's/abc/def/'
echo abc | sed -e 's#abc#def#'
- mdaniel 3y ago$str = q 7Hello, world.7; whew, I would not want to be in the business of lexing perl > sed also has something similar: I also recently learned that one can do the same in the "pattern match" case (not just with `s`) by using a backslash, as in: printf 'a/b/c\nd/e/f\n' | sed -ne '\_a/b/c_p' the man page says "any character" and sure enough, `\ a/b/c p` works also
- wernsey 3y agoSheesh, me neither. How on earth would you create syntax highlighters and other tools for this language?
- yellowapple 3y ago> whew, I would not want to be in the business of lexing perl Hence the saying: the only thing that can parse Perl is Perl :)
- mdaniel 3y agoYour comment made me curious, so I installed the Perl plugin for IJ <https://plugins.jetbrains.com/plugin/7796-perl https://plugins.jetbrains.com/plugin/7796-perl> and sure enough, it parsed that str assignment correctly. My hat's off to them! It also seems <https://github.com/Camelcade/Perl5-IDEA/blob/2023.1.1/plugin/core/grammar/Perl.flex#L353-L358 https://github.com/Camelcade/Perl5-IDEA/blob/2023.1.1/plugin...> that one can have line comments in there, too my $str = q #what is happening here 7Hello, world.7;
- gaudat 3y agoThanks. TIL. I also started to use perl because it lets me write dirty one-liners to transform text on the fly. It's a fresh change from Python and its mandatory whitespace usage.
- Lammy 3y agoRuby has these too https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-Percent+Literals https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.ht...