[27460] in Perl-Users-Digest
Perl-Users Digest, Issue: 9079 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 23 14:05:51 2006
Date: Thu, 23 Mar 2006 11: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: 9079
Today's topics:
Re: Code review request. <zen13097@zen.co.uk>
Re: Code review request. <a24061@yahoo.com>
Re: Code review request. <someone@example.com>
Re: Code review request. <tadmc@augustmail.com>
Re: Parallel LWP callback doesn't terminate. xhoster@gmail.com
Re: unicode conversion <corff@zedat.fu-berlin.de>
Re: XML::Simple and utf8 woes <chronos@chronos-tachyon.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 23 Mar 2006 08:52:26 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: Code review request.
Message-Id: <442261ca$0$8344$da0feed9@news.zen.co.uk>
Michael Press <jack@abc.net> 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
With modern perls "use warnings" is better than -w
Also, remember to "use strict".
> sub pm;
>
> while (<DATA>) { pm $_ }
A matter of personal taste, but I'm not keen on sub pre-declaration
(I just think it adds avoidable noise) . I'd either define the
function before it's used:
sub pm {...}
while (<DATA>) { pm $_ }
or use parentheses when calling the sub to avoid the need for
pre-declaration:
while (<DATA>) { pm($_) }
sub pm {...}
>
> sub pm
A horrible name for a subroutine - I can guess from your description
above that it means "permute" (or something similar) but a more
descriptive name will greatly aid future code maintenance.
> {
> my $z;
> my @line;
> my %p;
More horrible variable names! z and p mean nothing (to me). @line doesn't
seem to be holding lines, so its name is misleading.
Also, your code would be more readable (and shorter) if you declared
the variables where you first use them (see below)
>
> # Parse the cycles, and initialize the permutation map.
> $z = shift (@_);
my $line = shift;
> @line = $z =~ m/(\w+|[()])/g;
my @tokens = $line =~ m/(\w+|[()])/g;
> %p = map { $_ => $_ } grep { m/\w+/} @line;
my %p = map { $_ => $_ } grep { m/\w+/} @tokens;
> printf ("%s\n", join ':', @line), "\n";
'printf (...) interpreted as function at - line 18.'
Why enable warnings, only to ignore them?
printf "%s\n", join(':', @tokens), "\n"; #(untested)
> while (( $key, $value) = each (%p)) { printf "%7s%7s\n", $key, $value}
You don't declare $key/$value ("use strict" would have caught this).
Another use of variable names that are too generic and thus make code
maintenance difficult.
HTH.
------------------------------
Date: Thu, 23 Mar 2006 09:00:57 +0000
From: Adam Funk <a24061@yahoo.com>
Subject: Re: Code review request.
Message-Id: <9n4af3-90q.ln1@news.ducksburg.com>
On 2006-03-23, Dave Weaver <zen13097@zen.co.uk> wrote:
> With modern perls "use warnings" is better than -w
I didn't know that -- what's the difference?
> Also, remember to "use strict".
Definitely useful advice!
------------------------------
Date: Thu, 23 Mar 2006 12:56:02 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Code review request.
Message-Id: <CVwUf.472$B_1.192@edtnps89>
Dave Weaver wrote:
> Michael Press <jack@abc.net> wrote:
>>
>> printf ("%s\n", join ':', @line), "\n";
>
> 'printf (...) interpreted as function at - line 18.'
>
> Why enable warnings, only to ignore them?
>
> printf "%s\n", join(':', @tokens), "\n"; #(untested)
You have a somewhat similar problem to the OP's code in that the ', "\n"' at
the end is not doing anything (there is no format in the format string that
uses that value.)
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 23 Mar 2006 09:17:32 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Code review request.
Message-Id: <slrne25f0c.46l.tadmc@magna.augustmail.com>
Adam Funk <a24061@yahoo.com> wrote:
> On 2006-03-23, Dave Weaver <zen13097@zen.co.uk> wrote:
>
>> With modern perls "use warnings" is better than -w
>
> I didn't know that -- what's the difference?
The first sentence of the description in:
perldoc warnings
tells the difference.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 23 Mar 2006 16:00:57 GMT
From: xhoster@gmail.com
Subject: Re: Parallel LWP callback doesn't terminate.
Message-Id: <20060323110859.954$zU@newsreader.com>
"Peter Hill" <peter.hill@modulus.com.au> wrote:
> 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?
...
> sub callback_for_parse {
> my ($content, $response, $protocol, $entry) = @_;
> if (length $content) {
> if (length($response->content) < $MAX_SIZE and
> $response->content_type =~ /text\/html/i) {
...
> 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;
> }
>
As far as I can tell, the only error you are committing is in your
expectations, not in your code. The only way "We never get here"
should be printed is if you either get called with empty content (and why
would that happen? If there is nothing to send to the callback, why
call it?), or with an over-sized chunk. Otherwise, the
"return length $content;" will be activated, by-passing the print.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 23 Mar 2006 10:54:51 GMT
From: <corff@zedat.fu-berlin.de>
Subject: Re: unicode conversion
Message-Id: <48fd3rFjok7sU1@uni-berlin.de>
corff@zedat.fu-berlin.de wrote:
: No. You'd say
: my $w=chr(25163.chr(...)....;
Should read:
: my $w=chr(25163).chr(...)....;
Please accept my apologies for the typo.
Oliver.
--
Dr. Oliver Corff e-mail: corff@zedat.fu-berlin.de
------------------------------
Date: Thu, 23 Mar 2006 11:21:23 -0600
From: Chronos Tachyon <chronos@chronos-tachyon.net>
Subject: Re: XML::Simple and utf8 woes
Message-Id: <mOAUf.846$wC1.745@dukeread01>
[Whoops, meant to post, not mail]
corff@zedat.fu-berlin.de wrote:
> Dear wizards,
>
> I use XML::Simple to parse an XML file and
> also to write it out. The problem lies in the
> utf8 character data contained in the XML
> source. While the XMLin() function seems
> to read them properly, the XMLout() function
> tries to replace utf8 material by multibyte
> nonsense.
>
> Below is my minimal example, run under perl 5.8.5
> on a Fedora C3 box. Just compare the output
> of the script (in w.xml) with its input, in DATA.
>
> Please advice on how to fix the broken utf8 output.
>
> Thanks in advance,
> Oliver.
>
> #!/usr/bin/perl
> use XML::Simple;
> print "Reading data from XML source...\n";
> $data=XMLin(\*DATA,
> ForceArray=>[manju,hauer],
> ContentKey=>'-content',
> KeyAttr=>[name],
> );
> print "Retrieve and display data example:\n";
> $k='0004.1';
> print $k.": ".
> $data->{lemma}->{$k}->{manju}->[0].
> "\n";
> print "Writing data to XML file...\n";
> XMLout($data,
> NumericEscape=>0,
> RootName=>'wuti',
> XMLDecl=>1,
> OutputFile=>'w.xml',
> );
> __DATA__
> <?xml version='1.0' encoding='utf-8' standalone='yes'?>
> <wuti>
> <lemma name="0004.1">
> <hauer>in der Morgendämmerung (H).</hauer>
> <manju>farhûn suwaliyame</manju>
> </lemma>
> <lemma name="0004.2">
> <hauer>Morgendämmerung.</hauer>
> <manju>gersi fersi</manju>
> </lemma>
> </wuti>
>
The problem seems to be the absence of a "use utf8;" pragma. Perl is
assuming that your code (including the __DATA__ section) is in ISO-8859-1.
[Addendum: FWIW, your newsreader is also making the same assumption.]
--
Donald King, a.k.a. Chronos Tachyon
http://chronos-tachyon.net/
------------------------------
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 9079
***************************************