[22899] in Perl-Users-Digest
Perl-Users Digest, Issue: 5119 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 14 00:05:47 2003
Date: Fri, 13 Jun 2003 21:05:06 -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 Fri, 13 Jun 2003 Volume: 10 Number: 5119
Today's topics:
book <john62@electronmail.com>
die and exception over-stringification <joey@kitenet.net>
Re: Do it faster (Jay Tilton)
Re: encrypt/decrypt module that uses only printable cha <mbudash@sonic.net>
get data on socket problem (rdack)
Re: How to do a 'wrapper' on Windows? <foomf@attbi.com>
Re: How to do a 'wrapper' on Windows? <foomf@attbi.com>
Re: NEWBEE: Client-Server Timout implementation (Zack)
Newline and here-doc <m.theiss@web.de>
Re: Newline and here-doc <No_4@dsl.pipex.com>
Re: Newline and here-doc <No_4@dsl.pipex.com>
Re: Permute List of Lists? (Jay Tilton)
Query Problem <nal@mta.ca>
Recursive Copying Script Question (abstractjoe)
regexp challenge (Bob Gotchall)
Re: regexp challenge <jkeen@concentric.net>
Re: require specific version as opposed to > than versi (Jay Tilton)
Re: Variable value being interpreted as HTML (Jay Tilton)
Re: Which value is tha largest(benchmarked) <usenet@dwall.fastmail.fm>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 14 Jun 2003 00:04:10 -0400
From: j <john62@electronmail.com>
Subject: book
Message-Id: <3EEA9EBA.3080309@electronmail.com>
could someone recommend a good perl book to me?
--
-------------------------------------------------------------
Coldmail.us offers free web access to newsgroups.
http://www.coldmail.us/
-------------------------------------------------------------
------------------------------
Date: Fri, 13 Jun 2003 14:28:22 -0400
From: Joey Hess <joey@kitenet.net>
Subject: die and exception over-stringification
Message-Id: <slrnbek5u6.cfi.joey@dragon.kitenet.net>
I have an exception class that overloads stringify and when stringified
has to do something expensive. I had hoped that moving the complex
claculation to happen when it's stringified would avoid it entirely in
cases where the exception is caught and thrown away. Instead it seems
that die always stringifies it, and sometimes it will even get
stringified twice.
The code below illistrates the problem. The first exception is caught
by the eval, but has already been stringified once at that point. The
second exception actually gets stringified twice, once apparently when
die first throws it, and once when perl stringifies it to print it as it
dies of it.
print "catch exception and throw it away\n";
eval {
die My::Exception->new();
};
print "die of exception\n";
die My::Exception->new;
package My::Exception;
my $counter=0;
sub new {
my $c=++$counter;
return bless(\$c, shift);
}
use overload '""' => sub {
my $this=shift;
print "expensive operation here (for exception #".$$this.")\n";
sleep 2;
return "stringification of exception #".$$this;
}
This takes 6+ seconds to run, not just 2, and prints:
catch exception and throw it away
expensive operation here (for exception #1)
die of exception
expensive operation here (for exception #2)
expensive operation here (for exception #2)
stringification of exception #2 at test line 7.
Grr, this is annoying. I have done something evil and ugly using
$SIG{__DIE__} to work around this, but I wish perl were less eager to
stringify exception objects when it shouldn't need to.
--
see shy jo
------------------------------
Date: Sat, 14 Jun 2003 00:25:24 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Do it faster
Message-Id: <3eea6aaa.183420896@news.erols.com>
sholden@flexal.cs.usyd.edu.au (Sam Holden) wrote:
: Of course addition should be done via a ripple carry adder implemented
: in perl.
:
: For reasons I haven't worked out yet, my code is really slow...
Pshaw.
sub add {
my( $i, $j ) = @_;
{
my $c = $i & $j;
$i ^= $j;
($j = $c<<1) && redo;
}
$i;
}
When it's that much fun, who cares if it's an order of magnitude
slower?
And screw floats, just as a matter of principle.
------------------------------
Date: Fri, 13 Jun 2003 22:59:16 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: encrypt/decrypt module that uses only printable chars?
Message-Id: <mbudash-BC56C8.15591513062003@typhoon.sonic.net>
In article <20030613160842.113$0i@newsreader.com>, ctcgag@hotmail.com
wrote:
> Michael Budash <mbudash@sonic.net> wrote:
> > In article <ff7ef7bd.0306131129.5a530ee@posting.google.com>,
> > olhipie@qwest.net (mark) wrote:
> >
> > > I'm trying to encrypt and decrypt ascii strings, and the Crypt::xx
> > > modules that I have tried all work great, but produce non printable
> > > chars in the encryption phase. Is there a switch or particular module
> > > that that I could use?
> > >
> > > Thanks, Mark
> >
> > hex-ify it after encrypting:
> >
> > $hex_encrypted = unpack ("H*", $encrypted);
> >
> > and de-hex-ify it before decrypting:
> >
> > $encrypted = pack ("H*", $hex_encrypted);
>
> I was going to recommend uuencoding it:
>
> $uu=pack "u", $encrypted;
>
> $encrypted=unpack "u",$uu;
>
> I hadn't thought of doing it your way, using unpack to do the packing
> and pack to the unpacking. :)
>
> Xho
i do everything while looking in the mirror 8^)
--
Michael Budash
------------------------------
Date: 13 Jun 2003 20:21:49 -0700
From: rdacker@pacbell.net (rdack)
Subject: get data on socket problem
Message-Id: <644f6688.0306131921.2dab1ee7@posting.google.com>
i send a binary file on a socket and the client sends all bytes fine,
no error, but the server doesn't get the whole file. what could be
going wrong?
this is the server side code for getting the data:
$res = 0;
$chnk=32768;
# $len comes from client and is correct (530664 bytes)
while($res < $len)
{
print "$res ";
$chnk = $len - $res if ($res + $chnk) > $len;
print $chnk." ";
$rr = sysread $sock, $data, $chnk, $res;
print $rr."\n";
die "failed to get data (got $res bytes): $!\n" if !defined
$rr;
last if $rr == 0;
$res += $rr;
}
i can get between 480k+ and 516k+ bytes. and i see not every read gets
a full chunk asked for.
then hangs on last read when client has sent all bytes, but server
seems to have lost some bytes (12k - 40k) and thinks there should be
more coming.
does sysread not buffer? should client send in smaller/larger chunks
than server gets?
is there some canned code for a simple data xfer between sockets?
rdack
------------------------------
Date: Fri, 13 Jun 2003 23:37:28 GMT
From: "foomf" <foomf@attbi.com>
Subject: Re: How to do a 'wrapper' on Windows?
Message-Id: <YetGa.146605$DV.150658@rwcrnsc52.ops.asp.att.net>
"Carlton Brown" <carltonbrown@hotmail.com> wrote in message
news:aa611a32.0306120353.7dd67ce7@posting.google.com...
> "foomf" <foomf@attbi.com> wrote in message
news:<7RKFa.909447$OV.843548@rwcrnsc54>...
> > I have a database access program that consumes licenses. It connects to
a
> > server via a program that runs in a DOS command window.
> [snippage regarding sales people being idiots]
> > I've been instructed to put a wrapper around the process that watches
> > keyboard input and if it is left untouched for too long, sends the
sequence
> > to shut down the database connection.
>
> First, I must say that the "real" solution here would be on the
> database side because there's no client-side timeout solution that can
> account for the typical sales-department shutdown technique of yanking
> the power strip. For this reason, most databases or license software
> have environment variables or at least commands to unregister stale
> connections. I suggest checking your DB or license daemon docs before
> going forward with the client side.
The database server is a piece of antique code from a company called ADP.
The IT manager for
whom I am attempting this bit of klugerie says that they don't provide a
server-side timeout feature.
That's supposed to come in the future, apparently.
> Having given due warning - If you are unlucky enough not to have a
> server-side solution, what about using open2() to open read and write
> pipes to your program, use the READ handle to capture the input, and
> the WRITE handle to capture output? Use alarm() to set a client-side
> timeout. Create signal handler that sends appropriate shutdown
> command when certain signals are received.
That was my intention, until I read that alarm() was unsupported, and when I
tried out select()
it didn't time out and it didn't return any kind of useful indicator that it
had failed.
I see from further in this thread that 5.8.0 supports alarm, and since I
have that version, I can
test it, but I'm unsure what will happen on the mix of win95 thru winME
systems that the customer
has installed in his sales organization. The last thing he wants to do is
to install newer software anywhere,
or I'd be trying to do this with a .NET app.
I do appreciate your help.
------------------------------
Date: Sat, 14 Jun 2003 01:03:01 GMT
From: "foomf" <foomf@attbi.com>
Subject: Re: How to do a 'wrapper' on Windows?
Message-Id: <9vuGa.938226$OV.885043@rwcrnsc54>
"Ted Zlatanov" <tzz@lifelogs.com> wrote in message
news:4nn0gnl4m7.fsf@lockgroove.bwh.harvard.edu...
> I agree with Carlton's ideas, but he relies on a CR/LF being pressed.
> The user could be typing, backspacing, hitting TAB, etc.
>
> I would catch every key press with something like Term::InKey and
> forward it to the client program; set an alarm() on every key stroke.
> Term::InKey works on Windows.
Good point, and one I was going to investigate once I got it working. At the
moment, though, for no
obvious reason, the open2() call isn't working - I looked at the .pm from
lib/IPC and discovered it is
heavily into fork(). The mapping for fork() SHOULD be clean enough but
reading the perlfork doc
makes me suspicious that there might be some hidden problems. In any case,
though, I'm getting an
undefined subroutine error, despite the use IPC::open2; I added to the front
of his code frag.
------------------------------
Date: 13 Jun 2003 18:45:04 -0700
From: uzmargov@yahoo.com (Zack)
Subject: Re: NEWBEE: Client-Server Timout implementation
Message-Id: <a5f6b9a8.0306131745.27c33c0f@posting.google.com>
Anyone ?.......
------------------------------
Date: Sat, 14 Jun 2003 00:11:13 +0200
From: "Martin Theiß" <m.theiss@web.de>
Subject: Newline and here-doc
Message-Id: <bcdi60$sbr$1@news.uni-stuttgart.de>
I've got a problem with a here-doc statement. I tried this:
print <<EOF
Hello
EOF
;
As a Result "Hello" ist printed with a leading and a trailing newline, but i
don't want to have a trailing newline. Does anybody know a workaround (maybe
a regex) for this problem.
Kind regards
Martin Theiß
------------------------------
Date: Fri, 13 Jun 2003 23:51:51 +0100
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Newline and here-doc
Message-Id: <3eea5587$0$10624$cc9e4d1f@news.dial.pipex.com>
Martin Theiß wrote:
>
> print <<EOF
> Hello
> EOF
> ;
Although it doesn't affect the output (in this case) that syntax is
wrong. It should be:
print <<EOF;
Hello
EOF
ie, you put the ; at the end of the print statment. The EOF...EOF is
input for it.
> As a Result "Hello" ist printed with a leading and a trailing newline,
I don't get a leading newline either way.
> but i
> don't want to have a trailing newline. Does anybody know a workaround (maybe
> a regex) for this problem.
print "HELLO";
but I suspect that your here document will be longer in practice so try
this.
chomp (my $text = <<EOF);
Hello
EOF
print $text;
--
-*- Just because I've written it here doesn't -*-
-*- mean that you should, or I do, believe it. -*-
------------------------------
Date: Sat, 14 Jun 2003 00:20:23 +0100
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Newline and here-doc
Message-Id: <3eea5c37$0$18493$cc9e4d1f@news.dial.pipex.com>
Big and Blue wrote:
>
> but I suspect that your here document will be longer in practice so try
> this.
or, making the variable localized only to the print statement:
print do {chomp (my $text = <<EOF);
Hello
EOF
$text}, " plus more...\n";
--
-*- Just because I've written it here doesn't -*-
-*- mean that you should, or I do, believe it. -*-
------------------------------
Date: Fri, 13 Jun 2003 22:07:25 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Permute List of Lists?
Message-Id: <3eea4a6e.175167847@news.erols.com>
david.evans@vir.gla.ac.uk (David) wrote:
: I have a list of lists, something like ([a,b],[i,j,k],[y,z]) and need
: to permute every possible combination i.e. aiy aiz ajy ajz aky akz ..
: bkz etc.
You might find glob() useful.
my @perms = glob( "{a,b}{i,j,k}{y,z}" );
All you need to do is programatically generate that
"{a,b}{i,j,k}{y,z}" string.
my @foo = qw/a b/;
my @bar = qw/i j k/;
my @baz = qw/y z/;
my @perms = glob
join '', map {local $" = ','; "{@$_}"} \(@foo, @bar, @baz);
------------------------------
Date: Sat, 14 Jun 2003 03:37:06 GMT
From: "John Smith" <nal@mta.ca>
Subject: Query Problem
Message-Id: <CLwGa.85143$j9%.76757@news04.bloor.is.net.cable.rogers.com>
Hi guys,
I'm new to Perl. Mostly I program in C++ or PHP, so please help me out with
the following problem or at least give me some hint.
----------------------
Given a table 'mailing' that has a column:
addr varchar(255);
Create a new table to hold a count of email addresses by their domain
name limited to those domains that have at least 100 addresses in the list
and add a set of rows on a daily basis.
Then report on this table for the last 2 months the top 50 domains
showing their counts.
Please note, do not use the database for processing only use Perl for
this ( ie, group by )
Please assume there are 10,000,000 "email addresses" and 100,000
domains. Please make the script as efficient as possible with this in mind.
Please assume the adding of new rows on a daily basis means going
through the mailing table which is also updated daily and updating the count
or
adding new rows for a domain that now has 100 or more count. The purpose
is this to trend analysis of domain growth.
Please note, a table is to be generated, not a report. There is no date
field
provided in the initial mailing table.
----------------
Thanks,
John
------------------------------
Date: 13 Jun 2003 16:32:01 -0700
From: abstract_joe@yahoo.com (abstractjoe)
Subject: Recursive Copying Script Question
Message-Id: <824524f6.0306131532.7d43a9ca@posting.google.com>
Below is recursive copy script (Written by Randal L. Schwartz). It
does everything I want it to do. Except, it doesn't copy files in the
sub directory. Example:
I updated a file in /usr/local/htdocs/www/files1/files1sub/ call
index.html I run the script and it prints out the following.
copying /usr/local/htdocs/www/files1/files1sub/index.html to
/usr/local/htdocs/www/files2/index.html
It copies the html file to files2 instead of files1sub directory...
Wish someone can help me troubleshoot this script. Thanks ahead.
#!/usr/bin/perl
use File::Find;
use File::Copy;
my $top_src = "/usr/local/htdocs/www/files1";
my $dest = "/usr/local/htdocs/www/files2";
find sub {
##might need it for later on
##ignore non html files
##return unless -f and /\.html$/;
## don't copy if we already have a later file
if (-e "$dest/$_" and -M "$dest/$_" <= -M $_) {
## tracing: turn off when you're happy
warn "ignoring older $File::Find::name\n";
return;
}
## tracing: turn off when you're happy
warn "copying $File::Find::name to $dest/$_\n";
copy $_, "$dest/$_";
}, $top_src;
------------------------------
Date: 13 Jun 2003 15:27:14 -0700
From: rgotchall@austin.rr.com (Bob Gotchall)
Subject: regexp challenge
Message-Id: <488df089.0306131427.40f619ce@posting.google.com>
Folks.
I think I have discovered a pattern that is really easy to see by the
human eye, but doesn't seem to have an elegant perl solution. I have
data (it's test vector data from a Teradyne Catalyst, if that means
anything to you) that looks like this:
opcode arg opcode opcode opcode arg X XX 10101 LHHL LHL 10 LLH
HLLH11010110 XX
opcode opcode opcode arg X 1X 10101 LHHL LHL -- LLH
HLLH11011110 XX
opcode arg opcode opcode opcode arg X XX 10101 LLLL HHL -- LLH
HLLH01010110 XX
opcode arg opcode opcode opcode 1 X0 10101 LHHL LHL -1 LLH
HLLH11110111 XX
opcode arg opcode X XX 10101 HHHH LHH -- LLH
HLLH11010010 XX
opcode arg opcode opcode opcode arg X XX 10101 LHHL LHL -- LLH
HLLH01010010 XX
Where an "opcode" is a word like JUMP, REPEAT, LABEL and an "arg" is
any old alphanumeric. The vector field is all [10HLX-]. and the
width and composition of the file varies with file, of course. There
is no column number assumptions that will work.
So I want to do something like this:
$line =~ / (stuff thats opcode words)\s+(stuff that's vector words)/;
$opcodes=$1; $vectors=$2;
Thoughts? Is it impossible?
------------------------------
Date: 14 Jun 2003 03:52:09 GMT
From: "James E Keenan" <jkeen@concentric.net>
Subject: Re: regexp challenge
Message-Id: <bce659$99a@dispatch.concentric.net>
"Bob Gotchall" <rgotchall@austin.rr.com> wrote in message
news:488df089.0306131427.40f619ce@posting.google.com...
> Folks.
>
> opcode arg opcode opcode opcode arg X XX 10101 LHHL LHL -- LLH
HLLH01010010 XX
>
> Where an "opcode" is a word like JUMP, REPEAT, LABEL and an "arg" is
> any old alphanumeric. The vector field is all [10HLX-]. and the
> width and composition of the file varies with file, of course. There
> is no column number assumptions that will work.
>
1. Your character class should probably include the wordspace: [10HLX- ]
2. Is there a *minimum* number of characters in the vector field? If the
minimum were, say, 10, then a first pass might look like:
/(.*)([10HLX- ]{10,})$/
------------------------------
Date: Sat, 14 Jun 2003 02:30:30 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: require specific version as opposed to > than version
Message-Id: <3eea8841.190997471@news.erols.com>
ktom <abc@nowhere.com> wrote:
: with respect to using versions in modules.
:
: we would place the following command in our perl file to use the module
: info.
:
: use SomeModule 1.23;
:
: which according to the docs indicates that the module $VERSION must be
: 1.23 or greater.
:
: what if i want it to use ONLY 1.23, not greater, not lessor?
:
: can that be done?
The "only" module from CPAN may be useful.
use only SomeModule => 1.23;
------------------------------
Date: Fri, 13 Jun 2003 22:15:35 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Variable value being interpreted as HTML
Message-Id: <3eea4c18.175593596@news.erols.com>
ianp@myrealbox.com (Ian) wrote:
: I'm trying to use Win32::ODBC to pull some data from an existing
: table. The trouble is that one of the table columns is called "title"
: which appears to be confusing Perl. When I have a variable with that
: name it gets mangled in the process going from "title" to "<title />"
: which of course screws things up.
Not quoting your strings is why it's going screwy.
: $cols[3]=title;
Your code must be importing the &title subroutine from CGI.pm .
In that line, the bareword 'title' is being interpreted as a call to
that subroutine, not as a string.
$cols[3] = 'title';
"use strict;" could have prevented this trouble.
------------------------------
Date: Sat, 14 Jun 2003 01:55:19 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: Which value is tha largest(benchmarked)
Message-Id: <Xns9399DF0083B84dkwwashere@216.168.3.30>
Martien Verbruggen <mgjv@tradingpost.com.au> wrote:
> On Fri, 13 Jun 2003 01:22:44 -0000,
> David K. Wall <usenet@dwall.fastmail.fm> wrote:
>> for my $n (1e1, 1e2, 1e3, 1e4, 1e5, 1e6) {
>> my @ra1 = (1..$n);
>
> This will fill @ra1 with integers,
>
>> my @ra2;
>> push @ra2, rand for 1..$n;
>
> This will fill @ra2 with doubles. It's probably better to make sure
> they're the same thing. Either floats everywhere, or ints everywhere.
Good point. Oops.
>
>> my @ra3 = reverse @ra1;
>> print "n = $n\n\n";
>> my $results = timethese(2000000, {
>
> I generally use something like -3 as the first argument here. It's
> less fiddly to get right, and you can predict how long you need to
> wait.
Actually I do, too. I was trying to follow the example of the post I
was following up.
>> '1 to n' => 'my $m = max(@ra1)',
>> 'random' => 'my $m = max(@ra2)',
>> 'n to 1' => 'my $m = max(@ra2)',
>
> Be careful with lexicals and eval, which is what is used in this
> case. You're not actually finding the max() of anything (in the eval
> you won't see that lexical). Also, the last @ra2 should probably be
> @ra3.
Oops again on the @ra3, and I didn't realize that about the eval. More
learning necessary, I see. :-)
> Fixing the above gives a more correct comparison.
>
> I haven't actually run it, but I can imagine that you'd still find a
> small disadvantage for the '1 to n' case, because it has to assign a
> different value to $max for each element inspected.
Ah, that answers my question about how sorting is relevant. I'm
chagrined I didn't see that myself. When the code is corrected, you do
indeed find a disadvantage for the 1 to n case.
>The 'n to 1' case
> assigns the same value every time (I'd fix the subroutine as well:
> why assign $max to $max ever?).
Because it was a quick-and-dirty solution that I never intended for any
heavy-duty use? :-) IIRC, the OP wanted to find the max of four values.
> Perl might optimise that sort of
> thing internally, or not. It probably does. I don't think it'll make
> an enormous amount of difference anyway.
Not a huge one, no, but noticeable.
>> n = 10
>>
>> Benchmark: timing 2000000 iterations of 1 to n, n to 1, random...
>> 1 to n: 3 wallclock secs ( 3.50 usr + 0.00 sys = 3.50 CPU) @
>> 571428.57/s (n=2000000)
> ^^^^^^^^^^^
>
>> n = 1000000
>>
>> Benchmark: timing 2000000 iterations of 1 to n, n to 1, random...
>> 1 to n: 4 wallclock secs ( 3.52 usr + 0.00 sys = 3.52 CPU) @
>> 568990.04/s (n=2000000)
> ^^^^^^^^^^^
>
> I don't think there are many machines where a loop that iterates
> 1000000 times takes as little time as one that iterates 10 times.
True, but the n in this case is the size of the array, not the number of
iterations.
--
David Wall
------------------------------
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 5119
***************************************