Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by smls (Friar)
on Jan 31, 2013 at 21:38 UTC ( [id://1016397]=note: print w/replies, xml ) Need Help??


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

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?

  • Comment on Re^2: Single Quotes - how to avoid any escape processing?

Replies are listed 'Best First'.
Re^3: Single Quotes - how to avoid any escape processing?
by LanX (Saint) on Jan 31, 2013 at 21:59 UTC
    > 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

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

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

      =P

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-18 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found