understanding \Q \E

G

Guy

I bought the 3 books: Learning Perl, Intermediate Perl, Mastering Perl, and
what I've read is great. But the only info I've found so far on \Q \E is on
page 24 of Learning Perl which says "Quote non-word characters by adding a
backslash until \E." Does this mean metacharacters?

And I read on http://www.informit.com/articles/article.aspx?p=24468
that the full list of metacharacters is \, |, ^, $, *, +, ?, ., (, ), [, {

So I wrote the following lines to see the results, but I don't seem to grasp
what is happening and what makes \Q \E useful. You may want to ignore the
<BR> as in my case I am printing to HTML.
my $str1 = "\a|a^a$a*a+a?a.a(a)a[a{a";
my $str2 = "\Q\a|a^a$a*a+a?a.a(a)a[a{a\E";
my $str3 = "\\Q\a|a^a$a*a+a?a.a(a)a[a{a\\E";
print "</CENTER>";
print '\a|a^a$a*a+a?a.a(a)a[a{a<BR>'; # the original string
print "$str1 <BR>";
print "$str2 <BR>";
print "$str3 <BR>";

This is the results.
\a|a^a$a*a+a?a.a(a)a[a{a
|a^a*a+a?a.a(a)a[a{a
\\|a\^a\*a\+a\?a\.a\(a\)a\[a\{a
\Q|a^a*a+a?a.a(a)a[a{a\E

Can someone show me a useful example of \Q \E ? All info is appreciated.

Guy D
 
A

Anonymous

I bought the 3 books: Learning Perl, Intermediate Perl, Mastering Perl, and
what I've read is great. But the only info I've found so far on \Q \E is on
page 24 of Learning Perl which says "Quote non-word characters by adding a
backslash until \E."  Does this mean metacharacters?

And I read onhttp://www.informit.com/articles/article.aspx?p=24468
that the full list of metacharacters is \, |, ^, $, *, +, ?, ., (, ), [, {

So I wrote the following lines to see the results, but I don't seem to grasp
what is happening and what makes \Q \E useful. You may want to ignore the
<BR> as in my case I am printing to HTML.
    my $str1 = "\a|a^a$a*a+a?a.a(a)a[a{a";
    my $str2 = "\Q\a|a^a$a*a+a?a.a(a)a[a{a\E";
    my $str3 = "\\Q\a|a^a$a*a+a?a.a(a)a[a{a\\E";
    print "</CENTER>";
    print '\a|a^a$a*a+a?a.a(a)a[a{a<BR>';    # the original string
    print "$str1 <BR>";
    print "$str2 <BR>";
    print "$str3 <BR>";

This is the results.
    \a|a^a$a*a+a?a.a(a)a[a{a
     |a^a*a+a?a.a(a)a[a{a
    \ \|a\^a\*a\+a\?a\.a\(a\)a\[a\{a
    \Q |a^a*a+a?a.a(a)a[a{a\E

Can someone show me a useful example of \Q \E ? All info is appreciated.

One scenario: a search function that scans a document with a regex
and incorporates user inputs. If the user wants to see
if there's a '$foo' in section 2, then any regex metacharacters (such
as '$') in the user's input will need to be escaped.

$user_input = '$foo';
$regex = qr/^section 2.*?\Q$user_input\E.*?^section 3/ms;
if ($doc =~ $regex) {
...
 
G

Guy

Ben Morrow said:
Quoth "Guy" <[email protected]>:
[SNIP]

Does this mean you are running programs as CGI scripts? This is a bad
idea while you are just learning the language. Get a copy of perl for
your machine and do your experimenting locally: for Windows, you can get
perl from strawberryperl.com, and other systems (including Mac OS) will
usually have perl installed already.

I do have Strawberry Perl, and yes it would have made more sense to test it
locally, and no I don't know why I was uploading to the server to test, and
yes I feel stupid now :O

[SNIP]
See "Quote and Quote-like Operators" in perldoc perlop for the full list
of escapes in double-quoted strings (if you haven't already).
Ben


A lot of people refer to perldoc but I don't know how to access this. Is
this a website or did it install with Strawberry Perl, or is it on the
server?

I can access the version of perl by doing system 'perl -v'; This also says
"Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'." I've tried system
'perldoc'; but that didn't show anything.

Thanks,
Guy
 
T

Tad J McClellan

page 24 of Learning Perl which says "Quote non-word characters by adding a
backslash until \E." Does this mean metacharacters?


No, it means non-word characters.

And I read on http://www.informit.com/articles/article.aspx?p=24468
that the full list of metacharacters is \, |, ^, $, *, +, ?, ., (, ), [, {


That is not a list of metacharacters in a string, that is a list
of metacharacters in a regular expression.

Hyphen is not a regex metacharacter (it _is_ a Perl metacharacter,
it is also meta in character classes) but it is a non-word character.

Space is not a regex metacharacter but it is a non-word character.

And they are both "quoted" (backslashed) by \Q

my $str1 = "a hyphenated-string";
print "$str1\n";
print "\Q$str1", "\n";

There is a named function that does what \Q does in strings:

perldoc -f quotemeta

This code makes the 2 more lines of output that are the same
as the last line of code above:

print quotemeta($str1), "\n";

$str1 =~ s/(\W)/\\$1/g;
print "$str1\n";

So I wrote the following lines to see the results, but I don't seem to grasp
what is happening and what makes \Q \E useful.


It is most useful when using regular expressions rather than
simple quoted strings.

You may want to ignore the
<BR> as in my case I am printing to HTML.


You may want to convert your <BR> to \n before posting,
as nobody but you will be printing to HTML.

(because it a is foolish approach to figuring out what is going on.
eg: Why does my HTML look like this "a sentence" when my code is
print "a sentence";
?

Why did Perl delete all those spaces?

A: Perl did not delete any spaces.
)

You are once again demonstrating a lack of regard for us.

Should one person change the <BR> to \n or should that one person
just leave it, and let the dozens of other people that are being
asked to do you a favor convert them?

Is your time more valuable than the time of dozens/hundreds of us?

my $str1 = "\a|a^a$a*a+a?a.a(a)a[a{a";
^^
^^

You should always enable warnings when developing Perl code!

my $str2 = "\Q\a|a^a$a*a+a?a.a(a)a[a{a\E";
my $str3 = "\\Q\a|a^a$a*a+a?a.a(a)a[a{a\\E";
print "</CENTER>";
print '\a|a^a$a*a+a?a.a(a)a[a{a<BR>'; # the original string
print "$str1 <BR>";
print "$str2 <BR>";
print "$str3 <BR>";

This is the results.

No it isn't.

This is the results:

\a|a^a$a*a+a?a.a(a)a[a{a
|a^a*a+a?a.a(a)a[a{a
\\|a\^a\*a\+a\?a\.a\(a\)a\[a\{a
\Q|a^a*a+a?a.a(a)a[a{a\E


Your code has

print "</CENTER>";

in it. Did that print statement fail?

Can someone show me a useful example of \Q \E ?


It is useful when your pattern contains a regex metacharacter
that you want to match literally:

------------------
#!/usr/bin/perl
use warnings;
use strict;

my $str = 'fooxhtml';
print "matched\n" if $str =~ /foo.html/; # Oops!
print "matched again\n" if $str =~ /\Qfoo.html/;

my $url = 'http://www.informit.com/articles/article.aspx?p=24468';
print "matched url\n" if $url =~ /aspx?p/; # Oops!
print "matched url again\n" if $url =~ /\Qaspx?p/;
 
T

Tad J McClellan

Ben Morrow said:
Quoth "Guy" <[email protected]>:
And I read on http://www.informit.com/articles/article.aspx?p=24468
that the full list of metacharacters is \, |, ^, $, *, +, ?, ., (, ), [, {

Assuming you mean regex metacharacters, a better place to look would be
perldoc perlreref. Relying on random web pages rather than the real
documentation is a bad idea for any programming language, but
particularly so for Perl. There's an awful lot of rubbish out there
about Perl, unfortunately.


I was going to say something similar...

.... until I looked at the byline on that page.


Clinton is a Real Perl Programmer, so in this case it isn't the
usual "random" web page we usually see referred to here...


It is so important, let me say it yet again, since repetition is
the key to learning.

The standard Perl docs are better than web pages.

The standard Perl docs are better than books.

In this case, most especially when what you need is of a "reference"
nature rather than a "tutorial" nature.
 
T

Tad J McClellan

Guy said:
A lot of people refer to perldoc but I don't know how to access this.


A lot (most, in fact) of Perl programmers use Perl on *nix, so they
are used to accessing it by typing into the command line exactly what
you usually see posted (which is why it is posted that way).

eg. Most folks see Perl's std docs by typing:

perldoc -f quotemeta

at a shell prompt.

The important part to get here, is that when they say

perldoc -f quotemeta

it is a short-hand for:

you should read the description of the quotemeta function
in Perl's standard documentation.

How you do that on your system is system-specific.

Is
this a website


No, the standard Perl docs are part of a normal perl distribution.

That is, they are normally installed along with perl itself.

or did it install with Strawberry Perl,


I do not use Strawberry Perl, so I can't help you with that part.

or is it on the
server?


If it is a *nix server and it is configured properly, then yes,
the standard Perl docs should be on your server.

Do you have command line access to your (web, I assume) server?
 
G

Guy

Tad J McClellan said:
page 24 of Learning Perl which says "Quote non-word characters by adding
a
backslash until \E." Does this mean metacharacters?


No, it means non-word characters.

And I read on http://www.informit.com/articles/article.aspx?p=24468
that the full list of metacharacters is \, |, ^, $, *, +, ?, ., (, ), [,
{


That is not a list of metacharacters in a string, that is a list
of metacharacters in a regular expression.

Hyphen is not a regex metacharacter (it _is_ a Perl metacharacter,
it is also meta in character classes) but it is a non-word character.

Space is not a regex metacharacter but it is a non-word character.

And they are both "quoted" (backslashed) by \Q

my $str1 = "a hyphenated-string";
print "$str1\n";
print "\Q$str1", "\n";

There is a named function that does what \Q does in strings:

perldoc -f quotemeta

This code makes the 2 more lines of output that are the same
as the last line of code above:

print quotemeta($str1), "\n";

$str1 =~ s/(\W)/\\$1/g;
print "$str1\n";

So I wrote the following lines to see the results, but I don't seem to
grasp
what is happening and what makes \Q \E useful.


It is most useful when using regular expressions rather than
simple quoted strings.

You may want to ignore the
<BR> as in my case I am printing to HTML.


You may want to convert your <BR> to \n before posting,
as nobody but you will be printing to HTML.

(because it a is foolish approach to figuring out what is going on.
eg: Why does my HTML look like this "a sentence" when my code is
print "a sentence";
?

Why did Perl delete all those spaces?

A: Perl did not delete any spaces.
)

You are once again demonstrating a lack of regard for us.

Should one person change the <BR> to \n or should that one person
just leave it, and let the dozens of other people that are being
asked to do you a favor convert them?

Is your time more valuable than the time of dozens/hundreds of us?


I understand exactly and totally agree with that but honestly didn't realize
it as I was cutting and pasting. I'm sincerely sorry... again.

my $str1 = "\a|a^a$a*a+a?a.a(a)a[a{a";
^^
^^

You should always enable warnings when developing Perl code!

my $str2 = "\Q\a|a^a$a*a+a?a.a(a)a[a{a\E";
my $str3 = "\\Q\a|a^a$a*a+a?a.a(a)a[a{a\\E";
print "</CENTER>";
print '\a|a^a$a*a+a?a.a(a)a[a{a<BR>'; # the original string
print "$str1 <BR>";
print "$str2 <BR>";
print "$str3 <BR>";

This is the results.

No it isn't.

This is the results:

</CENTER>\a|a^a$a*a+a?a.a(a)a[a{a<BR>|a^a*a+a?a.a(a)a[a{a
<BR>\\|a\^a\*a\+a\?a\.a\(a\)a\[a\{a <BR>\Q|a^a*a+a?a.a(a)a[a{a\E <BR>


Again, I see what you mean by including HTML stuff here.

\a|a^a$a*a+a?a.a(a)a[a{a
|a^a*a+a?a.a(a)a[a{a
\\|a\^a\*a\+a\?a\.a\(a\)a\[a\{a
\Q|a^a*a+a?a.a(a)a[a{a\E


Your code has

print "</CENTER>";

in it. Did that print statement fail?


Just in case this wasn't a retorical question, it didn't appear to fail, not
from the server and not from my local Strawberry perl.

It is useful when your pattern contains a regex metacharacter
that you want to match literally:

------------------
#!/usr/bin/perl
use warnings;
use strict;

my $str = 'fooxhtml';
print "matched\n" if $str =~ /foo.html/; # Oops!
print "matched again\n" if $str =~ /\Qfoo.html/;

my $url = 'http://www.informit.com/articles/article.aspx?p=24468';
print "matched url\n" if $url =~ /aspx?p/; # Oops!
print "matched url again\n" if $url =~ /\Qaspx?p/;

Thanks for all Tad,
Guy D.
 
J

Jürgen Exner

Guy said:
A lot of people refer to perldoc but I don't know how to access this. Is
this a website or did it install with Strawberry Perl, or is it on the
server?

perldoc is a standard part of any perl installation and you can access
it by simply typing the command
perldoc
plus the appropriate arguments on the command line. Try
perldoc perldoc
for an overview of what options are available.

If your installation of perl is missing perldoc then either the
installation is broken (you may want to fix it) or it is a stripped-down
special use version for e.g. a server where documentation would be of no
use because nobody would be using it to develop programs on it..

jue
 
J

Jürgen Exner

Tad J McClellan said:
A lot (most, in fact) of Perl programmers use Perl on *nix, so they
are used to accessing it by typing into the command line exactly what
you usually see posted (which is why it is posted that way).

eg. Most folks see Perl's std docs by typing:

perldoc -f quotemeta

at a shell prompt.

Just for reference: it works exactly the same way from a MS Windows
command line, too.

jue
 
T

Tad J McClellan

Jürgen Exner said:
Just for reference: it works exactly the same way from a MS Windows
command line, too.


Thanks.

I thought I had heard that it didn't for AS Perl and that the
docs in that distro were only in HTML.

Is that accurate?

Does Strawberry Perl install "perldoc" and the PODs?
 
J

Jürgen Exner

Tad J McClellan said:
Jürgen Exner <[email protected]> wrote:
[about calling perldoc from command line]
I thought I had heard that it didn't for AS Perl and that the
docs in that distro were only in HTML.

Is that accurate?

If "AS" stands for ActiveState Perl then at least for older versions
both statements are false. AS does install perldoc, it works just fine,
and the documentation is available in POD and HTML.

Can't comment on 5.10 because I haven't updated to it yet.
Does Strawberry Perl install "perldoc" and the PODs?

Don't know, have never used it.

jue
 
A

A. Sinan Unur

Tad J McClellan said:
Jürgen Exner <[email protected]> wrote:
[about calling perldoc from command line]
I thought I had heard that it didn't for AS Perl and that the
docs in that distro were only in HTML.

Is that accurate?

If "AS" stands for ActiveState Perl then at least for older versions
both statements are false. AS does install perldoc, it works just
fine, and the documentation is available in POD and HTML.

Can't comment on 5.10 because I haven't updated to it yet.

ActiveState Perl (including 5.10) has included with perldoc, standard
pod documentation and the HTML version ever since the first time I
installed an ActiveState distribution (circa 5.6 something).

Yes it does. It does not install HTML docs however.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top