Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Just BTW (and it seems to make no difference in my experiments, your Line 7 lacks the closing double-quote found in OP's datum...

No. Everything inside unquoted [] inside qr// is a character class. Repeating characters inside [] is allowed, but has no effect. Double-quotes have no special meaning there, they are just like almost any other character. In fact, they are the first character only because I sorted all of the characters found in your $pattern line by their ASCII code:

>perl -E '$x=q[Category("notestrecord")];@a=split "",$x;say join "" => + sort grep { !$seen{$_}++ } @a' "()Cacdegnorsty >

You could move the double quotes to any other position between [] without any effect. See Bracketed Character Classes.

have not satisfied myself that I understand the implications of the discussion of 'normaliz(ation) in http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators.

Should not be very relevant here. The most important part (perhaps the only one?) is that RE modifiers are merged into the RE using embedded pattern-match modifiers ((?:pattern), (?adluimsx-imsx:pattern), or (?^aluimsx:pattern) notation):

>perl -e 'print "$_\n" for (qr/ab.c/,qr/ab.c/i,qr/ab.c/s,qr/ab.c/si)' (?^:ab.c) (?^i:ab.c) (?^s:ab.c) (?^si:ab.c) >

Enabling newer perl features may add the unicode flag:

>perl -E 'say for (qr/ab.c/,qr/ab.c/i,qr/ab.c/s,qr/ab.c/si)' (?^u:ab.c) (?^ui:ab.c) (?^us:ab.c) (?^usi:ab.c) >

See Extended Patterns.

Normalizing does not clean up redundant characters in character classes, nor does it sort characters in character classes - at least not in my perl 5.18.1. Enabling features does not change that. Using qr"" instead of qr// also has no effect:

>perl -e 'print "$_\n" for (qr/[abc]/,qr/[aabbcc]/,qr/[abcabc]/,qr/[cc +cbba]/)' (?^:[abc]) (?^:[aabbcc]) (?^:[abcabc]) (?^:[cccbba]) >perl -E 'say for (qr/[abc]/,qr/[aabbcc]/,qr/[abcabc]/,qr/[cccbba]/)' (?^u:[abc]) (?^u:[aabbcc]) (?^u:[abcabc]) (?^u:[cccbba]) >perl -e 'print "$_\n" for (qr"[abc]",qr"[aabbcc]",qr"[abcabc]",qr"[cc +cbba]")' (?^:[abc]) (?^:[aabbcc]) (?^:[abcabc]) (?^:[cccbba]) >

(Note that ALL of the test patterns match a single character a, b, or c).

There is a difference between $pattern="pattern"; $foo=~/$pattern/, $pattern=qr/pattern/; $foo=~/$pattern/, and $pattern=qr/pattern/; $foo=~$pattern: The first example is simple string interpolation, $pattern is just a simple scalar that will be compiled every time it is used in a regexp. The second and third example creates a Regexp object with a (possibly) compiled regular expression, using it in a regexp avoids recompiling it.

So, qr is not just a quoting operator for regular expressions (despite its name), but a constructor for Regexp (object) references. Those objects stringify to an equivalent (but not necessarily equal) regexp string. Perl may choose to compile the pattern inside the Regexp (object) reference. The exact wording in perlop is "This operator quotes (and possibly compiles) its STRING as a regular expression." in 5.20.1, "A string which is (possibly) interpolated and then compiled as a regular expression." in 5.005.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^4: Problem with input strings that have "[]" brackets by afoken
in thread Problem with input strings that have "[]" brackets by Laszlo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found