trying out escape characters

Discussion in 'Perl' started by Griff, Aug 3, 2004.

  1. Griff

    Griff Guest

    i'm using ActivePerl, version 5.6.1

    My question is:

    How come

    print "\ "

    prints a backslash followed by a space

    but

    print "\z"

    prints the z without a backslash ?

    As neither \z nor \+space are Perl escape sequences as far as I know,
    i would expect them both to be processed identically ?

    Thanks,

    Griff
    Griff, Aug 3, 2004
    #1
    1. Advertising

  2. Griff

    Guest

    (Griff) wrote in message news:<>...

    > How come
    >
    > print "\ "
    >
    > prints a backslash followed by a space


    It does not. It prints a space.

    > print "\z"
    >
    > prints the z without a backslash ?


    Yeah, but it emits a warning.

    > As neither \z nor \+space are Perl escape sequences as far as I know,
    > i would expect them both to be processed identically ?


    If you have warnings switched off then they appear to be processed
    identically.

    However, you should not have warnings switched off.

    Any non-letter preceded by a backslash represents that literal
    character, always.

    Any letter preceded by a backslash is potentially a Perl escape
    sequence. There may be no \z yet but there may be one in future.
    Hense you get a warning. Until and unless \z is defined it is
    interpreted as a literal z. If this offends you then you can always
    promote the warning to an error.

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.
    , Aug 4, 2004
    #2
    1. Advertising

  3. Griff

    Griff Guest

    (1) apologies for starting a thread in non-existent NG, I will use
    comp.lang.perl.misc in future.

    (2) i think I know where my confusion arose...

    print "\Qz\E";
    print "\n";
    print "\Q \E";
    print "\n";

    produces three lines of output,
    line 1 : z
    line 2 : \
    line 3 : (newline)

    Why does output line 2 contain a backslash when output line 1 doesn't
    ?

    thanks - Griff
    Griff, Aug 17, 2004
    #3
  4. Griff

    thundergnat Guest

    Griff wrote:
    > (1) apologies for starting a thread in non-existent NG, I will use
    > comp.lang.perl.misc in future.
    >
    > (2) i think I know where my confusion arose...
    >
    > print "\Qz\E";
    > print "\n";
    > print "\Q \E";
    > print "\n";
    >
    > produces three lines of output,
    > line 1 : z
    > line 2 : \
    > line 3 : (newline)
    >
    > Why does output line 2 contain a backslash when output line 1 doesn't
    > ?
    >
    > thanks - Griff


    Actually line 2 has a has a backslash followed by a space. The \Q..\E
    construct all non word characters with a backslash. A space is not a
    word character, hence it returns "\ ". To illustrate, try somthing like:

    print "\Qab_cd1:2'3/4? 5\E";


    This group is defunct. Please use comp.lang.perl.misc in future.
    thundergnat, Aug 17, 2004
    #4
  5. Griff

    thundergnat Guest

    Griff wrote:

    > (1) apologies for starting a thread in non-existent NG, I will use
    > comp.lang.perl.misc in future.
    >
    > (2) i think I know where my confusion arose...
    >
    > print "\Qz\E";
    > print "\n";
    > print "\Q \E";
    > print "\n";
    >
    > produces three lines of output,
    > line 1 : z
    > line 2 : \
    > line 3 : (newline)
    >
    > Why does output line 2 contain a backslash when output line 1 doesn't
    > ?
    >
    > thanks - Griff


    Actually line 2 has a has a backslash followed by a space. The \Q..\E
    construct prepends a backslash to all non word characters. A space is
    not a word character, hence it returns "\ ". To illustrate, try somthing
    like:

    print "\Qab_cd 1:2'3/4?5\E";


    This group is defunct. Please use comp.lang.perl.misc in future.
    thundergnat, Aug 17, 2004
    #5
  6. Griff

    Joe Smith Guest

    Griff wrote:

    > (2) i think I know where my confusion arose...
    >
    > print "\Qz\E";


    That's not their purpose. \Q and \E should only be used
    a) in the match operator (m//).
    b) in the lefthand part of the substitue operator (s///).
    c) in creating regular expressions via qr//, to be
    used in one of the two above cases.

    -Joe
    Joe Smith, Aug 18, 2004
    #6
  7. Griff

    Guest

    Joe Smith <> wrote in message news:<O%DUc.7190$Fg5.7106@attbi_s53>...
    > Griff wrote:
    >
    > > (2) i think I know where my confusion arose...
    > >
    > > print "\Qz\E";

    >
    > That's not their purpose. \Q and \E should only be used
    > a) in the match operator (m//).
    > b) in the lefthand part of the substitue operator (s///).
    > c) in creating regular expressions via qr//, to be
    > used in one of the two above cases.


    That's not enirely true. quotemeta() (aka \Q...\E) can also be useful
    in constructing strings that are to passed to eval() or system().
    , Aug 20, 2004
    #7
    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. Maziar Aflatoun

    Escape characters

    Maziar Aflatoun, Dec 5, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    544
    Jason S
    Dec 5, 2003
  2. Guadala Harry

    What Happens To Escape Characters?

    Guadala Harry, Aug 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    681
    Lau Lei Cheong
    Aug 19, 2004
  3. =?Utf-8?B?YmFzdWxhc3o=?=

    Are there escape characters for SQL?

    =?Utf-8?B?YmFzdWxhc3o=?=, Jul 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    10,928
    Patrice
    Jul 7, 2005
  4. Frank Ratzlow
    Replies:
    0
    Views:
    288
    Frank Ratzlow
    Mar 31, 2005
  5. slomo
    Replies:
    5
    Views:
    1,507
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page