trying out escape characters

G

Griff

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
 
N

nobull

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.
 
G

Griff

(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
 
T

thundergnat

Griff said:
(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.
 
T

thundergnat

Griff said:
(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.
 
J

Joe Smith

Griff said:
(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
 
N

nobull

Joe Smith said:
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().
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top