[27452] in Perl-Users-Digest
Perl-Users Digest, Issue: 9078 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 23 03:05:57 2006
Date: Thu, 23 Mar 2006 00:05:05 -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, 23 Mar 2006 Volume: 10 Number: 9078
Today's topics:
Re: Code review request. <jack@abc.net>
Re: Code review request. <someone@example.com>
Re: Code review request. <tadmc@augustmail.com>
Re: Code review request. <jack@abc.net>
Re: deferred module loading ? <ced@blv-sam-01.ca.boeing.com>
Parallel LWP callback doesn't terminate. <peter.hill@modulus.com.au>
Re: Term::ReadKey not working on one linux box <nospam-abuse@ilyaz.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 22 Mar 2006 23:51:26 GMT
From: Michael Press <jack@abc.net>
Subject: Re: Code review request.
Message-Id: <jack-1413C0.15512522032006@newsclstr02.news.prodigy.com>
In article <rSjUf.238$%H.103@clgrps13>,
"John W. Krahn" <someone@example.com> wrote:
> Michael Press wrote:
> > Hello. I am reading in a line of permutation cycles,
> > preparatory to multiplying them. Want to parse the line
> > into tokens for linear scanning: permutation elements
> > and parentheses. Also want to initialize a hash whose
> > key element pairs are permutation elements. The initial
> > state of the hash is to be the identity map. I thought
> > I could make this code more tight or efficient, but
> > failed; particularly the map <- grep pipe. Will someone
> > comment on this code? Thanks for listening.
> >
> > ________________CUT_______________
> > #! /usr/bin/perl -w
> >
> > sub pm;
> >
> > while (<DATA>) { pm $_ }
>
> Put the code inside the while loop instead of calling a subroutine.
No. This is example code.
I use the subroutine like this:
my $alpha = "(99)(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)";
my $beta = "(99)(0)(3 6 12 1 2 4 8 16 9 18 13)(15 7 14 5 10 20 17 11 22 21 19)";
my $gamma = "(99 0)(1 22)(2 11)(3 15)(4 17)(5 9)(6 19)(7 13)(8 20)(10 16)(12 21)(14 18)";
my $delta = "(99)(0)(3)(15)(1 18 4 2 6)(5 21 20 10 7)(8 16 13 9 12)(11 19 22 14 17)";
my $x;
print "(delta alpha^11)^5 has shape 1^6 3^6\n";
$x = ($delta . $alpha x 11) x 5;
pm $x;
print "\n";
print "beta = alpha^5 gamma alpha^5 gamma alpha^14 gamma alpha^18 \n";
$x = $alpha x 5 . $gamma . $alpha x 5 . $gamma . $alpha x 14 . $gamma . $alpha x 18;
pm $x;
$x = $beta;
pm $x;
print "\n";
...
> > sub pm
> > {
> > my $z;
> > my @line;
> > my %p;
>
> Define and declare the variables at the same time.
Yes.
> > # Parse the cycles, and initialize the permutation map.
> > $z = shift (@_);
> > @line = $z =~ m/(\w+|[()])/g;
>
> You don't need the capturing parentheses in the pattern.
Dang. Where is the specification for this? Examples in
Programming Perl use capturing parentheses.
> > %p = map { $_ => $_ } grep { m/\w+/} @line;
> >
> > printf ("%s\n", join ':', @line), "\n";
>
> Use print instead of printf. The ', "\n"' at the end is not doing anything so
> why is it there?
Cut and paste error when editing for submission to this forum.
It runs, but I did not see the problem.
> > while (( $key, $value) = each (%p)) { printf "%7s%7s\n", $key, $value}
> > }
> >
> > __END__
> > (99)(0)(3)(1 4 2 6)(5 10 7)(8 9 )(1 2 4 8 9 3 6 )(5 7)(0 2 )(1 5 99)(3 10)(4 7 8 )(6)(9)
>
> sub pm {
> my %p = map { /\w/ ? ( $_ => $_ ) : () } my @line = $_[0] =~ /\w+|[()]/g;
>
> print join( ':', @line ), "\n";
>
> while ( my ( $key, $value ) = each %p ) {
> printf "%7s%7s\n", $key, $value;
> }
> }
Thank you. Use of $_[0] noted; `each %p' noted.
I still do not use `():?' enough.
--
Michael Press
------------------------------
Date: Thu, 23 Mar 2006 01:57:34 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Code review request.
Message-Id: <ignUf.324$%H.22@clgrps13>
Michael Press wrote:
> In article <rSjUf.238$%H.103@clgrps13>,
> "John W. Krahn" <someone@example.com> wrote:
>
>>Michael Press wrote:
>>>
>>> # Parse the cycles, and initialize the permutation map.
>>> $z = shift (@_);
>>> @line = $z =~ m/(\w+|[()])/g;
>>You don't need the capturing parentheses in the pattern.
>
> Dang. Where is the specification for this? Examples in
> Programming Perl use capturing parentheses.
perldoc perlop
m/PATTERN/cgimosx
/PATTERN/cgimosx
[snip]
The "/g" modifier specifies global pattern matching--that is,
matching as many times as possible within the string. How it
behaves depends on the context. In list context, it returns a
list of the substrings matched by any capturing parentheses in
the regular expression. If there are no parentheses, it
returns a list of all the matched strings, as if there were
parentheses around the whole pattern.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 23 Mar 2006 00:04:28 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Code review request.
Message-Id: <slrne24ejc.2gb.tadmc@magna.augustmail.com>
Michael Press <jack@abc.net> wrote:
> In article <rSjUf.238$%H.103@clgrps13>,
> "John W. Krahn" <someone@example.com> wrote:
>
>> Michael Press wrote:
>> > @line = $z =~ m/(\w+|[()])/g;
>>
>> You don't need the capturing parentheses in the pattern.
>
> Dang. Where is the specification for this?
in perlop:
The C</g> modifier specifies global pattern matching--that is,
matching as many times as possible within the string. How it behaves
depends on the context. In list context, it returns a list of the
substrings matched by any capturing parentheses in the regular
expression. If there are no parentheses, it returns a list of all
the matched strings, as if there were parentheses around the whole
pattern.
> Examples in
> Programming Perl use capturing parentheses.
Examples of m//g in a list context where you want to capture
the entire pattern?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 23 Mar 2006 07:36:35 GMT
From: Michael Press <jack@abc.net>
Subject: Re: Code review request.
Message-Id: <jack-B8D35F.23363422032006@newsclstr02.news.prodigy.com>
In article <slrne24ejc.2gb.tadmc@magna.augustmail.com>,
Tad McClellan <tadmc@augustmail.com> wrote:
> Michael Press <jack@abc.net> wrote:
> > In article <rSjUf.238$%H.103@clgrps13>,
> > "John W. Krahn" <someone@example.com> wrote:
> >
> >> Michael Press wrote:
>
>
> >> > @line = $z =~ m/(\w+|[()])/g;
> >>
> >> You don't need the capturing parentheses in the pattern.
> >
> > Dang. Where is the specification for this?
>
>
> in perlop:
>
> The C</g> modifier specifies global pattern matching--that is,
> matching as many times as possible within the string. How it behaves
> depends on the context. In list context, it returns a list of the
> substrings matched by any capturing parentheses in the regular
> expression. If there are no parentheses, it returns a list of all
> the matched strings, as if there were parentheses around the whole
> pattern.
>
> > Examples in
> > Programming Perl use capturing parentheses.
>
> Examples of m//g in a list context where you want to capture
> the entire pattern?
No example looks like what I wrote. I put in the
parentheses myself based on examples like
($a, $b) = /(\w+) (\w+)/;
When JK corrected me I did not infer that Programming Perl
had misled me. And on page 151 (third edition) we read
"If there are no capturing parentheses within the /g
pattern, then the complete matches are returned."
Thanks.
--
Michael Press
------------------------------
Date: Thu, 23 Mar 2006 05:53:36 GMT
From: Charles DeRykus <ced@blv-sam-01.ca.boeing.com>
Subject: Re: deferred module loading ?
Message-Id: <IwKGD9.6tv@news.boeing.com>
bugbear wrote:
> ...
> Is there any way to avoid loading a module
> until it is (in fact) needed?
> ...
>
perldoc autouse
NAME
autouse - postpone load of modules until a function is used
SYNOPSIS
use autouse 'Carp' => qw(carp croak);
carp "this carp was predeclared and autoused ";
....
--
Charles DeRykus
------------------------------
Date: Thu, 23 Mar 2006 07:31:28 GMT
From: "Peter Hill" <peter.hill@modulus.com.au>
Subject: Parallel LWP callback doesn't terminate.
Message-Id: <k9sUf.14622$dy4.4778@news-server.bigpond.net.au>
Hi,
I'm trying to get web documents returned for analysis using the RobotUA part
of LWP::Parallel, but for some reason the callback function never completes;
specifically in the sample code below (output at end) the line
print "We never get here.\n";
is never executed, which is where I would expect to call my analysis code.
What dumb error am I committing?
TIA
Peter Hill
#! /usr/bin/perl -w
use strict;
use LWP::Parallel::RobotUA qw(:CALLBACK);
my $MAX_SIZE = 100000; #bytes
my $ua = LWP::Parallel::RobotUA->new('foobar/1.0','foo@bar.com');
$ua -> delay(0.5);
$ua -> in_order (1); # handle requests in order of registration
$ua -> duplicates(0); # ignore duplicates
$ua -> timeout (2); # in seconds
$ua -> redirect (1); # follow redirects
$ua -> max_hosts(5);
$ua -> max_req(5);
# register initial request
addURL('http://www.cpan.org/');
# this is the main (implicit) loop
my $something = $ua -> wait(15);
sub callback_for_parse {
my ($content, $response, $protocol, $entry) = @_;
print "handling answer from ",$response->request->url,": ",
length($content), " bytes, Code ", $response->code, ", ",
$response->message,"\n";
if (length $content) {
print "... received chunk ",length($content)," bytes, type
".$response->content_type."\n";
$response->add_content($content);
if (length($response->content) < $MAX_SIZE and $response->content_type
=~ /text\/html/i) {
print "... returning ",length($content)."\n";
# print "content is :".$content."\n";
print "response is :".$response."\n";
print "protocol is :".$protocol."\n";
print "entry is :".$entry."\n";
return length $content;
}
else{
print "oversize or not text/html: content-type is ".$response ->
content_type."\n";
}
}
print "We never get here.\n";
return C_ENDCON;
}
sub addURL {
my $url = shift;
my $request = new HTTP::Request('GET', $url);
$ua -> register($request,\&callback_for_parse);
print "... registered request for $url\n";
}
# output
... registered request for http://www.cpan.org/
handling answer from http://www.cpan.org/: 4138 bytes, Code 200, OK
... received chunk 4138 bytes, type text/html
... returning 4138
response is :HTTP::Response=HASH(0x155b87c)
protocol is :LWP::Parallel::Protocol::http=HASH(0x2951c18)
entry is :LWP::Parallel::UserAgent::Entry=HASH(0x28e8da8)
handling answer from http://www.cpan.org/: 1665 bytes, Code 200, OK
... received chunk 1665 bytes, type text/html
... returning 1665
response is :HTTP::Response=HASH(0x155b87c)
protocol is :LWP::Parallel::Protocol::http=HASH(0x2951c18)
entry is :LWP::Parallel::UserAgent::Entry=HASH(0x28e8da8)
------------------------------
Date: Thu, 23 Mar 2006 07:14:02 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Term::ReadKey not working on one linux box
Message-Id: <dvthrq$1st2$1@agate.berkeley.edu>
[A complimentary Cc of this posting was sent to
Martin Mohr
<mm@mohr.de>], who wrote in article <dvskif$2u9$00$1@news.t-online.com>:
> > One possibility: Perl (and TRK) were not compiled on this particular
> > machine, but compiled somewhere else. This "somewhere else" package
> > is broken (as most out-of-site-compiled pieces of software on Unix
> > are).
> Term::ReadKey has been compiled on the respective machines. Perl itself
> not, but I have never compiled a perl myself. I don't want to sound
> ungraceful, but how likely can this be the cause?
Depends on how many alternative ways of action you have, does not it?
Myself, I won't TOUCH a perl compiled on another Unix machine - too
much time will be spend chasing what I would think are bugs in MY code.
> Do you have an idea for another test before I try to recompile perl
Recompiling Perl requires about 2min of human intervention. (Much less
than for me to write this message.)
> and mess up the rest of my system?
There is no need to mess your system until you know whether it solves
your problems. Uninstalled Perl can still be used to test extensions.
Just
env PERL5LIB=.../lib .../perl ARGS
(LD LOAD PATH should also be set - see how `make test' does it).
Hope this helps,
Ilya
------------------------------
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 9078
***************************************