[28938] in Perl-Users-Digest
Perl-Users Digest, Issue: 182 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 28 21:09:51 2007
Date: Wed, 28 Feb 2007 18:09:11 -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 Wed, 28 Feb 2007 Volume: 11 Number: 182
Today's topics:
Re: change chr(9) ? <abigail@abigail.be>
Re: change chr(9) ? <noreply@gunnar.cc>
Re: change chr(9) ? <abigail@abigail.be>
convert string to hash of hash? <ppc@cheapbooks.com>
Re: convert string to hash of hash? <jgibson@mail.arc.nasa.gov>
Re: decode a string to "Perl's internal form" without E anno4000@radom.zrz.tu-berlin.de
Re: Hash of arrays of hashes... There's got to be a cle <mark.clementsREMOVETHIS@wanadoo.fr>
Re: Parent process unable to read messages from child p xhoster@gmail.com
Re: Parent process unable to read messages from child p <himagauri@gmail.com>
Re: Parent process unable to read messages from child p anno4000@radom.zrz.tu-berlin.de
Re: Parent process unable to read messages from child p <himagauri@gmail.com>
Re: Parent process unable to read messages from child p xhoster@gmail.com
Re: Parent process unable to read messages from child p anno4000@radom.zrz.tu-berlin.de
Re: perl & heredoc to create xml <tadmc@augustmail.com>
Re: Perl and MySQL <tim@compuserve.com>
Re: Perl and MySQL <no@email.com>
Re: sysread & eof <lstouder@teksavvy.com>
Re: sysread & eof xhoster@gmail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 28 Feb 2007 20:49:01 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: change chr(9) ?
Message-Id: <slrneubql5.d9.abigail@alexandra.abigail.be>
Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on MMMMCMXXIX September
MCMXCIII in <URL:news:54lqbqF21ebi0U1@mid.individual.net>:
][ Abigail wrote:
][ > max wrote:
][ > !! I have txt file with chr(9) in text, how to change chr(9) - horizontal tab
][ > !! to chr(32)"space"
][ >
][ > I'd use 'tr'. But that isn't Perl.
][
][ Why do you say that 'tr' is not Perl?
$ which tr
/usr/bin/tr
$ which perl
/opt/perl/bin/perl
Abigail
--
#!/opt/perl/bin/perl -- # Remove trailing newline!
BEGIN{$SIG{__WARN__}=sub{$_=pop;y-_- -;print/".*(.)"/;
truncate$0,-1+-s$0;exec$0;}}//rekcaH_lreP_rehtona_tsuJ
------------------------------
Date: Thu, 01 Mar 2007 00:34:49 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: change chr(9) ?
Message-Id: <54mhpnF21disdU1@mid.individual.net>
Abigail wrote:
> Gunnar Hjalmarsson wrote:
> ][ Abigail wrote:
> ][ > max wrote:
> ][ > !! I have txt file with chr(9) in text, how to change chr(9) - horizontal tab
> ][ > !! to chr(32)"space"
> ][ >
> ][ > I'd use 'tr'. But that isn't Perl.
> ][
> ][ Why do you say that 'tr' is not Perl?
>
> $ which tr
> /usr/bin/tr
> $ which perl
> /opt/perl/bin/perl
Well, since this is a Perl group, the readers had good reasons to assume
that you were referring to the Perl operator tr///.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 28 Feb 2007 23:43:28 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: change chr(9) ?
Message-Id: <slrneuc4sa.d9.abigail@alexandra.abigail.be>
Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on MMMMCMXXIX September
MCMXCIII in <URL:news:54mhpnF21disdU1@mid.individual.net>:
}} Abigail wrote:
}} > Gunnar Hjalmarsson wrote:
}} > ][ Abigail wrote:
}} > ][ > max wrote:
}} > ][ > !! I have txt file with chr(9) in text, how to change chr(9) - horizontal tab
}} > ][ > !! to chr(32)"space"
}} > ][ >
}} > ][ > I'd use 'tr'. But that isn't Perl.
}} > ][
}} > ][ Why do you say that 'tr' is not Perl?
}} >
}} > $ which tr
}} > /usr/bin/tr
}} > $ which perl
}} > /opt/perl/bin/perl
}}
}} Well, since this is a Perl group, the readers had good reasons to assume
}} that you were referring to the Perl operator tr///.
Which is why I said "But that isn't Perl". Had he asked in a non-Perl
newsgroup, all I had answered was "I'd use 'tr'.".
Abigail
--
perl -wle 'print "Prime" if (0 x shift) !~ m 0^\0?$|^(\0\0+?)\1+$0'
------------------------------
Date: 28 Feb 2007 17:01:44 -0800
From: "dt" <ppc@cheapbooks.com>
Subject: convert string to hash of hash?
Message-Id: <1172710904.322814.6640@t69g2000cwt.googlegroups.com>
I have a data set such as this:
$ref{"a"}{"b"}[0]{"c"}
any way to take a string such as:
"a:b:[0]:c" and get the value stored in $ref{"a"}{"b"}[0]{"c"}?
does using Data::Dumper help?
I want to be able to put a string in a template file which gets the
value from the datastructure,
------------------------------
Date: Wed, 28 Feb 2007 17:42:55 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: convert string to hash of hash?
Message-Id: <280220071742559106%jgibson@mail.arc.nasa.gov>
In article <1172710904.322814.6640@t69g2000cwt.googlegroups.com>, dt
<ppc@cheapbooks.com> wrote:
> I have a data set such as this:
>
> $ref{"a"}{"b"}[0]{"c"}
>
> any way to take a string such as:
>
> "a:b:[0]:c" and get the value stored in $ref{"a"}{"b"}[0]{"c"}?
>
> does using Data::Dumper help?
>
> I want to be able to put a string in a template file which gets the
> value from the datastructure,
>
Sure. Split your multi-level key on ':' and follow the links.
jgibson 47% cat dt.pl
#!/usr/local/bin/perl
use strict;
use warnings;
my %ref;
$ref{"a"}{"b"}[0]{"c"} = 'Got it!';
my $key = 'a:b:[0]:c';
my $p = \%ref;
for ( split(/:/,$key) ) {
if( /\[(\d+)\]/ ) {
$p = ${$p}[$1];
}else{
$p = ${$p}{$_};
}
}
print "data['$key'] = $p\n";
jgibson 48% perl dt.pl
data['a:b:[0]:c'] = Got it!
However, you should add some error checking in-case your specified
element or some predecessor element does not exist, or the specified
element is not a scalar itself.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: 28 Feb 2007 19:12:45 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: decode a string to "Perl's internal form" without Encode module?
Message-Id: <54m2hdF21f2h7U1@mid.dfncis.de>
Raymundo <gypark@gmail.com> wrote in comp.lang.perl.misc:
[please don't top-post]
> Hmm... I have seen a web-hosting server in which Perl 5.6 was
> installed and there wasn't Encode module. :-D
From your original posting:
I want to decode it so that it become a string in "Perl's internal
form" (that is, unicode form.. is it so called "utf8"?).
That implies a Perl that does include Encode.
[tofu snipped]
Anno
------------------------------
Date: Wed, 28 Feb 2007 22:52:30 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Hash of arrays of hashes... There's got to be a cleaner way than this...
Message-Id: <45e5f99e$0$5091$ba4acef3@news.orange.fr>
tetchy wrote:
> Hi,
>
> I've been modifying a perl script that reads a configuration file,
> validates the contents and generates a bunch of system configuration
> files and an iptables firewall set. I discovered that the change from
> ifconfig to iproute2 style files required a change in the way that
> aliases were handled so the data no-longer fitted into my hash of arrays.
>
> So I have created a hash keyed by interface containing an array of
> hashes keyed by interface values ip, mtu, qdisc... etc. It took a while
> but I now have a solution just it's a bit long winded and involves
> creating a temporary hash.
<snip>
>>
>> my %theInterfaceHash = (
>> 'lo' => [ { ip => '127.0.0.1/8', mtu => '16436' } ],
>> 'eth0' => [ { ip => '192.168.58.23/24', mtu => '1500' }, { ip => '192.168.48.23/24', mtu => '1500' }, { ip => '172.16.16.23/16', mtu => '1492' } ],
>> 'eth1' => [ { ip => '192.168.44.23/24', mtu => '1500' } ]
>> );
I'd keep this data in an external file (YAML, XML or somesuch), and use
a templating system to generate the config files.
eg
Template::Toolkit
HTML::Template (HTML is in the name, but it isn't restricted to HTML)
Mark
------------------------------
Date: 28 Feb 2007 19:09:51 GMT
From: xhoster@gmail.com
Subject: Re: Parent process unable to read messages from child process
Message-Id: <20070228141503.652$Px@newsreader.com>
"Gauri" <himagauri@gmail.com> wrote:
> Hello,
>
> I've a process which is supposed to do the following:
> 1.A Parent process has to fork to create fixed number of child
> processes. (This fixed number changes each time my code runs.)
> 2. Each child computes certain values and writes these values on to a
> pipe.
> 3. The parent should read each of these values from pipe output of all
> children and store them in a global variable to be used later in the
> process.
>
> Fork work correct, Child processes compute the correct value, however
> Parent is unable to read output of all children. For e.g. when 3
> children are created, one child completes execution first, and parent
> is able to read it's value. The other two processes complete 20
> seconds later and exit immediately one after the other. The parent is
> unable to read the values of the 2nd and 3rd child.
First of all, you should be using strict and warnings. If you did
so, it probably would have helped you identify the problem.
> $count = 0;
> while(1){
> foreach my $client ($selectpipe->can_read) {
> $filedetail[$count] = <$uncompclient>;
> $count++;
> }
> }
Since $client is what can_read returned, you should be reading from
$client, not $uncompclient, which seems to be used no where else and
so probably undefined. You also need to remove the handle from $selectpipe
once it reaches eof. And you probably shouldn't mix <> and read on the
same handles you are using Select on.
And since your code doesn't do anything meaningful until all the children
are done anyway, there is really no reason to use IO::Select at all. Just
keep a list of the file handles and do a series of blocking reads on them:
my @filedetail = map {<$_>} @list_of_child_handles;
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 28 Feb 2007 11:47:06 -0800
From: "Gauri" <himagauri@gmail.com>
Subject: Re: Parent process unable to read messages from child process
Message-Id: <1172692025.954773.300490@s48g2000cws.googlegroups.com>
Hi,
I appreciate the quick response.
Apologies for the typo--
The statement in the main code is:
foreach $uncompclient($selectpipe->can_read){
my @filedetail = map {<$_>} @list_of_child_handles;
Could you please explain series of blocking read in details with
respect to my code?
-Thanks a lot.
-Regards,
Gauri
On Feb 28, 2:09 pm, xhos...@gmail.com wrote:
> "Gauri" <himaga...@gmail.com> wrote:
> > Hello,
>
> > I've a process which is supposed to do the following:
> > 1.A Parent process has to fork to create fixed number of child
> > processes. (This fixed number changes each time my code runs.)
> > 2. Each child computes certain values and writes these values on to a
> > pipe.
> > 3. The parent should read each of these values from pipe output of all
> > children and store them in a global variable to be used later in the
> > process.
>
> > Fork work correct, Child processes compute the correct value, however
> > Parent is unable to read output of all children. For e.g. when 3
> > children are created, one child completes execution first, and parent
> > is able to read it's value. The other two processes complete 20
> > seconds later and exit immediately one after the other. The parent is
> > unable to read the values of the 2nd and 3rd child.
>
> First of all, you should be using strict and warnings. If you did
> so, it probably would have helped you identify the problem.
>
> > $count = 0;
> > while(1){
> > foreach my $client ($selectpipe->can_read) {
> > $filedetail[$count] = <$uncompclient>;
> > $count++;
> > }
> > }
>
> Since $client is what can_read returned, you should be reading from
> $client, not $uncompclient, which seems to be used no where else and
> so probably undefined. You also need to remove the handle from $selectpipe
> once it reaches eof. And you probably shouldn't mix <> and read on the
> same handles you are using Select on.
>
> And since your code doesn't do anything meaningful until all the children
> are done anyway, there is really no reason to use IO::Select at all. Just
> keep a list of the file handles and do a series of blocking reads on them:
>
> my @filedetail = map {<$_>} @list_of_child_handles;
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> Usenet Newsgroup Service $9.95/Month 30GB- Hide quoted text -
>
> - Show quoted text -
------------------------------
Date: 28 Feb 2007 20:28:08 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Parent process unable to read messages from child process
Message-Id: <54m6uoF214sg8U1@mid.dfncis.de>
Gauri <himagauri@gmail.com> wrote in comp.lang.perl.misc:
> Hello,
>
> I've a process which is supposed to do the following:
> 1.A Parent process has to fork to create fixed number of child
> processes. (This fixed number changes each time my code runs.)
> 2. Each child computes certain values and writes these values on to a
> pipe.
> 3. The parent should read each of these values from pipe output of all
> children and store them in a global variable to be used later in the
> process.
>
> Fork work correct, Child processes compute the correct value, however
> Parent is unable to read output of all children. For e.g. when 3
> children are created, one child completes execution first, and parent
> is able to read it's value. The other two processes complete 20
> seconds later and exit immediately one after the other. The parent is
> unable to read the values of the 2nd and 3rd child.
>
> I want the parent to be able to read the values of all the children
> immediately after they exit.
> Can some one help me fix the problem?
>
> Here is my script:
Unfortunately, it isn't strict-compliant. That makes actual debugging
harder, so I'll only add a few remarks.
> use IO::Select;
> use IO::File;
>
> my $selectpipe = new IO::Select;
>
> for ($splitnum = 1; $splitnum <= $numparts; $splitnum++) { #$numparts
> changes each time.
> pipe($uncompread, $uncompwrite) or die "Cannot pipe: $!";
> my $pid = fork();
> die "fork: $!" unless defined $pid;
> if ($pid){#parent
> push (@childs, $pid);
> close $uncompwrite;
> $selectpipe->add($uncompread);
> } else { #child
> close $uncompread;
> $uncompwrite->autoflush(1);
>
> # compute the following arrays:
> # $uncompfile[$splitnum],
> # $uncompsize[$splitnum],
> # $uncompcksum[$splitnum]
> # and store the above values in the array: $uncomp[$splitnum
> - 1] as follows:
>
> $uncomp[$splitnum - 1] = "$uncompfile[$splitnum]
> $uncompsize[$splitnum] $uncompcksum[$splitnum]";
> print $uncompwrite $uncomp[$splitnum - 1];
>
> exit;
> }
> }
It appears that each child prints only a single line of output. That
may simplify things.
Up to here your code looks like you know what you are doing. That
ends here.
> # In the following part I'm trying to store the @uncomp() array above
> in the @filedetail() array.
> # The number of elements in @filedetail() array should be equal to
> $numparts above.
> # This is the part which causes error. I'm unable to read all elements
> in the @uncomp() array
> # above. After the first element in the @uncomp() array is read, the
> process exits. Can
> # somebody please help?
>
> $count = 0;
> while(1){
Starting an endless loop...
> foreach my $client ($selectpipe->can_read) {
> $filedetail[$count] = <$uncompclient>;
Why aren't you using the file handle that ->can_read returned to you?
Instead, you're reading from the fixed file handle $uncompclient
that is left over from the first part. That will only work once.
In a well-scoped program $uncompclient wouldn't even be in sight.
All file handles in this part should come from $selectpipe.
> $count++;
> }
> }
You did nothing to break the endless loop. I find it hard to believe
that the "process exits", as your comment above states.
> close $uncompread;
> close $uncompwrite;
>
> foreach(@childs){
> waitpid($_,0);
> }
>
> I'm using Perl version 5.8.5 on OS-Red Hat Linux.
If the amount of output of each child process is small, as your code
seems to indicate, a simple solution (without select) is possible.
It relies on the assumption that the kids never block on output and
*is* fragile. For a better solution, fix your original code.
use Vi::QuickFix;
use Data::Dumper;
my $numparts = 5;
my %pipes;
for (my $splitnum = 1; $splitnum <= $numparts; $splitnum++) {
pipe my ( $uncompread, $uncompwrite) or die "Cannot pipe: $!";
defined( my $pid = fork()) or die "fork: $!";
if ( $pid ) { # parent
close $uncompwrite;
$pipes{ $pid} = $uncompread;
} else { # child
close $uncompread;
sleep rand 5; # random execution time
print $uncompwrite "result of part $splitnum\n"; # some output
exit;
}
}
while ( ( my $pid = waitpid( -1, 0)) > 0 ) { # wait for next kid to be done
my $pipe = $pipes{ $pid}; # find its pipe
push @detail, scalar <$pipe>; # read output
}
chomp @detail; # make output more readable
print Dumper \ @detail;
__END__
Anno
------------------------------
Date: 28 Feb 2007 13:56:08 -0800
From: "Gauri" <himagauri@gmail.com>
Subject: Re: Parent process unable to read messages from child process
Message-Id: <1172699768.752650.123690@m58g2000cwm.googlegroups.com>
Hi,
Thanks for the quick. I tried to implement the solution you suggested
exactly as you've posted here, However, the parent process populates
the @detail array with only one pipe output. i.e. the output of the
child which exited first. Perhaps there is some delay between the
time, Child process writes to the pipe and parent reads from the
pipe.
Could someone suggest something more?
-Gauri
>
> If the amount of output of each child process is small, as your code
> seems to indicate, a simple solution (without select) is possible.
> It relies on the assumption that the kids never block on output and
> *is* fragile. For a better solution, fix your original code.
>
> use Vi::QuickFix;
> use Data::Dumper;
>
> my $numparts = 5;
> my %pipes;
> for (my $splitnum = 1; $splitnum <= $numparts; $splitnum++) {
> pipe my ( $uncompread, $uncompwrite) or die "Cannot pipe: $!";
> defined( my $pid = fork()) or die "fork: $!";
> if ( $pid ) { # parent
> close $uncompwrite;
> $pipes{ $pid} = $uncompread;
> } else { # child
> close $uncompread;
> sleep rand 5; # random execution time
> print $uncompwrite "result of part $splitnum\n"; # some output
> exit;
> }
> }
>
> while ( ( my $pid = waitpid( -1, 0)) > 0 ) { # wait for next kid to be done
> my $pipe = $pipes{ $pid}; # find its pipe
> push @detail, scalar <$pipe>; # read output
> }
> chomp @detail; # make output more readable
>
> print Dumper \ @detail;
> __END__
>
> Anno- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
------------------------------
Date: 28 Feb 2007 22:16:09 GMT
From: xhoster@gmail.com
Subject: Re: Parent process unable to read messages from child process
Message-Id: <20070228172123.836$SG@newsreader.com>
"Gauri" <himagauri@gmail.com> wrote:
> Hi,
> Thanks for the quick. I tried to implement the solution you suggested
> exactly as you've posted here, However, the parent process populates
> the @detail array with only one pipe output. i.e. the output of the
> child which exited first. Perhaps there is some delay between the
> time, Child process writes to the pipe and parent reads from the
> pipe.
It worked fine for me (well, once I declared @detail because I had added
use strict). Post exactly what you ran and what output you got.
> Could someone suggest something more?
Please don't top post.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 28 Feb 2007 23:18:28 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Parent process unable to read messages from child process
Message-Id: <54mgu4F217np3U1@mid.dfncis.de>
<xhoster@gmail.com> wrote in comp.lang.perl.misc:
> "Gauri" <himagauri@gmail.com> wrote:
> > Hi,
> > Thanks for the quick. I tried to implement the solution you suggested
> > exactly as you've posted here, However, the parent process populates
> > the @detail array with only one pipe output. i.e. the output of the
> > child which exited first. Perhaps there is some delay between the
> > time, Child process writes to the pipe and parent reads from the
> > pipe.
>
> It worked fine for me (well, once I declared @detail because I had added
> use strict).
Ah... sorry, that's two copy/paste errors. It should have had
strictures and warnings (instead of using Vi::QuickFix), and
@detail was supposed to be declared.
> Post exactly what you ran and what output you got.
>
> > Could someone suggest something more?
>
> Please don't top post.
Hehe :)
Anno
------------------------------
Date: Wed, 28 Feb 2007 19:05:12 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: perl & heredoc to create xml
Message-Id: <slrneuc9m8.7qv.tadmc@tadmc30.august.net>
LHradowy <lhradowy@gmail.com> wrote:
> chdir ("$fin_tck");
(perl isn't the shell.)
Please see:
perldoc -q vars
What’s wrong with always quoting "$vars"?
You should also check the return value to see if you actually
got what you asked for:
chdir( $fin_tck ) or die "could not cd to '$fin_tck' $!";
> system($command);
If you are interested in whether you actually got what you asked
for, then you should check the return value here too:
!system( $command ) or die "problem running sqlplus $?";
^ ^^
^ ^^
> chomp( my $date = `date '+%Y%B%d%H'` );
perldoc -f localtime
> open( TEMP, "$tempFile" );
You should always, yes *always*, check the return value from open():
open( TEMP, $tempFile ) or die "could not open '$tempFile' $!";
> s/\s+/,/g; # remove trailing whitespace
That does much more that merely removing trailing whitespace!
> my($tn,$account,$related) = split(",", $_);
Whitespace is not a scarce resource. Feel free to use as much of it
as you like to make your code easier to read and understand.
A regular expression should *look like* a regular expression:
my($tn, $account, $related) = split(/,/, $_);
> close OUT, AC_OUT;
That does not do what you think it does.
You should enable
use warnings;
use strict;
in every program that you write.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 28 Feb 2007 22:39:46 GMT
From: Tim <tim@compuserve.com>
Subject: Re: Perl and MySQL
Message-Id: <es50bi02dqr@news3.nntpjunkie.com>
"Charles A. Landemaine" <landemaine@gmail.com> wrote in
news:1172684648.037069.115240@q2g2000cwa.googlegroups.com:
> I have a MySQL table that is used to store comments on my blog. I just
> found out it's filled with spam. The table itself is 2 GB big, with
> more than half a million spam backlinks. I haven't found much material
> on how to interact between Perl and MySQL. What I'd like to do is do a
> simple Perl script that opens the DB table, goes through all rows and
> deletes all those which contain "<a href". I'll have to do that during
> the night, not to disrupt the server. How could I do that?
> Thanks,
>
Besides cleaning up the spam, wether you use Perl or directly accesing your
database with a MySQL client, you may want to check your current scripts to
make sure they minimize the chances of the same problem repeating itself.
As someone else had suggested, it would probably be easier to access your
SQL directly and clear the junk out there.
Tim
--
NNTPjunkie.com Unlimited Downloads $14.95 monthly
------------------------------
Date: Wed, 28 Feb 2007 22:48:31 +0000
From: Brian Wakem <no@email.com>
Subject: Re: Perl and MySQL
Message-Id: <54mf5vF1uo168U1@mid.individual.net>
Charles A. Landemaine wrote:
> I have a MySQL table that is used to store comments on my blog. I just
> found out it's filled with spam. The table itself is 2 GB big, with
> more than half a million spam backlinks. I haven't found much material
> on how to interact between Perl and MySQL. What I'd like to do is do a
> simple Perl script that opens the DB table, goes through all rows and
> deletes all those which contain "<a href". I'll have to do that during
> the night, not to disrupt the server. How could I do that?
> Thanks,
No need for Perl.
DELETE FROM table WHERE comments LIKE '%<a href%';
--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
------------------------------
Date: Wed, 28 Feb 2007 19:50:23 -0500
From: lucas <lstouder@teksavvy.com>
Subject: Re: sysread & eof
Message-Id: <190ce$45e6234b$cef8bf7a$14799@TEKSAVVY.COM-Free>
Ben Morrow wrote:
> Quoth lstouder@teksavvy.com:
> I don't know what makes you think that. Did you try it? sysread returns
> 0 on EOF.
>
> Note that you should also be checking defined($res), as sysread returns
> undef on errors. Depending on how you've opened the socket and your
> system, some errors (EINTR and EAGAIN, specifically) are not in fact
> errors, but indications that you need to retry the read.
>
> I think you need to read
>
> perldoc -f sysread
>
> and
>
> man 2 read
>
> before you go much further (the latter assumes you are on Unix; this is
> the documentation for the C read function which is what Perl's sysread
> calls. If you are on some other system you will need to find the
> appropriate documentation for yourself).
In my original post, I mentioned I am using Linux; Slackware in particular.
I have read the perldoc for sysread. The problem is sysread blocks. So,
when the last of the data is transmitted, $res has a value, the data is
processed, and when the loop goes back to read more data, there is none,
and sysread just sits, waiting for more.
So, I figure, the only way to get sysread to do this is with non-blocking.
Using IO::Select, I came up with this:
my $select = IO::Select->new($socket);
while (1) {
if (my $ready = $select->can_read(1)) {
my $res = sysread($socket,$_,4096);
last if(!defined($res)); #never used
print "RES:$res\nLEN:",length($_),"\n$_\n";
}
else { print "!READY\n"; last; }
}
I tried using "can_read(0)", so that it wouldn't wait, but it was too fast
and the "else { print "!READY\n"; last; }" statement would catch. Without
the else statement, the loop never exists. The "last if(!defined($res));"
is never used because the can_read line doesn't enter the loop if there
isn't any data.
I guess this is the best way to get this to work. It just seems a bit
clunky.
--
lucas
-------------------------
Perl Coder since 2001
shift || die;
-------------------------
------------------------------
Date: 01 Mar 2007 01:08:16 GMT
From: xhoster@gmail.com
Subject: Re: sysread & eof
Message-Id: <20070228201332.042$WO@newsreader.com>
lucas <lstouder@teksavvy.com> wrote:
> Ben Morrow wrote:
>
> In my original post, I mentioned I am using Linux; Slackware in
> particular. I have read the perldoc for sysread. The problem is sysread
> blocks.
It blocks waiting on more data. It doesn't block on eof, as there is no
more data to wait on.
> So, when the last of the data is transmitted, $res has a value,
> the data is processed, and when the loop goes back to read more data,
> there is none, and sysread just sits, waiting for more.
If that is actually the case, then the server is not sending an eof when it
should (or maybe it isn't supposed to send an eof and you are
misunderstanding the protocol).
....
>
> I guess this is the best way to get this to work. It just seems a bit
> clunky.
That often happens when you decline to use the existing modules and insist
on rolling your own.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
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 V11 Issue 182
**************************************