Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Single Quotes - how to avoid any escape processing?

by LanX (Canon)
on Jan 31, 2013 at 20:04 UTC ( #1016386=note: print w/ replies, xml ) Need Help??


in reply to Single Quotes - how to avoid any escape processing?

Welcome to the world of escaping! :)

If you have an end delimiter and you want to allow this end delimiter to appear literally you need to escape it, hence \' => '

But if you're escaping, you're also needing an escape character, which has also to be escaped when used literally, hence \\ => \

And thats it, nothing else needed within the single quotes family of q{} with a free choice of end delimiters.

And now, if you think that perl sucks try emacs lisp, which only supports double quotes! In elisp regexes suffer from a ugly disease called slasheritis.

Luckily you can use heredocs, because there is no delimiter that need to be escaped. And if you don't like the newlines then chomp:

$foo = <<'__HERE__'; \\some.address.or.whatever\subdir\ __HERE__ chomp $foo; print ">", $foo ,"<"; # >\\some.address.or.whatever\subdir +\<

I suppose you don't wanna put multiple path in a long heredoc?

So why don't you use a character like / as placeholder?

sub s2b { # slash to backslash (my $str = shift) =~ tr#/#\\#; return $str; } my $path = s2b '//some.address.or.whatever/subdir/';

Just don't blame Perl for M$ decision to use an escape character for separating paths. =)

Cheers Rolf

update

fixed code for s2b()

footnotes

) you could read them into a hash defined by preceded hash keys.

use Data::Dump; sub parsepath { my $str=shift; return split /\s+/,$str; } my %path = parsepath <<'__HERE__'; install \\some.address.or.whatever\subdir\ deinstall \\other.address.or.whatever\subdir\ __HERE__ dd \%path;

==>

{ deinstall => "\\\\other.address.or.whatever\\subdir\\", install => "\\\\some.address.or.whatever\\subdir\\", }

) yes, it insults the eyes!!!


Comment on Re: Single Quotes - how to avoid any escape processing?
Select or Download Code
Re^2: Single Quotes - how to avoid any escape processing?
by smls (Friar) on Jan 31, 2013 at 21:38 UTC
    If you have an end delimiter and you want to allow this end delimiter to appear literally you need to escape it

    But why is this always considered a necessary feature for a quoting delimiter, trumping all other considerations like avoiding headaches like the OP describes?

    For a language that has so many different quoting delimiters as Perl does, I would have expected there to be at least one that would simply drop the pedantic "must allow each and every Unicode character to appear in the string literal" requirement and do no escaping at all.
    With a use-case like file paths, it's quite unlikely that not being able to include single quotes (or a similar special-char delimiter) would become a problem.

    In addition, I'm not convinced that introducing a separate escape character (in this case, backslash) is technically necessary for implementing single-quoted strings that are supposed to allow single-quotes to appear inside them.
    What would prevent the usage of the delimiting character as its own escape character? I.e. two consecutive single-quotes would not terminate a single-quoted string literal, they would be interpreted as one single-quote.
    I don't see any reason for ever wanting to put two string literals right next to each other without space or comma in between, so it would not create any syntactical ambiguity.
    Or am I missing something?

      > But why is this always considered a necessary feature for a quoting delimiter, trumping all other considerations like avoiding headaches like the OP describes?

      I didn't say it's a necessary, I explained the motivation "If ... you want to allow ...".

      > What would prevent the usage of the delimiting character as its own escape character? I.e. two consecutive single-quotes would not terminate a single-quoted string literal

      IIRC VB (or VB-Script) does something like this.

      > I don't see any reason for ever wanting to put two string literals right next to each other without space or comma in between, so it would not create any syntactical ambiguity.

      empty string?

      > Or am I missing something?

      Perl has already a very complex syntax, adding new extra DWIM exception doesn't make it easier.

      What might be easy and meaningful for you can be line noise or just complexity horror for someone else.

      For instance see this discussion about the differences between Perl and JS in handling the closing bracket in regex-character classes: Regex: Char-class equivalent for [^].

      So in short: Rules have to be kept as simple as possible and backslashing is a well-established notation.

      Moritz already mentioned a new way in Perl6, which sounds reasonably simple, so help is on the way for the next version.

      EDIT: And for the records: The OP might hate them but I LOVE HERE-DOCS!!!

      Cheers Rolf

        Love... here-docs?!
        You must explain your reasoning behind this calculated insanity!

        =P

        > empty string?

        I don't think that would create a problem, since the search for the end delimiter would begin right after the start delimiter, not including it.
        The regex for matching such a single-quoted string would effectively be:
          /'(?:''|[^'])*'/
        ...which would match the empty string case just fine.

        > What's easy for you might be the horror for someone else.

        Well, one way to compare & contrast DWIM "horrors" would be to consider the likelihood of unsuspecting users "falling into the trap" while just going about their normal routine programming.

        Many who learn Perl come to colloquially know single-quoted strings as "like normal strings, but without escaping and interpolating". Then they go about using them, with perfect results, until at some point they are bitten by weird bugs like the OP described.
        While writing something like
          $path = '\\aaa\bbb';
        a non-experienced user who knows single-quoted strings as "strings without escaping", won't even consider that the double-slashes might cause unintended behavior.

        One the other hand, the same user would probably not write something like
          $contraction = 'Don''t';
        unless they were consciously experimenting how single-quoted strings behave in special cases. It would raise a red flag, because it challenges what they know about how start and end delimiters for string literals work.

        In both cases, people will not know how exactly things work until they are taught.
        But in the second case, they will know about not knowing something when it matters, whereas in the first case it will catch them by surprise later in the form of bugs.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016386]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-11-25 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (160 votes), past polls