[26058] in Perl-Users-Digest
Perl-Users Digest, Issue: 8262 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 21 11:01:46 2005
Date: Wed, 20 Jul 2005 03:05:04 -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 Wed, 20 Jul 2005 Volume: 10 Number: 8262
Today's topics:
Re: "Can't return a temporary from lvalue subroutine... <tassilo.von.parseval@rwth-aachen.de>
Re: copy contructor <abigail@abigail.nl>
Re: copy contructor (Anno Siegel)
Re: ithreads & memory xhoster@gmail.com
Re: ithreads & memory <1usa@llenroc.ude.invalid>
Re: ithreads & memory xhoster@gmail.com
Re: Numeric or character ? <abigail@abigail.nl>
Re: Numeric or character ? <1usa@llenroc.ude.invalid>
Re: Numeric or character ? <abigail@abigail.nl>
Re: Numeric or character ? <jurgenex@hotmail.com>
Regex (?(?{CODE})) has too many branches <spam-block-@-SEE-MY-SIG.com>
Re: Regex (?(?{CODE})) has too many branches <abigail@abigail.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 20 Jul 2005 11:44:06 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <slrndds776.k4j.tassilo.von.parseval@localhost.localdomain>
Also sprach J Krugman:
> FWIW, I found a bug in bugs.perl.org that seems related to the
> error message in the subject line:
>
> http://rt.perl.org/rt3/Ticket/Display.html?id=30582
>
> In particular, one of the follow ups shows a one-liner that elicits
> the bug:
>
> $ perl -MTie::Hash -we'tie %x,"Tie::StdHash"; sub foo:lvalue {$x{foo}} foo=1'
> Can't return a temporary from lvalue subroutine at -e line 1.
>
> As far as I can tell, there doesn't seem to be much momentum to
> get this bug fixed (but I know little of the process, so I could
> be way off-base on this).
Your post here added enough momentum. :-)
The problem was indeed as I described in
<slrnddp6jr.s2.tassilo.von.parseval@localhost.localdomain>.
I submitted a patch for it which got applied roughly eight minutes ago.
Applied means it's now in the development branch 5.9.x. But I assume it
will also be in 5.8.8 whenever it's due at which point you can start to
make use of it to your heart's desire.
Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
------------------------------
Date: 19 Jul 2005 23:08:04 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: copy contructor
Message-Id: <slrnddr1uj.7fo.abigail@alexandra.abigail.nl>
Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMCCCXL
September MCMXCIII in <URL:news:dbj11n$57m$1@mamenchi.zrz.TU-Berlin.DE>:
%%
%% That's why I'm preaching that accessors must be documented. An inheriting
%% class must know what they are and how to override them.
But that's breaking encapsulation. 'Accessor's are only a meaningful
term for the class that defines the accessor - any other class should
not have to know what is an accessor or not. With proper encapsulation,
a class, when viewed from the outside, is a black box. The box keeps
state, and it has some methods where you can poke at the box. Whether
a method happens to return an attribute or not should be irrelevant.
If you need to know, you break encapsulation.
How do you subclass a class that keeps state, but doesn't provide
an accessor?
package Angry::Snake;
sub new {
bless \do {my $c = int rand 5} => shift;
}
sub poke_it_with_a_stick {
my $snake = shift;
$snake -> attack if $$snake -- < 0;
}
sub attack { ... }
Abigail
--
map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";
------------------------------
Date: 20 Jul 2005 09:38:05 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: copy contructor
Message-Id: <dbl61t$e4m$1@mamenchi.zrz.TU-Berlin.DE>
Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMCCCXL
> September MCMXCIII in <URL:news:dbj11n$57m$1@mamenchi.zrz.TU-Berlin.DE>:
> %%
> %% That's why I'm preaching that accessors must be documented. An inheriting
> %% class must know what they are and how to override them.
>
>
> But that's breaking encapsulation. 'Accessor's are only a meaningful
> term for the class that defines the accessor - any other class should
> not have to know what is an accessor or not. With proper encapsulation,
> a class, when viewed from the outside, is a black box. The box keeps
> state, and it has some methods where you can poke at the box. Whether
> a method happens to return an attribute or not should be irrelevant.
> If you need to know, you break encapsulation.
True. The fundamental breach in encapsulation is that Perl objects
have a life of their own, they aren't opaque structures (nothing but
objects) like in most OO languages. That flaw can't be removed by
argumentation and paradigm shifts, it's here to stay.
The distinction in accessors and non-accessors can help in making
that breach manageable, not make it go away.
By publishing which methods are accessors and which aren't, you are
making a considerable implementational commitment. In particular,
you can't make an accessor out of what used not to be one. Your
client's code would break.
> How do you subclass a class that keeps state, but doesn't provide
> an accessor?
>
> package Angry::Snake;
>
> sub new {
> bless \do {my $c = int rand 5} => shift;
> }
>
> sub poke_it_with_a_stick {
> my $snake = shift;
> $snake -> attack if $$snake -- < 0;
> }
>
> sub attack { ... }
Mmmmm, nice snakey, pretty snakey, I'm not poking, okay...?
No, you can't inherit ->poke_with_a_stick. That's because it's an
accessor, even if it is not a field accessor in the usual sense.
It accesses (de-references) the object, and so it's out.
If Angry::Snake were written with an overridable method (->snake)
guarding the access, like this:
sub poke_it_with_a_stick {
my $snake = shift;
$snake -> attack if ${ $snake->snake} -- < 0;
}
sub snake { shift }
things would be different. Here's a less aggressive snake that doesn't
always notice when it is poked:
package Sleepy::Snake;
BEGIN { our @ISA = 'Angry::Snake' }
sub new {
bless {
sleepiness => 0.5,
snake => Angry::Snake->new,
}, shift;
}
sub snake { $_[ 0]->{ snake} }
sub poke_it_with_a_stick {
my $sn = shift;
$sn->SUPER::poke_it_with_a_stick if rand > $sn->sleepiness;
}
sub sleepiness {
$_[ 0]->{ sleepiness} = shift if @_ > 1;
$_[ 0]->{ sleepiness};
}
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: 19 Jul 2005 23:09:48 GMT
From: xhoster@gmail.com
Subject: Re: ithreads & memory
Message-Id: <20050719190948.661$mE@newsreader.com>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>
> Is there a reason you are using the same $th_n for all the threads over
> and over again?
>
> Consider the code below:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use threads;
> use Time::HiRes "usleep";
>
> sub th { usleep 10000; print "$_[0]: Done\n" }
>
> my $th_n;
> for (1 .. 100) {
> $th_n = threads->new(\&th, $_);
> $th_n->detach;
> usleep 100000;
> }
>
> sleep 1 while 1;
>
> __END__
>
> When I ran this code on my system (Windows XP SP2, Perl 5.8.7), the
> memory footprint of the Perl process grew to 46,696K and stayed there.
> This looks like a standard leaker to me (although I could not describe
> to you the exact steps in which resources are being leaked), but it is,
> in principle, similar to the following C code:
>
> #include <stdlib.h>
>
> void leak(void) {
> char *p;
> while( 1 ) {
> p = malloc(1);
> }
> }
It is not similar in principle to that, because in principle Perl has a
garbage collector and C doesn't. I don't see how this behavior can be
anything other than a bug.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Tue, 19 Jul 2005 23:56:03 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: ithreads & memory
Message-Id: <Xns9698CAC5A18A6asu1cornelledu@127.0.0.1>
xhoster@gmail.com wrote in news:20050719190948.661$mE@newsreader.com:
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>> there. This looks like a standard leaker to me (although I could not
>> describe to you the exact steps in which resources are being leaked),
>> but it is, in principle, similar to the following C code:
>>
>> #include <stdlib.h>
>>
>> void leak(void) {
>> char *p;
>> while( 1 ) {
>> p = malloc(1);
>> }
>> }
>
> It is not similar in principle to that, because in principle Perl
> has a garbage collector and C doesn't.
OK, that was a bad diagnosis and bad analogy.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 20 Jul 2005 00:18:29 GMT
From: xhoster@gmail.com
Subject: Re: ithreads & memory
Message-Id: <20050719201829.676$1w@newsreader.com>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
> xhoster@gmail.com wrote in news:20050719190948.661$mE@newsreader.com:
>
> > "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
> >> there. This looks like a standard leaker to me (although I could not
> >> describe to you the exact steps in which resources are being leaked),
> >> but it is, in principle, similar to the following C code:
> >>
> >> #include <stdlib.h>
> >>
> >> void leak(void) {
> >> char *p;
> >> while( 1 ) {
> >> p = malloc(1);
> >> }
> >> }
> >
> > It is not similar in principle to that, because in principle Perl
> > has a garbage collector and C doesn't.
>
> OK, that was a bad diagnosis and bad analogy.
Nah, I think it was a good diagnosis and a bad analogy. :)
On linux, too, the in-the-loop "my" makes it much more memory efficient.
I just can't figure out why.
Xho
>
> Sinan
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 19 Jul 2005 23:25:32 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Numeric or character ?
Message-Id: <slrnddr2vc.7fo.abigail@alexandra.abigail.nl>
A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCCCXL September
MCMXCIII in <URL:news:Xns9698B4F254690asu1cornelledu@127.0.0.1>:
//
// BTW, the variables $a and $b are special in Perl
// (see perldoc -f sort). You should not use them willy-nilly.
I use $a and $b willy-nilly all the time, and I have never ran into
trouble. While it is possible to create code that might do something
unexpected, it takes a bit of an effort to do so.
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: Tue, 19 Jul 2005 23:59:20 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Numeric or character ?
Message-Id: <Xns9698CB53C6281asu1cornelledu@127.0.0.1>
Abigail <abigail@abigail.nl> wrote in
news:slrnddr2vc.7fo.abigail@alexandra.abigail.nl:
> A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCCCXL September
> MCMXCIII in <URL:news:Xns9698B4F254690asu1cornelledu@127.0.0.1>:
> //
> // BTW, the variables $a and $b are special in Perl
> // (see perldoc -f sort). You should not use them willy-nilly.
>
> I use $a and $b willy-nilly all the time, and I have never ran into
> trouble. While it is possible to create code that might do something
> unexpected, it takes a bit of an effort to do so.
True, but I have a knack for unintentionally discovering the complicated
steps to produce unlikely scenarios while doing somthing completely
mundane.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 20 Jul 2005 00:19:37 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Numeric or character ?
Message-Id: <slrnddr64p.7fo.abigail@alexandra.abigail.nl>
A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCCCXL September
MCMXCIII in <URL:news:Xns9698CB53C6281asu1cornelledu@127.0.0.1>:
{} Abigail <abigail@abigail.nl> wrote in
{} news:slrnddr2vc.7fo.abigail@alexandra.abigail.nl:
{}
{} > A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCCCXL September
{} > MCMXCIII in <URL:news:Xns9698B4F254690asu1cornelledu@127.0.0.1>:
{} > //
{} > // BTW, the variables $a and $b are special in Perl
{} > // (see perldoc -f sort). You should not use them willy-nilly.
{} >
{} > I use $a and $b willy-nilly all the time, and I have never ran into
{} > trouble. While it is possible to create code that might do something
{} > unexpected, it takes a bit of an effort to do so.
{}
{} True, but I have a knack for unintentionally discovering the complicated
{} steps to produce unlikely scenarios while doing somthing completely
{} mundane.
If I do something mundane, I'm using lexical variables anyway. ;-)
Abigail
--
perl -swleprint -- -_='Just another Perl Hacker'
------------------------------
Date: Wed, 20 Jul 2005 01:35:47 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Numeric or character ?
Message-Id: <TThDe.35558$Tx1.27456@trnddc03>
John Cecere wrote:
> Since
>
> if ("aaa" == 0) {
> print "true\n";
> }
>
> will print true, how does one go about testing a value as an integer.
> For example, the user enters data:
> $a=<STDIN>;
>
> The user might accidentally type in character data,
Well, I hope he does. How could he type anything that is not a character?
> but I need to
> perform a test to see if what they entered is a number between 0 and
> 255, and return a bad status for anything else.
First of all I suggest to get your terminology right. The user enters a
sequence of characters. You want to restrict this to digits. And then Perl
can interpret a sequence of digits as a number. While this might seem to be
nitpicking it actually helps immensely to partition your problem in the
right way.
Having said that, is there anything wrong with the answer given in the FAQ?
perldoc -q number:
"How do I determine whether a scalar is a number/whole/integer/float?"
> Doing this:
> if ($a < 0 || $a > 255)
>
> won't catch character data.
By some definiton of "character" which has nothing to do with the standard
definition of character.
Of course it doesn't because you are comparing the numerical value of a
scalar. If you want to test the characters of string (yes, even digits are
characters) then use a function that uses strings or characters, e.g.
pattern matching or substr or pos or similar.
> What's the _simple_ way of doing this ?
Maybe the way as suggested in the FAQ?
jue
------------------------------
Date: Wed, 20 Jul 2005 01:35:58 +0100
From: James Taylor <spam-block-@-SEE-MY-SIG.com>
Subject: Regex (?(?{CODE})) has too many branches
Message-Id: <ant200058ab5fNdQ@riscpc.jtnet>
I have a web page in $page and I'm trying to scrape out a
particular table of information from it. There are many
tables in the page and some of them are nested. The table
I'm interested in is unique in that it satisfies both of the
following two conditions:
1. It does not contain any nested tables
2. It does contain cells with class="whiteHeading"
I feel it ought to be possible to extract the table with a
single regex and, even if only as a learning exercise, I'd
like to know how to achieve it. This is what I've got so far:
my ($table) = $page =~ m{
(?> <table\b.*?> (.*?) </table> ) # Get table without backtracking
(?(?{
$1 !~ /<table\b/i && # Must not contain another table
$1 =~ /\bclass="whiteHeading"/i # and must contain white headings
}) | _FAIL_ )
}six;
Unfortunately, this generates the following error:
/\bclass="whiteHeading"/: Switch (?(condition)... contains too many
branches at myprog line 66.
Is my mistake obvious? How else can I match a portion that
does NOT contain a particular substring?
Thanks.
--
James Taylor, London, UK PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.
------------------------------
Date: 20 Jul 2005 07:45:57 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Regex (?(?{CODE})) has too many branches
Message-Id: <slrndds09k.7fo.abigail@alexandra.abigail.nl>
James Taylor (spam-block-@-SEE-MY-SIG.com) wrote on MMMMCCCXLI September
MCMXCIII in <URL:news:ant200058ab5fNdQ@riscpc.jtnet>:
@@ I have a web page in $page and I'm trying to scrape out a
@@ particular table of information from it. There are many
@@ tables in the page and some of them are nested. The table
@@ I'm interested in is unique in that it satisfies both of the
@@ following two conditions:
@@
@@ 1. It does not contain any nested tables
@@ 2. It does contain cells with class="whiteHeading"
@@
@@ I feel it ought to be possible to extract the table with a
@@ single regex and, even if only as a learning exercise, I'd
@@ like to know how to achieve it. This is what I've got so far:
@@
@@ my ($table) = $page =~ m{
@@ (?> <table\b.*?> (.*?) </table> ) # Get table without backtracking
@@ (?(?{
@@ $1 !~ /<table\b/i && # Must not contain another table
@@ $1 =~ /\bclass="whiteHeading"/i # and must contain white headings
@@ }) | _FAIL_ )
@@ }six;
@@
@@ Unfortunately, this generates the following error:
@@
@@ /\bclass="whiteHeading"/: Switch (?(condition)... contains too many
@@ branches at myprog line 66.
I don't know about this syntax(?) error, but even if you manage to work
around it, it's not going to work. The regex engine is not re-entrant,
which means that you can not use a regex from within a (?{ }) or (??{ })
construct.
@@ Is my mistake obvious? How else can I match a portion that
@@ does NOT contain a particular substring?
Use index().
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: 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 8262
***************************************