Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

CTRL+C Regular Expression

by anshumangoyal (Scribe)
on Nov 07, 2012 at 05:51 UTC ( #1002632=perlquestion: print w/replies, xml ) Need Help??
anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I am having a code which send CTRL + C to a shell prompt. My Problem is I am not able to match the CTRL + C with Regexp. Here is my code:
my $Command = '\x03'; # This is some sub-routine I am calling to send the command. SendCommand ($Command); # This Regexp never sets true which is the problem: if ($Command =~ m/\\x03/) { # it never goes in this if statement print "Matched !!"; }
Can some one suggest what the wrong here is?

Replies are listed 'Best First'.
Re: CTRL+C Regular Expression
by Athanasius (Chancellor) on Nov 07, 2012 at 06:48 UTC

    AFAICT, there are 2 possibilities:

    (1) You are sending a Ctrl-C from one process to another. In that case, forget about regexen — you need a signal handler. See Signals.

    (2) You are sending “\x03” as the argument to a subroutine within the same process. In that case, it makes sense to match it with a regex. And there are two options:

    (2a) Suppress interpolation and match the string character-by-character. That is in fact what you did in the OP, which does print Matched !! contrary to the claim made there.

    (2b) Interpolate and match with \cC in the regex, as per Kenosis’s original answer:

    16:46 >perl -Mstrict -wE "say 'Matched !!' if '\x03' =~ /\\x03/;" Matched !! 16:46 >perl -Mstrict -wE "say 'Matched !!' if qq[\x03] =~ /\\x03/;" 16:46 >perl -Mstrict -wE "say 'Matched !!' if qq[\x03] =~ /\cC/;" Matched !! 16:46 >

    Hope that helps,

    Athanasius <°(((><contra mundum

      $ perl -e '"\x03" =~ /\x03/ and print "Matched!\n"' Matched! $ perl -e 'chr(3) =~ /\x03/ and print "Matched!\n"' Matched! $ perl -e '3 =~ /\x03/ and print "Matched!\n"'
Re: CTRL+C Regular Expression
by bart (Canon) on Nov 07, 2012 at 11:30 UTC
    my $Command = '\x03';
    That's not a ctrl-C. This is a ctrl-C:
    my $Command = "\x03";
    Notice the double quotes, instead of the single quotes you used.
Re: CTRL+C Regular Expression
by wirito (Acolyte) on Nov 07, 2012 at 11:21 UTC
    It could be interesting to know how you are sending "Ctrl+C". Also, I can't figure how you came that Ctrl+C is the same as '\x03'.
    If you 'open' a shell process and send '\x03' it won't be interpreted as Ctrl+C but as chr(3):
    $ perl -e 'open F, "|cat>test" or die("cant open"); print F "\x03"; cl +ose F' $ hexdump -C test 00000000 03 |.| 00000001
    However If you mean pressing 'Ctrl+C' the shell will send a signal (nothing to do with values) that you can trap as Athanasius said before.
    $ perl -e '$SIG{INT} = sub { die "Got signal" }; while() {};' [Press CTRL+C] Got signal at -e line 1.
Re: CTRL+C Regular Expression
by Kenosis (Priest) on Nov 07, 2012 at 06:09 UTC

    Did you try \cC for ctrl-c?

    Not sure what I was thinking...

      Its the same as \x03. Question is how to match \x03 in regular expression?

        You're right. Not sure what I was thinking. Will strike it...

Re: CTRL+C Regular Expression
by Kenosis (Priest) on Nov 07, 2012 at 06:22 UTC

    Sorry... Replied to myself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002632]
Approved by Kenosis
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-07-28 11:18 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (428 votes). Check out past polls.