Scope of \Q, \L, \U, etc.

E

Edi Weitz

man perlop says:

\L lowercase till \E
\U uppercase till \E
\E end case modification
\Q quote non-word characters till \E

However, it doesn't seem to be that simple:

edi@bird:~ > perl -le '$a="Aa-"; print "\Q$a\L$a\E$a"'
Aa\-aa\-Aa\-
edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\Q$a\E$a"'
aa-aa\-aa-

These two cases seem to imply that it isn't as simple as "till \E" but
that the scopes of these modifiers can be nested - in the first case
the scope of \Q extendes beyond the \E, in the second case the scope
of \L extends beyond the \E.

But not if we replace \Q with \U:

edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\Q$a\E$a"'
aa-aa\-aa-
edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\U$a\E$a"'
aa-AA-Aa-

Now the \E ends the scope of both \L and \U, the string behind \E
remains unmodified. This makes sense, kind of, because one could argue
that if the scoping rules were as above the result would be something
like

lc($a . uc($a) . $a)

which would be 'aa-aa-aa-', probably not what people expect.

And what about this one?

edi@bird:~ > perl -le '$a="Aa-"; print "\Q$a\L$a\E$a"'
Aa\-aa\-Aa\-
edi@bird:~ > perl -le '$a="Aa-"; print "\U\Q$a\L$a\E$a"'
AA\-aa-Aa-

By placing \U in front of the string the scope of \Q suddenly ends
at \L? How's that?

Am I missing any rules which could explain this behaviour or are we
just looking at implementation bugs?

FWIW, I'm using Perl 5.8.0 but 5.6.1 also behaves like that.

Thanks in advance for your help,
Edi.
 
M

Michael P. Broida

edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\Q$a\E$a"'
aa-aa\-aa-
edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\U$a\E$a"'
aa-AA-Aa-

Now the \E ends the scope of both \L and \U, the string behind \E
remains unmodified.

Since \U and \L are opposites, I think it's more probable
that the \U ends the scope of the \L, and the \E ends the
scope of whatever is the most recent. The \U and \L each
kinda imply the end of the other one.

Mike
 
J

Jay Tilton

: man perlop says:
:
: \L lowercase till \E
: \U uppercase till \E
: \E end case modification
: \Q quote non-word characters till \E
:
: However, it doesn't seem to be that simple:

When investigating how Perl gets its DWIM, squeezing code through the
B::Deparse backend complements the test/hypothesize cycle very well.

: edi@bird:~ > perl -le '$a="Aa-"; print "\Q$a\L$a\E$a"'
: Aa\-aa\-Aa\-

Deparsed: print("\Q$a\L$a\E$a\E");

: edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\Q$a\E$a"'
: aa-aa\-aa-

print("\L$a\Q$a\E$a\E");

: These two cases seem to imply

and B::Deparse confirms

: that it isn't as simple as "till \E" but
: that the scopes of these modifiers can be nested - in the first case
: the scope of \Q extendes beyond the \E, in the second case the scope
: of \L extends beyond the \E.
:
: But not if we replace \Q with \U:
:
: edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\Q$a\E$a"'
: aa-aa\-aa-
: edi@bird:~ > perl -le '$a="Aa-"; print "\L$a\U$a\E$a"'
: aa-AA-Aa-

print("\L$a\E\U$a\E$a");

: Now the \E ends the scope of both \L and \U,

Not quite what's going on, but the conclusion is consistent with
observed behavior. The effect of \L is terminated before the \U begins.

So \Q can nest inside \U or \L,
and \U or \L can nest inside \Q,
but \U or \L cannot nest inside each other--one will be terminated
before the next begins.

: And what about this one?
:
: edi@bird:~ > perl -le '$a="Aa-"; print "\Q$a\L$a\E$a"'
: Aa\-aa\-Aa\-
: edi@bird:~ > perl -le '$a="Aa-"; print "\U\Q$a\L$a\E$a"'
: AA\-aa-Aa-
:
: By placing \U in front of the string the scope of \Q suddenly ends
: at \L? How's that?

That's an interesting one. The deparse is very enlightening.

print("\U\Q$a\E\E\L$a\E$a");

The \U gets terminated before \L since they don't nest,
and since the \Q is inside the \U, they both get terminated.

: Am I missing any rules which could explain this behaviour or are we
: just looking at implementation bugs?

It doesn't look like a bug. I don't see an explanation of what's really
going on in perlop. Maybe it could stand to have a brief note added.
 
E

Edi Weitz

When investigating how Perl gets its DWIM, squeezing code through
the B::Deparse backend complements the test/hypothesize cycle very
well.

Thanks, I didn't know about that one.
I don't see an explanation of what's really going on in perlop.
Maybe it could stand to have a brief note added.

Yes, I think so.

Thanks for your help,
Edi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top