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

Discussion in 'Perl Misc' started by Edi Weitz, Oct 17, 2003.

  1. Edi Weitz

    Edi Weitz Guest

    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.
    Edi Weitz, Oct 17, 2003
    #1
    1. Advertising

  2. Edi Weitz wrote:
    >

    <snip>
    >
    > 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
    Michael P. Broida, Oct 17, 2003
    #2
    1. Advertising

  3. Edi Weitz

    Jay Tilton Guest

    Edi Weitz <> wrote:

    : 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.
    Jay Tilton, Oct 18, 2003
    #3
  4. Edi Weitz

    Edi Weitz Guest

    On Fri, 17 Oct 2003 23:34:35 GMT, (Jay Tilton) wrote:

    > 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.
    Edi Weitz, Oct 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    926
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    646
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    458
  4. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    368
    Fredrik Lundh
    Aug 13, 2008
  5. Xah Lee
    Replies:
    0
    Views:
    2,221
    Xah Lee
    Feb 26, 2009
Loading...

Share This Page