develooper Front page | perl.perl6.language.data | Postings from September 2000

Re: RFC 328 (v1) Single quotes don't interpolate \' and \\

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
September 29, 2000 04:03
Subject:
Re: RFC 328 (v1) Single quotes don't interpolate \' and \\
Message ID:
20000929120304.B51277@plum.flirble.org
On Fri, Sep 29, 2000 at 09:58:45AM +0100, Hildo Biersma wrote:
>  
> > =head1 ABSTRACT
> > 
> > Remove all interpolation within single quotes and the C<q()> operator, to
> > make single quotes 100% shell-like. C<\> rather than C<\\> gives a single
> > backslash; use double quotes or C<q()> if you need a single quote in your
> > string.
> 
> Single-quoted strings need to be able to contain single quotes, which
> means some escape mechanism is required.

What do you mean by "need"? Strings need to be able to contain single
quotes, but single quotes are not the only way to build a string. Single
and double quotes don't generate a different fundamental type; a double
quoted string with no variable interpolation also generates a string
constant.  Having an escape system for single quotes makes it easy to
embed a ' inside a string without worrying about what else would mean
something special to double quote interpolation. But makes it necessary
to remember the escape system, which does differ from the "" system.

$  perl -lw
print "\z"
Unrecognized escape \z passed through at - line 1.
z
$  perl -lw
print '\z'
\z


> So,

>   $line = 'This string contains both single \' and double " quotes';
> needs an escape.  It would be silly to have an escape character
> different from the backslash that everything else uses.

I'm proposing *no* escape character. Not a different escape character,
but a lack of escape character. It doesn't change how strings are
stored internally, just how the language parser sees one particular
construction that generates a string.

> We also need to be able to do:
>   $line = 'This single-quoted string ends on a backslash \';
> Oops.  Which is why that is spelled:
>   $line = 'This single-quoted string ends on a backslash \\';
> Again, the same escape mechanism is used.
> 
> Conceivably, we could say that \\ in the middle of a single-quoted
> string is not escaped, i.e. it means two backslashes, that that way lies
> madness.

Agree

> So, I'd say, leave single quotes as they are.  The two escapes are
> useful and required, as I've demonstrated above.

They are required to build a string from single quotes. But I can write the
above as

$line = "This string contains both single \' and double \" quotes';
$line = "This single-quoted string ends on a backlash \\";


I don't agree that the escape is absolutely required (because there are other
  ways to build the string)
I agree that the escapes are useful, but I also feel that they are confusing
  and inconsistent w.r.t. how backslashes work in double quoted strings,
  because backslash followed by non-special-character differs.

The precise behaviour of backslash in single quote string syntax in perl is
unique to perl. Double quote backslash behaviour is very close to C,
double quote interpolation behaviour is close to unix shells.

An alternative to either of the above could be to borrow SQL or BASIC's
syntax:

$line = 'This string contains both single '' and double " quotes';
$line = 'This single-quoted string ends on a backslash \';


but I don't think that this is a good idea.


It might be an idea to allow the perl5 behaviour or the strict no-escape
switched by a pragma. But this also is confusing, unless it's part of a
more general

use perl5;

I've also discovered that I missed a reference - rfc226
"Selective interpolation in single quotish context."


Nicholas Clark

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About