469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

trying out escape characters

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
Jul 19 '05 #1
6 13997
gr*****@aol.com (Griff) wrote in message news:<d6**************************@posting.google. com>...
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.
Jul 19 '05 #2
(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
Jul 19 '05 #3
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.
Jul 19 '05 #4
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.
Jul 19 '05 #5
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
Jul 19 '05 #6
Joe Smith <Jo*******@inwap.com> 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().
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by BTnews | last post: by
2 posts views Thread by Matthew Wieder | last post: by
18 posts views Thread by Steve Litvack | last post: by
4 posts views Thread by Guadala Harry | last post: by
15 posts views Thread by pkaeowic | last post: by
131 posts views Thread by Lawrence D'Oliveiro | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.