Hello All:

I'm working on a Perl/Tk gui that I hope will allow a user to type a regexp into an entry widget for matching against some text. I'd like to to be true that the user, if he or she pleases, may supply a qr//'d regexp. So far, what seems to me to be the obvious way to do it has failed.

The following code demonstrates the failure...
use warnings; use strict; use Tk; my $string = "foobarbaz"; my $mw = MainWindow->new; my $regexp_entry = $mw -> Entry() -> pack; my $go_button = $mw -> Button( -takefocus => 1, -text => "Match using what you typed!", -command => sub{ my $exp = $regexp_entry -> get(); print STDOUT "I'm looking for a match for $exp...\n"; $string =~ m/$exp/ ? print STDOUT "match!\n" : print STDOUT "N +o match!\n"; } )-> pack; my $used_stored_exp = $mw -> Button( -text => "Match with Stored Regexp!", -command => sub{ my $exp = qr/foo/; print STDOUT "I'm looking for a match for $exp...\n"; $string =~ m/$exp/ ? print STDOUT "match!\n" : print STDOUT "N +o match!\n"; } )-> pack; $regexp_entry -> focus; MainLoop;
To see the failure, run the script and do the following:
(1)press the "Match using stored Regexp" button, and watch your STDOUT line.
(2)type
qr/foo/
into the entry widget, press the "Match using what you typed!" button, and watch STDOUT.

Surprisingly (perhaps only to a neophyte like me) it "works" if in step (2) above, you instead type
(?-xism:foo)
into the entry widget.

What's going on here? My first guess is that there's something happening with the entry widget's "get" method

Thanks!

In reply to passing qr//'d regexp via Perl/Tk Entry widget by young_stu

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.