[22623] in Perl-Users-Digest
Perl-Users Digest, Issue: 4844 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Apr 14 18:10:49 2003
Date: Mon, 14 Apr 2003 15:10:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 14 Apr 2003 Volume: 10 Number: 4844
Today's topics:
Need perl to conect to mysql database <urzaserra@home.com>
Re: Need perl to conect to mysql database <Jeff@aetherweb.co.uk>
Re: Need perl to conect to mysql database <tzz@lifelogs.com>
Re: Network socket - binary data. <goldbb2@earthlink.net>
Re: Network socket - binary data. (Bryan Castillo)
Parse::RecDescent -- precedence and evaluation order <mjcarman@mchsi.com>
Photo gallery perl scripts <veino@bc.edu>
problem with this perl script <horneja@ufl.edu>
Re: problem with this perl script <Jeff@aetherweb.co.uk>
regular expression substitution and perl -p -i -e <osx@nospamntlworld.com>
Re: Shortcut <goldbb2@earthlink.net>
Re: Subroutines:: Return Type Vs Performance <kasp@epatra.com>
Re: Subroutines:: Return Type Vs Performance (Tad McClellan)
suggestions for perl upgrade (Jeff)
Re: suggestions for perl upgrade <abigail@abigail.nl>
Re: Thread-safe resolver? <goldbb2@earthlink.net>
To extract a portion of a text file (John Smith)
Re: To extract a portion of a text file <mbudash@sonic.net>
Re: Ugly code; what does it do? <michael.p.broida@boeing.com>
Re: Ugly code; what does it do? <michael.p.broida@boeing.com>
Re: Ugly code; what does it do? (Tad McClellan)
Re: variables in modules <Jamie_Bohr@Agilent.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 14 Apr 2003 11:52:17 -0700
From: "matt" <urzaserra@home.com>
Subject: Need perl to conect to mysql database
Message-Id: <rqDma.173$xk4.116@fed1read02>
I need some perl code that will connect to a mysql database and display the
results on the users browser, in exactly the same order as they are in the
database.
------------------------------
Date: Mon, 14 Apr 2003 20:22:05 +0100
From: "Jeff Snoxell" <Jeff@aetherweb.co.uk>
Subject: Re: Need perl to conect to mysql database
Message-Id: <b7f1nn$p5s$1@news7.svr.pol.co.uk>
"matt" <urzaserra@home.com> wrote in message
news:rqDma.173$xk4.116@fed1read02...
> I need some perl code that will connect to a mysql database and display
the
> results on the users browser, in exactly the same order as they are in the
> database.
>
use DBI;
use strict;
my $db = DBI->connect("DBI:mysql:$dbname:$hostname", $username, $password)
|| die 'could not connect: ' . DBI->errstr;
my $sth = $db->prepare("SELECT * FROM MyTable")
|| die "Could not prepare: " . $db->errstr;
$sth->execute() || die "Could not execute: " . $sth->errstr;
print "Content-type: text/html\n\n";
print "<table>";
while (my $record = $sth->fetchrow_hashref())
{
print "<tr>";
print "<td>" . $record->{'Field1'} . "</td>";
print "<td>" . $record->{'Field2'} . "</td>";
print "<td>" . $record->{'Field3'} . "</td>";
print "<td>" . $record->{'Field4'} . "</td>";
print "<td>" . $record->{'Field5'} . "</td>";
print "</tr>";
}
$sth->finish();
print "</table>";
-------
if the order is screwing up then append
ORDER BY xyz ASC
to your SQL
HTH
Jeff Snoxell
Aetherweb Ltd - http://www.bespoke-web-design-uk.co.uk
------------------------------
Date: Mon, 14 Apr 2003 16:00:51 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Need perl to conect to mysql database
Message-Id: <4nn0isrgr0.fsf@lockgroove.bwh.harvard.edu>
On Mon, 14 Apr 2003, urzaserra@home.com wrote:
> I need some perl code that will connect to a mysql database
Use the Class::DBI CPAN module. It's trivial to set up and use. I
assume it will serve your needs, though you haven't stated any of
them.
Class::DBI::mysql can automatically discover your columns, so your
code just specifies the MySQL server/username/password and you're
off.
> and display the results on the users browser, in exactly the same
> order as they are in the database.
This is a much more complex problem, since you don't specify how the
results are to be displayed. I would recommend looking at the
Class::DBI examples, they will probably show you the right way to go
through the database, and for the rest maybe the Template Toolkit or
HTML::Template will work for you (or CGI.pm if you're into that sort
of thing).
Ted
------------------------------
Date: Mon, 14 Apr 2003 12:48:25 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Network socket - binary data.
Message-Id: <3E9AE659.8E98ED88@earthlink.net>
Bart Lateur wrote:
>
> Ras wrote:
>
> >I have found lots of examples of how to pass ASCII data via IO
> >sockets, and its essentially the same as a chat server or webserver...
> >
> >what i am having a hard time finding data on is how to pass binary
> >data (many examples state they don't work with binary data, but don't
> >give an alternative.)
> >
> >i am trying to move data from one system to another without using FTP.
>
> Don't use binmade(). That's basically it.
Hmm, I dunno -- if you've created a perlio layer specially designed to
decode the binary data your protocol uses, then binmode() is the way
that you would push that layer onto the socket handle. :)
Oh, and doing binmode(SOCKET) (just one argument, no extra args) is
harmless, because it doesn't do anything.
> Oh, and don't use the <SOCKET> syntax to read the data.
Right, not unless you've set $/ to a reference to a number, or to undef.
> Binary data doesn't consist of lines. Use read() or sysread() instead.
> I never quite understood the difference between the two,
Read uses stdio or perlio buffers -- it will perform multiple calls to
the C-read function, until it's either gotten the amount of data you've
requested, or until it hits EOF. Sometimes, it will read more data than
you've requested, putting the excess into a buffer.
Sysread doesn't use buffers -- it will perform precisely ONE call to the
C-read function.
Using read(), or using <>, can (and almost certainly eventually *will*)
cause select()/IO::Select to become confused, and result in deadlock.
If you don't use multiplexing (select/IO::Select), then it's ok to use
read() or <>.
> I think that disserence is actually quite system-dependent anyway. If
> anybody feels like shedding a light on this, please feel free to do so.
No, it's not especailly system dependent. Well, it slightly is -- if
you use read(), you're using stdio, and stdio may differ slightly from
system to system... but not by much.
> You can always base64- encode your data, that way you can transmit data
> as text and make sure it doesn't get corrupted on the way.
Alas, that only works if you can design both the server and the client.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: 14 Apr 2003 11:13:21 -0700
From: rook_5150@yahoo.com (Bryan Castillo)
Subject: Re: Network socket - binary data.
Message-Id: <1bff1830.0304141013.3b44b5ef@posting.google.com>
Bart Lateur <bart.lateur@pandora.be> wrote in message news:<m56l9vg0med2a9s8ct039t1ob9o1b7n5s6@4ax.com>...
> Ras wrote:
>
> >I have found lots of examples of how to pass ASCII data via IO sockets, and
> >its essentially the same as a chat server or webserver...
> >
> >what i am having a hard time finding data on is how to pass binary data
> >(many examples state they don't work with binary data, but don't give an
> >alternative.)
> >
> >i am trying to move data from one system to another without using FTP.
>
> Don't use binmade(). That's basically it. Oh, and don't use the <SOCKET>
> syntax to read the data. Binary data doesn't consist of lines. Use
> read() or sysread() instead. I never quite understood the difference
> between the two, I think that disserence is actually quite
> system-dependent anyway. If anybody feels like shedding a light on this,
> please feel free to do so.
Using <> to read data will end up doing end of line translation, while
sysread will not. If you are using windows and you read a line with
<> and the mode is set to ":crlf" you will have a LF at the end of
the line, however if you had the mode set to ":raw" you would have
CRLF at the end of the line.
So, if you are going to use <> to read the data make sure you do use
binmode to set the mode to ":raw". You would probably want to make
sure IO is unbuffered also, which can be done by setting $| to 1.
Overall, I think bart is right in saying you should just use sysread
and syswrite. (Especially if you need to use (4-arg) select, or might
need to in the future.)
>
> You can always base64- encode your data, that way you can transmit data
> as text and make sure it doesn't get corrupted on the way.
------------------------------
Date: Mon, 14 Apr 2003 16:36:03 -0500
From: Michael Carman <mjcarman@mchsi.com>
Subject: Parse::RecDescent -- precedence and evaluation order
Message-Id: <b7f9k4$9u6@onews.collins.rockwell.com>
I'm trying to build a parser to turn some pseudocode into a parse-tree
for later manipulation. Parse::RecDescent looked like the right tool for
the job, so I've been trying to bring myself up to speed with it. I'm
running into problems trying to handle precedence, and (particularly)
order of evaluation:
(Simplified code, though still a little long.)
#!/usr/bin/perl
BEGIN {require 5.006}
use strict;
use warnings;
use Parse::RecDescent;
sub branch {return bless {op => shift @_, args => [@_]}, 'Branch'}
sub leaf {my $s = shift; return bless \$s, 'Leaf'}
my $grammar = <<'_END_GRAMMAR_';
startrule : /^/ expr /$/ { $item{expr} }
expr :
unary(?) atom boolean <commit> expr
{
if ($#{$item{unary}} > -1) {
$item{unary}[0]{args}[0] = $item{atom};
$item{boolean}{args}[0] = $item{unary}[0];
}
else {
$item{boolean}{args}[0] = $item{atom};
}
$item{boolean}{args}[1] = $item{expr};
$item{boolean};
}
| unary expr
{
$item{unary}{args}[0] = $item{expr};
$item{unary};
}
| unary(?) group boolean <commit> expr
{
if ($#{$item{unary}} > -1) {
$item{unary}[0]{args}[0] = $item{group};
$item{boolean}{args}[0] = $item{unary}[0];
}
else {
$item{boolean}{args}[0] = $item{group};
}
$item{boolean}{args}[1] = $item{expr};
$item{boolean};
}
| group | atom | <error>
group : '(' expr ')' { $item{expr} }
unary : ( 'not' | '+' | '-' ) { ::branch($item[1]) }
boolean : ( 'and' | 'or' | 'xor' ) { ::branch($item[1]) }
atom : parameter | literal
parameter : /\b[a-z_][a-z0-9_]*\b/i { ::leaf($item[1]) }
literal : /[+-]?(?:\d+(?:\.\d*)?|\.\d+)/ { ::leaf($item[1]) }
_END_GRAMMAR_
my $parser = Parse::RecDescent->new($grammar);
use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper $parser->startrule('A and B and C');
__END__
$VAR1 = bless( {
'args' => [
bless( do{\(my $o = 'A')}, 'Leaf' ),
bless( {
'args' => [
bless( do{\(my $o = 'B')}, 'Leaf' ),
bless( do{\(my $o = 'C')}, 'Leaf' )
],
'op' => 'and'
}, 'Branch' )
],
'op' => 'and'
}, 'Branch' );
I want operators with the same precedence to evaluate left-to-right, but
as you can see from the the dump of the tree, I'm getting right-to-left.
I understand why, but I can't figure out how to fix it -- at least not
without making the grammar left-recursive, which P:RD doesn't allow.
-mjc
------------------------------
Date: Mon, 14 Apr 2003 12:28:10 -0400
From: Matt Veino <veino@bc.edu>
Subject: Photo gallery perl scripts
Message-Id: <3E9AE19A.7010204@bc.edu>
Hello, I'm looking for a Perl script to simplify making a photo webpage...
To see the type of page that I'm looking to automate, check out my
current webpage at http://www.phishyphotos.com
What I'd like is to have a fully customizable page, so my webpage just
doesn't have a "click the directory" look to it.
Maybe what would even be best is, a script that creates code that I can
SSI in the layout table on the right side of my current page.
Basically, I'd like to be able to upload a directory to the website and
have the links be updated automatically.
But, I'd like to eventually put up more stuff than just pictures, so I
have to be able to edit the "main page" to make it look like a typical
webpage with links, not a photo album (like a lot of scripts I've found)
I'd appreciate any suggestions for pre-written scripts. Thanks a lot.
------------------------------
Date: Mon, 14 Apr 2003 13:26:57 -0400
From: "Jim Horne" <horneja@ufl.edu>
Subject: problem with this perl script
Message-Id: <b7er14$6skp$1@ID-148893.news.dfncis.de>
We were running this script successfully on a Linux box running apache. We
swapped to a W2K server running Apache 1.3 and perl. A most of our perl
scripts run fine but the one below returns an error:
[an error occurred while processing this directive]
We used the following to call the script:
<!--#exec cmd="/home/httpd/cgi-bin/Index/makeindex.pl"-->
We now use:
<!--#exec cmd="C:/perl/cgi-bin/Index/makeindex.pl"-->
And the script returns the above error...
Any ideas?
Thanks , Jim
#!c:/perl/bin/wperl
#
# makeindex -- read files from a directory and output an html list of
links
# named after html title or filename if title not present
#
# M. Conlon February 4, 2000
# modified for shtml use -cjp20000621
#
#
#
# Loop over the names of the HTML files and create a link for each one #
while(<*.htm>){
print_link($_);
}
while(<*.html>){
print_link($_);
}
while(<*.shtml>){
print_link($_);
}
sub print_link {
$filename = $_[0];
($filename eq $ENV{'DOCUMENT_NAME'}) && return;
if (open(FILE,"< $filename")) {
while (<FILE>) {
if ((m!<title>(.*)</title>!i) ||
(m/<!--#set\s+var="title"\s+value="([^"]*)"-->/i)) {
print "\<li\>\<a href=\"$filename\"\>$1\<\/a\>\n";
close(FILE);
return;
}
}
close(FILE);
}
print "\<li\>\<a href=\"$filename\"\>$filename\<\/a\>\n";
return;
}
------------------------------
Date: Mon, 14 Apr 2003 20:24:36 +0100
From: "Jeff Snoxell" <Jeff@aetherweb.co.uk>
Subject: Re: problem with this perl script
Message-Id: <b7f1se$otd$1@news8.svr.pol.co.uk>
"Jim Horne" <horneja@ufl.edu> wrote in message
news:b7er14$6skp$1@ID-148893.news.dfncis.de...
> We were running this script successfully on a Linux box running apache.
We
> swapped to a W2K server running Apache 1.3 and perl. A most of our perl
> scripts run fine but the one below returns an error:
>
> [an error occurred while processing this directive]
>
> We used the following to call the script:
>
> <!--#exec cmd="/home/httpd/cgi-bin/Index/makeindex.pl"-->
>
> We now use:
>
> <!--#exec cmd="C:/perl/cgi-bin/Index/makeindex.pl"-->
>
> And the script returns the above error...
>
> Any ideas?
>
> Thanks , Jim
The error message is of no assistance because it's the result of a failed
SSI call. You need to run:
perl -c makeindex.pl
at a prompt in the C:/perl/cgi-bin/Index/ folder
in order to get a more useful error message that will lead you to the root
of your problem.
HTH
Jeff Snoxell
Aetherweb Ltd - http://www.bespoke-web-design-uk.co.uk
------------------------------
Date: Mon, 14 Apr 2003 22:55:39 +0100
From: osx <osx@nospamntlworld.com>
Subject: regular expression substitution and perl -p -i -e
Message-Id: <pan.2003.04.14.22.55.39.521400.1266@nospamntlworld.com>
Hi
I've got thousands of files on my computer and I'd like to rename each so
that they look much tidier and structured.
I am trying to use the "perl -p -i -e" command to do so but am not
getting the expected results.
An example
current filename: Quantum Computing.htm
I'd like to remove all capital letters and whitespace.
Ideally I'd like: quantum_computing.htm
I not very good with regular expressions (as you might have guessed!) and
have had a Google but can not get it to work.
I've tried both of these and a few variations with single quotes but have had no
success.
perl -p -i -e 'tr/A-Z/a-z/' 'Quantum Computing.htm'
perl -p -i -e 's/([A-Z])/([a-z])/gi' Quantum\ Computing.htm
Any help would be really appreciated.
Many thanks
David
------------------------------
Date: Mon, 14 Apr 2003 12:22:37 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Shortcut
Message-Id: <3E9AE04D.B4C7B6FF@earthlink.net>
"Jürgen Exner" wrote:
[snip]
> The OP said "swapping" and his code uses the classic way of doing it:
> swapping the values of the two variables $a and $b by using a temporary
> third variable $c (which is a poor choice in naming, $tmp would have
> made his intentions clearer). And he was asking for a simpler way to do
> this, i.e. how to swap $a and $b without the use of a third variable. A
> very classic problem that cannot be solved in e.g. C, Pacal, or Modula.
[OT] Slightly untrue. You can swap two variables in C without a third
variable:
a ^= b;
b ^= a;
a ^= b;
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Mon, 14 Apr 2003 21:00:10 +0530
From: "Kasp" <kasp@epatra.com>
Subject: Re: Subroutines:: Return Type Vs Performance
Message-Id: <b7ek67$nnc$1@newsreader.mailgate.org>
Yes Tad, I meant List Context. Sorry about that.
This quote is coming from a program's comment that I have my hands on
now...probably one of the old coders who is now not around :-(
I understand the concept of stack that you mentioned. The fact of a variable
being passed involves it being pushed and later popped from the stack.
But I want to now if there are any idiosyncrasies in Perl. For eg the way it
handles lists etc that do something special when being passed around.
As in C/C++, arrays are not passed-by-value, rather their address is
passed - making it a pass-by-reference. Also when I want to return an array
from a routine, I return it's address back to the calling function.
TIA.
--
"Accept that some days you are the pigeon and some days the statue."
"A pat on the back is only a few inches from a kick in the butt." - Dilbert.
------------------------------
Date: Mon, 14 Apr 2003 16:29:25 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Subroutines:: Return Type Vs Performance
Message-Id: <slrnb9ma1l.ivq.tadmc@magna.augustmail.com>
Kasp <kasp@epatra.com> wrote:
> Yes Tad, I meant List Context. Sorry about that.
>
> This quote is coming from a program's comment that I have my hands on
> now...probably one of the old coders who is now not around :-(
Someone who didn't see, or just didn't understand this Perl FAQ:
What is the difference between a list and an array?
> I understand the concept of stack that you mentioned. The fact of a variable
> being passed involves it being pushed and later popped from the stack.
> But I want to now if there are any idiosyncrasies in Perl. For eg the way it
> handles lists etc that do something special when being passed around.
What kind of lists do something special when being passed around?
I can't think of one.
Did you mean "array" instead of "list" again?
Are you talking about something other than a tied array?
Got code?
> As in C/C++, arrays are not passed-by-value, rather their address is
> passed -
A "reference" is not an "address", as in C/C++.
In Perl a reference is a reference, you cannot do arithmetic on
it like you can in C et. al.
> making it a pass-by-reference.
But you're right in that it _is_ pass-by-reference, which is why
nearly every subroutine's first action is to copy @_ into variables
and operate only on those variables, never on @_ itself directly.
Effectively converting it to pass-by-value.
Unless you _want_ it to be pass-by-reference of course.
> Also when I want to return an array
> from a routine, I return it's address back to the calling function.
You return a "reference" rather than an address. This is not C. :-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 14 Apr 2003 08:10:15 -0700
From: jc_cann@yahoo.com (Jeff)
Subject: suggestions for perl upgrade
Message-Id: <bf5acf77.0304140710.4a65da5@posting.google.com>
Greetings.
Our shop has perl 5.004_04 in production. Yes, it's been about 5
years since we put it there, but this version perl has been so
successful, we haven't really needed to upgrade. There is also the
issue of FUD by some of our SAs regarding perl upgrades, specifically
module dependancies.
As you can imagine, we have a lot of perl code. Some of it is in web
applications, and a lot more is in system code - perl became our
replacement for UNIX shell. So, most of the work in upgrading perl
would be for testing, in particular new versions of modules.
Unfortunately, we need to upgrade perl to use the Expect perl module.
IO-Pty does not work on Solaris unless you are on 5.005_03.
So, I am fishing for comments, suggestions and war stories about
upgrading perl. It seems to me that we could drop in the 5.005_03
version with a minimal amount of pain and without recompiling our
dynamically loaded modules. OTH, it would be nice to get to a newer
version of perl especially since we will have to disrupt the
environment anyway.
Finally, what are general strategies used by large IT departments on
keeping up with new versions of perl? Mind you, we'll spend a year
testing an OS upgrade...
Thanks in advance.
Jeff
------------------------------
Date: 14 Apr 2003 18:22:01 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: suggestions for perl upgrade
Message-Id: <slrnb9lv29.jcl.abigail@alexandra.abigail.nl>
Jeff (jc_cann@yahoo.com) wrote on MMMDXIII September MCMXCIII in
<URL:news:bf5acf77.0304140710.4a65da5@posting.google.com>:
~~ Greetings.
~~
~~ Our shop has perl 5.004_04 in production. Yes, it's been about 5
~~ years since we put it there, but this version perl has been so
~~ successful, we haven't really needed to upgrade. There is also the
~~ issue of FUD by some of our SAs regarding perl upgrades, specifically
~~ module dependancies.
~~
~~ As you can imagine, we have a lot of perl code. Some of it is in web
~~ applications, and a lot more is in system code - perl became our
~~ replacement for UNIX shell. So, most of the work in upgrading perl
~~ would be for testing, in particular new versions of modules.
~~
~~ Unfortunately, we need to upgrade perl to use the Expect perl module.
~~ IO-Pty does not work on Solaris unless you are on 5.005_03.
~~
~~ So, I am fishing for comments, suggestions and war stories about
~~ upgrading perl. It seems to me that we could drop in the 5.005_03
~~ version with a minimal amount of pain and without recompiling our
~~ dynamically loaded modules. OTH, it would be nice to get to a newer
~~ version of perl especially since we will have to disrupt the
~~ environment anyway.
~~
~~ Finally, what are general strategies used by large IT departments on
~~ keeping up with new versions of perl? Mind you, we'll spend a year
~~ testing an OS upgrade...
If you are hesitant about rolling out new versions, why bother rolling
out something that's 4 years old, and unsupported? If you find a bug
in that version *IT WILL NOT BE FIXED*. Perl is 2 major releases away
from 5.005_03. I'd say, either go for 5.6.1, 5.8.0 or wait for 5.8.1,
which shouldn't take too long to hit the streets.
Testing an upgrade for a long time is good. And I can understand being
hestitant about upgrading if there's no need. But upgrading to another
old and retired version doesn't make much sense to me.
Abigail
--
perl -MTime::JulianDay -lwe'@r=reverse(M=>(0)x99=>CM=>(0)x399=>D=>(0)x99=>CD=>(
0)x299=>C=>(0)x9=>XC=>(0)x39=>L=>(0)x9=>XL=>(0)x29=>X=>IX=>0=>0=>0=>V=>IV=>0=>0
=>I=>$==-2449231+gm_julian_day+time);do{until($=<$#r){$_.=$r[$#r];$=-=$#r}for(;
!$r[--$#r];){}}while$=;$,="\x20";print+$_=>September=>MCMXCIII=>=>=>=>=>=>=>=>'
------------------------------
Date: Mon, 14 Apr 2003 15:18:47 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Thread-safe resolver?
Message-Id: <3E9B0997.3BE31502@earthlink.net>
Ryan Thompson wrote:
>
> Hi All,
>
> I'm using iThreads in 5.8.0 on FreeBSD 4.7 for what is (basically) a
> producer-consumer problem. Several producer threads are created to
> look up DNS information from many hosts, the results of which are
> passed to a single consumer thread and merged for output.
>
> The threads are working wonderfully. However, Net::DNS is not ;-)
> (Memory leaks and core dumps when threaded).
Hmm. Net::DNS is pure-perl. I'm surprised that you have problems.
Any chance you could show us a minimal working perl program which
demonstrates those problems?
> So, as a horrible hack, I've been forced to exec host(1) and grab its
> exit value. Yes, it's slow. Yes, it's still many times faster than
> doing the lookups synchronously. :-)
How about having multiple threads use the gethostbyname() function?
Or, use Net::DNS without threads, and use bgsend to start multiple
queries, and IO::Select to get the addresses as they become available?
Something like:
my $res = Net::DNS::Resolver->new;
my $sel = IO::Select->new(map $res->bgsend($_), @hosts);
while( my @ok = $sel->can_read ) {
$sel->remove(@ok);
$res->bgread($_)->print for @ok;
}
Or maybe even the much simpler:
my $res = Net::DNS::Resolver->new;
for my $sock (map $res->bgsend($_), @hosts) {
$res->bgread($sock)->print;
}
Of course, there's probably a limit on the number of sockets you can
have open, so you'd likely want to avoid too many parallel queries...
my $res = Net::DNS::Resolver->new;
my $sel = IO::Select->new(map $res->bgsend($_), splice @hosts, 0, 10);
while( my @ok = $sel->can_read ) {
my $n = @ok;
$sel->remove(@ok);
$res->bgread($_)->print for splice @ok;
$sel->add(map $res->bgsend($_), splice @hosts, 0, 10 - $n);
}
or:
my $res = Net::DNS::Resolver->new;
my @socks = map $res->bgsend($_), splice @hosts, 0, 10;
while( @socks ) {
shift(@socks)->bgread($sock)->print;
push @socks, $res->bgsend(shift @hosts) if @hosts;
}
[all code here is untested]
> I've scoured CPAN, the web, and several group archives, but haven't
> been able to find a solution. Any suggestions? Or will I have to roll
> my own?
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: 14 Apr 2003 08:44:57 -0700
From: clearguy02@yahoo.com (John Smith)
Subject: To extract a portion of a text file
Message-Id: <500f84f3.0304140744.47b7ceaf@posting.google.com>
Hi all,
I am working on the below perl script:
I have a big file (C:\test.txt) with 100s of lines. Out of all the
lines, there is a repetetive block of code and I need to extract only
that stuff and ignore the rest of the lines in the file:
Example: (test.txt)
--------------------
first line...
second line....
.................
Test Case : test1_john
test2_rob
Surprise Key : hi_ there
........
again some lines of code.....
Test Case : test3_john
test5_will
test10_smith
Surprise Key : hi_there2
hi_there3
................
.............
again some lines of code...........
..........
-------------------------------------
Here, the fields, Test Case and Surprise key appear many times with
different values each time. All the values for the "Test Case" and
"Surprise Key" fileds should be added together at one place. The
values don't follow any indentation.
Now, the out put should like as follows:
----------------------
Test Case : test1_john
test2_rob
test3_john
test5_will
test10_smith
Surprise Key : hi_ there
hi_there2
hi_there3
------------------------------------------------
I have below part of script:
-----------------------------------
#! C:/perl/bin/perl
$file = "C:\\test.txt);
open (FILE, $file) || die "Can not open the file: $!";
while (<FILE>)
{
next unless (/^\s*Test/.../^\s*Surprise/);
print $_;
}
---------------------------------------
Now, all other junk line as well with the required output.
What should I do to get only the output that I have mentioned above?
Thanks,
John
------------------------------
Date: Mon, 14 Apr 2003 20:17:27 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: To extract a portion of a text file
Message-Id: <mbudash-B767FD.13172714042003@typhoon.sonic.net>
In article <500f84f3.0304140744.47b7ceaf@posting.google.com>,
clearguy02@yahoo.com (John Smith) wrote:
> Hi all,
>
> I am working on the below perl script:
>
> I have a big file (C:\test.txt) with 100s of lines. Out of all the
> lines, there is a repetetive block of code and I need to extract only
> that stuff and ignore the rest of the lines in the file:
>
> Example: (test.txt)
> --------------------
> first line...
> second line....
> .................
> Test Case : test1_john
> test2_rob
>
> Surprise Key : hi_ there
>
> ........
> again some lines of code.....
>
> Test Case : test3_john
> test5_will
> test10_smith
>
> Surprise Key : hi_there2
> hi_there3
> ................
> .............
> again some lines of code...........
> ..........
>
> -------------------------------------
>
> Here, the fields, Test Case and Surprise key appear many times with
> different values each time. All the values for the "Test Case" and
> "Surprise Key" fileds should be added together at one place. The
> values don't follow any indentation.
>
> Now, the out put should like as follows:
> ----------------------
> Test Case : test1_john
> test2_rob
> test3_john
> test5_will
> test10_smith
>
> Surprise Key : hi_ there
> hi_there2
> hi_there3
> ------------------------------------------------
>
> I have below part of script:
>
> -----------------------------------
> #! C:/perl/bin/perl
>
> $file = "C:\\test.txt);
>
> open (FILE, $file) || die "Can not open the file: $!";
>
> while (<FILE>)
> {
> next unless (/^\s*Test/.../^\s*Surprise/);
> print $_;
> }
> ---------------------------------------
>
> Now, all other junk line as well with the required output.
>
> What should I do to get only the output that I have mentioned above?
>
> Thanks,
> John
first off, what marks the end of the 'Surprise Key' lines (and the
beginning of uninteresting data)?
--
Michael Budash
------------------------------
Date: Mon, 14 Apr 2003 18:44:02 GMT
From: "Michael P. Broida" <michael.p.broida@boeing.com>
Subject: Re: Ugly code; what does it do?
Message-Id: <3E9B0172.1290EDE4@boeing.com>
Tony McNulty wrote:
>
> On Fri, 11 Apr 2003 21:14:00 GMT, Michael P. Broida
> <michael.p.broida@boeing.com> wrote:
>
> [snip]
>
> > At the very end, I don't understand the use of:
> > if( $$[-1] );
>
> $$[-1] refers to the last element of the array. Useful tool that I've not
> seen in any other language.
>
> i.e., @a = qw/a b c/;print $a[-1]; would print out the letter c.
Thanks! I guess it's one of those "more than one way"
things. You could just use $$[$#$] to get last element,
couldn't you? Actually, the -1 syntax is "prettier" in
this case because the array name is so ugly. <grin>
Mike
------------------------------
Date: Mon, 14 Apr 2003 18:45:11 GMT
From: "Michael P. Broida" <michael.p.broida@boeing.com>
Subject: Re: Ugly code; what does it do?
Message-Id: <3E9B01B7.F32D36FE@boeing.com>
Tad McClellan wrote:
>
> Michael P. Broida <michael.p.broida@boeing.com> wrote:
> > "Michael P. Broida" wrote:
>
> >> print join("\n",@$[0..$#$]) if( $$[-1] );
>
> > At the very end, I don't understand the use of:
> > if( $$[-1] );
> >
> > OK, $$[x] is the xth element of array @$ created in the
> > preceding line. But I don't understand the index of -1.
>
> From the very first paragraph in perldata.pod:
>
> Normal arrays are ordered lists of scalars indexed by number,
> starting with 0 and with negative subscripts counting from the end.
Thank you! I might have found that if I had any idea which
perldoc magic args to use to get there. Now I know -some-.
Thanks!
Mike
------------------------------
Date: Mon, 14 Apr 2003 16:32:43 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Ugly code; what does it do?
Message-Id: <slrnb9ma7r.ivq.tadmc@magna.augustmail.com>
Michael P. Broida <michael.p.broida@boeing.com> wrote:
> Tad McClellan wrote:
[snip: about array indexing]
>> From the very first paragraph in perldata.pod:
Since your question was about Perl's _data_ types, it should
be obvious that perldata.pod would be The Place To Look.
> I might have found that if I had any idea which
> perldoc magic args to use to get there.
Shouldn't have mattered in this case, as perldata.pod is one of
The Biggies that should be read end-to-end every few months anyway.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 14 Apr 2003 13:49:14 -0600
From: "Jamie Bohr" <Jamie_Bohr@Agilent.com>
Subject: Re: variables in modules
Message-Id: <1050349755.910349@goodnews.cos.agilent.com>
Thank you, the perldoc on Exporter was helpful and pointed me in the right
direction.
- Jamie
"James E Keenan" <jkeen@concentric.net> wrote in message
news:b7culs$sq7@dispatch.concentric.net...
>
> "Jamie Bohr" <Jamie_Bohr@Agilent.com> wrote in message
> news:1050273965.241758@goodnews.cos.agilent.com...
> > I am tring to find out what variables are set in a Perl module and what
> the
> > variables are set too. So far I have hit some brick walls. dumpvar.pl
> does
> > not return array values. For example I have the following Perl module
> named
> > test.pm in a modules directory:
> >
> > ######################
> > package test_config;
> >
> > use strict;
> > use vars qw($VERSION %test @test);
> >
> > require Exporter;
> >
> > # set the version for version checking
> > $VERSION = 2.00;
> >
> > %test = ("key" => "value");
> >
> > @test = (1,2,3,4);
> > ###########################
> >
> > If I run the following at a command prompt:
> >
> > perl -e '
> > use modules::test;
> > require "dumpvar.pl";
> > dumpvar("test_config");'
> >
> > I get:
> >
> > @test = (
> > )
> > %test = (
> > )
> > $VERSION = 2
> >
> > I expected to get more, like the values of @test and %test. Please tell
> me
> > there is a better way.
> >
> Start by studying the documentation for the Exporter module.
> 'perldoc Exporter'
>
>
------------------------------
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.
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 4844
***************************************