[26606] in Perl-Users-Digest
Perl-Users Digest, Issue: 8718 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 1 21:11:23 2005
Date: Thu, 1 Dec 2005 18:11:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 1 Dec 2005 Volume: 10 Number: 8718
Today's topics:
Killing all child processes upon parent exit <snort_sam@yahoo.com>
Re: Killing all child processes upon parent exit (Anno Siegel)
Re: Killing all child processes upon parent exit <snort_sam@yahoo.com>
Re: Killing all child processes upon parent exit (Anno Siegel)
Re: Killing all child processes upon parent exit <snort_sam@yahoo.com>
Re: Killing all child processes upon parent exit (Anno Siegel)
Re: Killing all child processes upon parent exit <snort_sam@yahoo.com>
Re: Killing all child processes upon parent exit (Anno Siegel)
Re: Killing all child processes upon parent exit xhoster@gmail.com
Re: Killing all child processes upon parent exit <No_4@dsl.pipex.com>
length is 0 with unpack_socketaddr_in <snort_sam@yahoo.com>
Re: length is 0 with unpack_socketaddr_in xhoster@gmail.com
lookfor failure in SerialPort <cNaOlSePbA@MvPeLtEsAtSaEr.com>
Matching escaped delimiter chars <witold.rugowski@hp.com>
Re: Matching escaped delimiter chars <sherm@dot-app.org>
Re: Matching escaped delimiter chars robic0
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 30 Nov 2005 20:58:07 +1100
From: bsder <snort_sam@yahoo.com>
Subject: Killing all child processes upon parent exit
Message-Id: <438d77ba@news.rivernet.com.au>
Hi,
Could anyone please tell me how to kill all child processes when parent
exit?
Here is the server code:
#!/usr/bin/perl
use strict;
use Socket;
use Data::Dumper;
# forward declaration
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
$SIG{INT} = $SIG{TERM} =$SIG{HUP} = \&signal_handler;
$SIG{CHLD} = 'IGNORE';
$SIG{CLD} = 'IGNORE';
my %kids = {};
my $proto = getprotobyname('tcp');
socket(SOCK,PF_INET,SOCK_STREAM,$proto) || die "socket : $!";
my $port = 7888;
my $address = pack_sockaddr_in($port, INADDR_ANY);
bind(SOCK, $address) || die "bind : $!";
while (1) {
listen(SOCK, SOMAXCONN) || die "listen: $!";
my $hostName = inet_ntoa(INADDR_ANY);
logmsg "server started on port $port, $hostName";
sub REAPER {
my $waitedpid = wait;
$SIG{CHLD} = \&REAPER;
logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
}
$SIG{CHLD} = \&REAPER;
print STDOUT "Server host: $hostName\n";
print STDOUT "Server port: $port\n";
(my $addr = accept(NEWSOCK,SOCK)) or ($! eq 'Interrupted system
all' and redo) or die $!;
select(NEWSOCK); $| = 1; select(STDOUT);
my $kidpid;
if (!defined($kidpid = fork())) {
die "cannot fork: $!";
}
elsif ($kidpid == 0) { # child
my ($cPort, $cHost) = unpack_sockaddr_in($addr);
my $cHostName = inet_ntoa($cHost);
print STDOUT "Client host: $cHostName\n";
print STDOUT "Client port: $cPort\n";
print NEWSOCK "Welcome to Code Generator.\r\n";
while (my $m=<NEWSOCK>) {
$m =~ s/\n|\r//g;
last if ($m eq ".");
print NEWSOCK "Server received $m\r\n";
}
close(NEWSOCK) || die "close in child: $!";
delete %kids->{$kidpid};
exit;
}
}
sub signal_handler
{
local $SIG{HUP} = 'IGNORE';
kill HUP => -$$;
print STDOUT "going to die\n";
exit;
}
Client code:
#!/usr/bin/perl
$domain = 2; # Internet domain
$type = 1; # Sequenced, reliable, two-way connection, byte streams
$proto = 6; # Transmission Control Protocol (TCP)
socket(SOCK,$domain,$type,$proto);
$host = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
$port = 1024;
$address = pack('S n a4 x8', $domain, $port, $host);
bind(SOCK, $address);
print STDOUT "Client host: ",join('.',unpack('C4', $host)),"\n";
print STDOUT "Client port: $port\n";
$sHost = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
$sPort = 7888;
$sAddress = pack('S n a4 x8', $domain, $sPort, $sHost);
connect(SOCK, $sAddress);
print STDOUT "Server host: ",join('.',unpack('C4', $sHost)),"\n";
print STDOUT "Server port: $sPort\n";
select(SOCK); $| = 1; select(STDOUT);
while ($m=<SOCK>) {
print STDOUT $m;
$m = <STDIN>;
print SOCK $m;
}
close(SOCK);
exit;
Thanks
Sam
------------------------------
Date: 30 Nov 2005 10:01:57 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Killing all child processes upon parent exit
Message-Id: <dmjtal$n9b$1@mamenchi.zrz.TU-Berlin.DE>
bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> Hi,
>
> Could anyone please tell me how to kill all child processes when parent
> exit?
What's the problem? You have the PID, Perl has the kill() function.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Wed, 30 Nov 2005 21:24:40 +1100
From: bsder <snort_sam@yahoo.com>
Subject: Re: Killing all child processes upon parent exit
Message-Id: <438d7dea$1@news.rivernet.com.au>
Anno Siegel wrote:
> bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
>
>>Hi,
>>
>>Could anyone please tell me how to kill all child processes when parent
>>exit?
>
>
> What's the problem? You have the PID, Perl has the kill() function.
>
> Anno
Yes I have added the kill() function in the signal_handler(), is this a
proper way to add kill() function in signal handler?
Thanks
Sam
------------------------------
Date: 30 Nov 2005 10:48:17 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Killing all child processes upon parent exit
Message-Id: <dmk01h$o4k$2@mamenchi.zrz.TU-Berlin.DE>
bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> >
> >>Hi,
> >>
> >>Could anyone please tell me how to kill all child processes when parent
> >>exit?
> >
> >
> > What's the problem? You have the PID, Perl has the kill() function.
> >
> > Anno
>
> Yes I have added the kill() function in the signal_handler(), is this a
> proper way to add kill() function in signal handler?
In which signal handler? That doesn't make sense. A signal handler is
activated when the process receives a signal. You want to *send* a
signal when the process exits, so call kill() when it exits.
If you want to automate the kill-on-exit, you could call the kill function
in an END block. Look up END in perlmod to see the possibilities and
limitations.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Wed, 30 Nov 2005 22:01:47 +1100
From: bsder <snort_sam@yahoo.com>
Subject: Re: Killing all child processes upon parent exit
Message-Id: <438d869e@news.rivernet.com.au>
Anno Siegel wrote:
> bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
>
>>Anno Siegel wrote:
>>
>>>bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
>>>
>>>
>>>>Hi,
>>>>
>>>>Could anyone please tell me how to kill all child processes when parent
>>>>exit?
>>>
>>>
>>>What's the problem? You have the PID, Perl has the kill() function.
>>>
>>>Anno
>>
>>Yes I have added the kill() function in the signal_handler(), is this a
>>proper way to add kill() function in signal handler?
>
>
> In which signal handler? That doesn't make sense. A signal handler is
> activated when the process receives a signal. You want to *send* a
> signal when the process exits, so call kill() when it exits.
>
have you look thru my server code? the signal handler is at the end of
the server code. I want to kill all its child processes when the server
code received an interrupt signal.
sam
> If you want to automate the kill-on-exit, you could call the kill function
> in an END block. Look up END in perlmod to see the possibilities and
> limitations.
>
> Anno
------------------------------
Date: 30 Nov 2005 11:41:39 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Killing all child processes upon parent exit
Message-Id: <dmk35j$qog$2@mamenchi.zrz.TU-Berlin.DE>
bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> >
> >>Anno Siegel wrote:
> >>
> >>>bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> >>>
> >>>
> >>>>Hi,
> >>>>
> >>>>Could anyone please tell me how to kill all child processes when parent
> >>>>exit?
> >>>
> >>>
> >>>What's the problem? You have the PID, Perl has the kill() function.
> >>>
> >>>Anno
> >>
> >>Yes I have added the kill() function in the signal_handler(), is this a
> >>proper way to add kill() function in signal handler?
> >
> >
> > In which signal handler? That doesn't make sense. A signal handler is
> > activated when the process receives a signal. You want to *send* a
> > signal when the process exits, so call kill() when it exits.
> >
> have you look thru my server code?
No. You gave us 95 lines of code with no indication how it relates
to your question. I haven't read it.
> the signal handler is at the end of
> the server code. I want to kill all its child processes when the server
> code received an interrupt signal.
That would kill the children only if the process itself is killed by
that specific signal. You can do that, but I'd still suggest taking
care of the children in an END block. You must still catch the signals
you expect to receive because an uncaught exception bypasses END. It
suffices to catch them unspecifically with "sub { die }".
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Wed, 30 Nov 2005 23:11:11 +1100
From: bsder <snort_sam@yahoo.com>
Subject: Re: Killing all child processes upon parent exit
Message-Id: <438d96e2@news.rivernet.com.au>
Anno Siegel wrote:
> bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
>
>>Anno Siegel wrote:
>>
>>>bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
>>>
>>>
>>>>Anno Siegel wrote:
>>>>
>>>>
>>>>>bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
>>>>>
>>>>>
>>>>>
>>>>>>Hi,
>>>>>>
>>>>>>Could anyone please tell me how to kill all child processes when parent
>>>>>>exit?
>>>>>
>>>>>
>>>>>What's the problem? You have the PID, Perl has the kill() function.
>>>>>
>>>>>Anno
>>>>
>>>>Yes I have added the kill() function in the signal_handler(), is this a
>>>>proper way to add kill() function in signal handler?
>>>
>>>
>>>In which signal handler? That doesn't make sense. A signal handler is
>>>activated when the process receives a signal. You want to *send* a
>>>signal when the process exits, so call kill() when it exits.
>>>
>>
>>have you look thru my server code?
>
>
> No. You gave us 95 lines of code with no indication how it relates
> to your question. I haven't read it.
>
>
>>the signal handler is at the end of
>>the server code. I want to kill all its child processes when the server
>>code received an interrupt signal.
>
>
> That would kill the children only if the process itself is killed by
> that specific signal. You can do that, but I'd still suggest taking
> care of the children in an END block. You must still catch the signals
> you expect to receive because an uncaught exception bypasses END. It
> suffices to catch them unspecifically with "sub { die }".
>
> Anno
I just want to follow C traditional style. There is no END style in C
anyway. well.. may be good just put kill() in the END block..
Thanks
Sam
------------------------------
Date: 30 Nov 2005 13:41:30 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Killing all child processes upon parent exit
Message-Id: <dmka6a$31i$1@mamenchi.zrz.TU-Berlin.DE>
bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> >>Anno Siegel wrote:
> >>>bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
> >>>>Anno Siegel wrote:
> >>>>>bsder <snort_sam@yahoo.com> wrote in comp.lang.perl.misc:
[...]
> >>>>>>Could anyone please tell me how to kill all child processes when parent
> >>>>>>exit?
[...]
> >>the signal handler is at the end of
> >>the server code. I want to kill all its child processes when the server
> >>code received an interrupt signal.
> >
> >
> > That would kill the children only if the process itself is killed by
> > that specific signal. You can do that, but I'd still suggest taking
> > care of the children in an END block. You must still catch the signals
> > you expect to receive because an uncaught exception bypasses END. It
> > suffices to catch them unspecifically with "sub { die }".
> >
> > Anno
>
> I just want to follow C traditional style. There is no END style in C
> anyway. well.. may be good just put kill() in the END block..
I took a look back at your code now you've said what part of it you
have problems with. One possible problem is that you set the signal
handler before you fork, so the kids have a (HUP-) handler too. It
looks to me that the kids should eventually die anyway, but it is
probably not what you want. Try setting the handler in the parent
only (after fork), or reset it in the kids.
For a Perl solution I'd still prefer END {}, but if you need a prototype
for a C(++) program, by all means debug the sig-handler solution. Is the
handler actually invoked? Does it succeed in sending the signal? What
is the return value of kill? Do the kids receive the signal? What
happens if you give an explicit list of pids instead of signalling the
process group?
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: 30 Nov 2005 20:08:00 GMT
From: xhoster@gmail.com
Subject: Re: Killing all child processes upon parent exit
Message-Id: <20051130150800.102$zw@newsreader.com>
bsder <snort_sam@yahoo.com> wrote:
> Hi,
>
> Could anyone please tell me how to kill all child processes when parent
> exit?
> elsif ($kidpid == 0) { # child
> my ($cPort, $cHost) = unpack_sockaddr_in($addr);
> my $cHostName = inet_ntoa($cHost);
> print STDOUT "Client host: $cHostName\n";
> print STDOUT "Client port: $cPort\n";
> print NEWSOCK "Welcome to Code Generator.\r\n";
>
> while (my $m=<NEWSOCK>) {
> $m =~ s/\n|\r//g;
> last if ($m eq ".");
> print NEWSOCK "Server received $m\r\n";
> }
> close(NEWSOCK) || die "close in child: $!";
> delete %kids->{$kidpid};
> exit;
What is the point of the delete? For one thing, you've never put
anything into %kids, neither in parent nor child, so there is nothing to
delete. For another thing, you are immediately exiting, which mean %kids
is going away. Why change a variable which is going to disappear a
nanosecond later anyway? You probably want the delete to be in the
SIG{CHLD} handler, so that it occurs in the parent process rather than the
doomed child.
...
> kill HUP => -$$;
In perl, you want to negate the signal, not the pid. But better yet,
once you fix your code dealing with %kids, kill them explicitly.
kill HUP => keys %kids.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Fri, 02 Dec 2005 00:21:00 +0000
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Killing all child processes upon parent exit
Message-Id: <LKmdnZIstpPzDhLeRVnygQ@pipex.net>
bsder wrote:
>
> I just want to follow C traditional style. There is no END style in C
> anyway.
Yes there is. It's called atexit(). (You might not have ever used it,
but that's another matter).
And what has C style got to do with programming in Perl anyway??? Do
you declare all variable to be ints/floats/char etc???
--
Just because I've written it doesn't mean that
either you or I have to believe it.
------------------------------
Date: Tue, 29 Nov 2005 22:29:35 +1100
From: bsder <snort_sam@yahoo.com>
Subject: length is 0 with unpack_socketaddr_in
Message-Id: <438c3ba7@news.rivernet.com.au>
Hi,
I got the following error message when the client drops its connection
after I pressed the Ctl-C.
Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16
at Echoer1.pl line 39.
Here is the Server code:
#!/usr/bin/perl
use strict;
use Socket;
# forward declaration
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
my $proto = getprotobyname('tcp');
socket(SOCK,PF_INET,SOCK_STREAM,$proto);
my $host = INADDR_ANY;
my $port = 7888;
my $address = pack_sockaddr_in($port, $host);
bind(SOCK, $address);
while (1) {
listen(SOCK, SOMAXCONN);
my $hostName = inet_ntoa($host);
logmsg "server started on port $port, $hostName";
my $waitedpid = 0;
my $paddr;
sub REAPER {
$waitedpid = wait;
$SIG{CHLD} = \&REAPER; # loathe sysV
logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
}
$SIG{CHLD} = \&REAPER;
print STDOUT "Server host: $hostName\n";
print STDOUT "Server port: $port\n";
my $cAddress = accept(NEWSOCK,SOCK);
my $kidpid;
if (!defined($kidpid = fork())) {
die "cannot fork: $!";
}
elsif ($kidpid == 0) { # child
my ($cPort, $cHost) = unpack_sockaddr_in($cAddress);
my $cHostName = inet_ntoa($cHost);
print STDOUT "Client host: $cHostName\n";
print STDOUT "Client port: $cPort\n";
select(NEWSOCK); $| = 1; select(STDOUT);
print NEWSOCK "Welcome to Code Generator.\r\n";
do "dbm_lib.pm";
my $dbm = new dbm_lib();
$dbm->exec_code();
while (my $m=<NEWSOCK>) {
$m =~ s/\n|\r//g;
print NEWSOCK "Server received $m\r\n";
}
close(NEWSOCK);
exit;
}
}
Here is the client code:
#!/usr/bin/perl
$domain = 2; # Internet domain
$type = 1; # Sequenced, reliable, two-way connection, byte streams
$proto = 6; # Transmission Control Protocol (TCP)
socket(SOCK,$domain,$type,$proto);
$host = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
$port = 1024;
$address = pack('S n a4 x8', $domain, $port, $host);
bind(SOCK, $address);
print STDOUT "Client host: ",join('.',unpack('C4', $host)),"\n";
print STDOUT "Client port: $port\n";
$sHost = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
$sPort = 7888;
$sAddress = pack('S n a4 x8', $domain, $sPort, $sHost);
connect(SOCK, $sAddress);
print STDOUT "Server host: ",join('.',unpack('C4', $sHost)),"\n";
print STDOUT "Server port: $sPort\n";
select(SOCK); $| = 1; select(STDOUT);
while ($m=<SOCK>) {
print STDOUT $m;
$m = <STDIN>;
print SOCK $m;
}
close(SOCK);
exit;
Thanks
Sam
------------------------------
Date: 29 Nov 2005 16:13:11 GMT
From: xhoster@gmail.com
Subject: Re: length is 0 with unpack_socketaddr_in
Message-Id: <20051129111311.698$xc@newsreader.com>
bsder <snort_sam@yahoo.com> wrote:
> Hi,
>
> I got the following error message when the client drops its connection
> after I pressed the Ctl-C.
>
> Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16
> at Echoer1.pl line 39.
You should check the success of your system calls, like "bind" and "accept"
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Wed, 30 Nov 2005 16:56:09 -0600
From: Spin <cNaOlSePbA@MvPeLtEsAtSaEr.com>
Subject: lookfor failure in SerialPort
Message-Id: <11osbg5ijm0nmf1@corp.supernews.com>
I am using Win32::SerialPort 0.19 and am having trouble getting the
lookfor to work. My understanding is that lookfor(1) should sit and wait
to read one character coming inbound on my serial port.
This is the sub that wraps the lookfor:
sub waitfor {
my $debug = 'y';
$ob->lookclear; # clear buffers
my $gotit = "";
my $response = shift;
$ob->are_match($response);
print "DEBUG: Waiting for \"$response\".\n" if $debug;
for (;;) {
return unless (defined ($gotit = $ob->lookfor(1)));
if ($gotit ne "") {
my ($match, $after, $pattern) = $ob->lastlook;
print "DEBUG: Got .\"$gotit$match$after\".\n" if $debug;
return $match;
}
return if ($ob->reset_error);
}
}
This is some of the context in which it is called:
while ($in) {
$loc = $ob->read(1);
if ($loc) {
# do stuff
} else {
$ob->write(chr(05)); # send <ENQ>
waitfor(chr(06)) or die; # wait for <ACK>
...send next piece etc
}
}
This is the output I get from the sub and the die:
DEBUG: Waiting for "?".
Died at C:\horiba_pentra60Cplus.pl line 259, <UPLOAD> line 2.
(in cleanup) Can't call method "Call" on an undefined value at
C:/Perl/site/lib/Win32API/CommPort.pm line 211, <UPLOAD> line 2 during
global destruction.
Outbound and inbound communications are working otherwise. I am using
Portmon and it indicates that the chr(05) goes out successfully, there
is one read on the port which returns with length of 0 and then the port
closes.
Troubleshooting help or any advice greatfully received.
Regards,
Spin
------------------------------
Date: Mon, 28 Nov 2005 21:21:09 +0100
From: Witold Rugowski <witold.rugowski@hp.com>
Subject: Matching escaped delimiter chars
Message-Id: <438b66b5$1@usenet01.boi.hp.com>
Hi!
I want to match with regexp substring, which is delimited by, let's say ". It is trivial, but I don't know how to match escaped quotes with \. OK, example will be better ;-))
Let's take string such:
AAAAAAA "blah blah \" blah blah" BBBBBB
How to match all what is in between quotes, not counting escaped quote. In this case it should match to:
blah blah \" blah blah
How it can be done????
Best regards,
Witold Rugowski
------------------------------
Date: Mon, 28 Nov 2005 15:52:52 -0500
From: Sherm Pendley <sherm@dot-app.org>
Subject: Re: Matching escaped delimiter chars
Message-Id: <m2y8384hqz.fsf@Sherm-Pendleys-Computer.local>
Witold Rugowski <witold.rugowski@hp.com> writes:
> Let's take string such:
> AAAAAAA "blah blah \" blah blah" BBBBBB
>
> How to match all what is in between quotes, not counting escaped quote.
> In this case it should match to:
> blah blah \" blah blah
>
> How it can be done????
A very simplistic way of doing that would be to use a zero-width look-behind,
so that the end of the match is (in English) "a quote character that's not
immediately preceded by a backslash." In Perl:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'AAAAAAA "blah blah \" blah blah" BBBBBB';
if ($string =~ /\"(.*)(?<!\\)\"/) {
print $1, "\n";
}
As I said though, that's a pretty simplistic way of doing it. It works for
the specific example given, but may not work with real-world data. It does
not handle, for example, the case where the string you're interested in ends
in a backslash which is escaped - i.e. blah\\".
The special cases and "what if" scenarios like the above can get out of hand
rather quickly. For production use, I'd use something like the Text::Balanced
module on CPAN, or even a full-blown parser using Parse::RecDescent.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Tue, 29 Nov 2005 00:02:19 -0800
From: robic0
Subject: Re: Matching escaped delimiter chars
Message-Id: <sn2oo1pu3bpdgeil7buc35spe1ifgknuru@4ax.com>
On Mon, 28 Nov 2005 21:21:09 +0100, Witold Rugowski
<witold.rugowski@hp.com> wrote:
>Hi!
>I want to match with regexp substring, which is delimited by, let's say ". It is trivial, but I don't know how to match escaped quotes with \. OK, example will be better ;-))
>
>Let's take string such:
>AAAAAAA "blah blah \" blah blah" BBBBBB
>
>How to match all what is in between quotes, not counting escaped quote. In this case it should match to:
>blah blah \" blah blah
>
>How it can be done????
>
>Best regards,
>Witold Rugowski
my @regx_esc_codes = ( "\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@' );
my $funky_string = 'AAAAAAA "blah blah \" blah blah" BBBBBB';
my $match_string = 'your_logic_here';
for (@regx_esc_codes)
{
my $tc = $_;
# code template for regex
my $xx = "\$match_string =~ s/\\$tc/\\\\\\$tc/g;";
eval $xx;
#print "$xx\n";
}
## match should be ready now,
## be sure to trap regex violations
$fnd = 0;
# -- #
my $ctmpl = "if (\$funky_string =~ /$match_string/ {\$fnd = 1;}";
eval $ctmpl;
# -- #
if ($@) {
## Check the $ctmpl, get the control code, log this error as a
code issue.
## This shouldn't happen ... the compiler will show the escape
char, add
## the char to "@regx_esc_codes", now its fixed!
$@ =~ s/^[\x20\n\t]+//; $@ =~ s/[\x20\n\t]+$//;
print $@,"\n";
exit;
}
Note - I may be wrong in this context, its been a while
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8718
***************************************