understanding \Q \E

Discussion in 'Perl Misc' started by Guy, Jun 10, 2009.

  1. Guy

    Guy Guest

    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
    Guy, Jun 10, 2009
    #1
    1. Advertising

  2. Guy

    Anonymous Guest

    On Jun 9, 4:12 pm, "Guy" <> wrote:
    > 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) {
    ...

    --
    Charles DeRykus
    Anonymous, Jun 10, 2009
    #2
    1. Advertising

  3. Guy

    Guy Guest

    "Ben Morrow" <> wrote:
    >
    > Quoth "Guy" <>:


    [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
    Guy, Jun 10, 2009
    #3
  4. Guy <> wrote:


    > 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:

    </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>


    > \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/;
    ------------------

    It saves you from having to do the backslashing yourself.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Jun 10, 2009
    #4
  5. Ben Morrow <> wrote:
    > Quoth "Guy" <>:


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


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Jun 10, 2009
    #5
  6. Guy <> wrote:

    > 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?


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Jun 10, 2009
    #6
  7. Guy

    Guy Guest

    "Tad J McClellan" <> wrote:
    > Guy <> wrote:
    >
    >
    >> 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.


    >> 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/;
    > ------------------
    >
    > It saves you from having to do the backslashing yourself.


    Thanks for all Tad,
    Guy D.
    Guy, Jun 10, 2009
    #7
  8. "Guy" <> wrote:
    >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ürgen Exner, Jun 10, 2009
    #8
  9. Tad J McClellan <> wrote:
    >Guy <> wrote:
    >
    >> 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.


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

    jue
    Jürgen Exner, Jun 10, 2009
    #9
  10. Jürgen Exner <> wrote:
    > Tad J McClellan <> wrote:
    >>Guy <> wrote:
    >>
    >>> 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.

    >
    > 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?


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Jun 10, 2009
    #10
  11. Tad J McClellan <> wrote:
    >Jürgen Exner <> wrote:

    [about calling perldoc from command line]
    >> Just for reference: it works exactly the same way from a MS Windows
    >> command line, too.

    >
    >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
    Jürgen Exner, Jun 10, 2009
    #11
  12. Jürgen Exner <> wrote in
    news::

    > Tad J McClellan <> wrote:
    >>Jürgen Exner <> wrote:

    > [about calling perldoc from command line]
    >>> Just for reference: it works exactly the same way from a MS Windows
    >>> command line, too.

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

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


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

    Sinan

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

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Jun 10, 2009
    #12
    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. George Hester
    Replies:
    3
    Views:
    3,492
    Carl Prothman [MVP]
    Aug 11, 2003
  2. Alvin Andries

    Re: understanding an error

    Alvin Andries, Sep 12, 2003, in forum: VHDL
    Replies:
    0
    Views:
    474
    Alvin Andries
    Sep 12, 2003
  3. Geo
    Replies:
    0
    Views:
    476
  4. Geo
    Replies:
    6
    Views:
    522
  5. Leon Shaw

    Server Error Understanding

    Leon Shaw, Jul 28, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    431
    John Saunders
    Jul 28, 2003
Loading...

Share This Page