[31582] in Perl-Users-Digest
Perl-Users Digest, Issue: 2841 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 1 09:09:27 2010
Date: Mon, 1 Mar 2010 06:09:09 -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 Mon, 1 Mar 2010 Volume: 11 Number: 2841
Today's topics:
Re: eval exit/exec (was: macros: return or exit) <marc.girod@gmail.com>
Re: eval exit/exec (was: macros: return or exit) <ben@morrow.me.uk>
Re: OT, blowing off steam <RedGrittyBrick@spamweary.invalid>
Parsing HTML with HTML::Tree <nickli2000@gmail.com>
Re: Parsing HTML with HTML::Tree <tadmc@seesig.invalid>
Recursion without lists <simon.andrews@bbsrc.ac.uk>
Re: Recursion without lists <jurgenex@hotmail.com>
Re: Recursion without lists (Jens Thoms Toerring)
Re: Requesting regular expression help sln@netherlands.com
SCIENCE AND SCIENTISTS <mabaloch31@gmail.com>
Use of freed value in iteration - how to workaround? <alexander.farber@gmail.com>
Re: Use of freed value in iteration - how to workaround <sherm@debian.shermpendley.com>
Re: Use of freed value in iteration - how to workaround <alexander.farber@gmail.com>
Re: Use of freed value in iteration - how to workaround <xhoster@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 28 Feb 2010 12:50:43 -0800 (PST)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: eval exit/exec (was: macros: return or exit)
Message-Id: <31c2ddef-5580-40eb-b17b-a9e89d480f60@15g2000yqa.googlegroups.com>
On Feb 28, 3:20=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> No, you haven't, and no, it isn't :(.
Life is hard and then you die...
> The test is wrong because the 'sub exec' happens before the '*Foo::exec
> =3D $exec'. Since this is all one file, the sub is compiled at compile
> time and the assignment happens at run time, so your override gets
> called.
OK.
> (Note that the original sub didn't get called, though, so this
> still isn't quite what you need.)
You mean that I should have noticed that the output wasn't:
Foo::exec: my exec
foo
Right?
Well yes. This is not what I wanted.
I can see that, as Foo is a package and not a class,
Foo::exec is a function and not a method, so that
there is a recusion in the code before any override.
So, there is no solution to discriminate the two in
this test. But fortunately, there is in the real
case...
In the meanwhile, I got a similar problem with my
real code, as I overrode my own member data, because
I had named it $exec, so that it matched *exec...
> However, if you put the assignment in a BEGIN block (as in your real
> setup) you will find that the 'sub exec' completely wipes out your
> override.
OK: I get the recursion effect 'back'.
> The only way around this is to monkey-patch Argv after you
> load it, and replace &Argv::exec with a copy of that sub that calls your
> wrapper instead of the builtin. That is, you need
> =A0 =A0 BEGIN {
> =A0 =A0 =A0 =A0 require Argv;
> =A0 =A0 =A0 =A0 no warnings "redefine";
> =A0 =A0 =A0 =A0 *Argv::exec =3D sub {
> =A0 =A0 =A0 =A0 =A0 =A0 # here copy the body of 'sub exec' from Argv.pm, =
but replace
> =A0 =A0 =A0 =A0 =A0 =A0 # the two calls to 'exec(@cmd)' with '$exec->(@cm=
d)'.
> =A0 =A0 =A0 =A0 };
> =A0 =A0 }
> before anything else tries to use Argv.
Argh. This is ugly (no offense).
But I can see that it might indeed be the only way.
I.e. I do not override the builtin at all, but
only the method, right?
I just don't care anymore for the builtin...
And this is only for Argv, for the other cases,
the previous scheme should work?
I have to sleep over this.
Thanks again...
Marc
------------------------------
Date: Sun, 28 Feb 2010 22:34:10 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: eval exit/exec (was: macros: return or exit)
Message-Id: <2k7r57-pt62.ln1@osiris.mauzo.dyndns.org>
Quoth Marc Girod <marc.girod@gmail.com>:
> On Feb 28, 3:20 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>
> In the meanwhile, I got a similar problem with my
> real code, as I overrode my own member data, because
> I had named it $exec, so that it matched *exec...
Since you are (I hope) using 'my' variables, you can limit their scope
with blocks. If you follow a strict policy of limiting variables to the
scope they're actually needed in, you will be much less likely to stomp
on things you didn't mean to.
> > The only way around this is to monkey-patch Argv after you
> > load it, and replace &Argv::exec with a copy of that sub that calls your
> > wrapper instead of the builtin. That is, you need
>
> > BEGIN {
> > require Argv;
> > no warnings "redefine";
> > *Argv::exec = sub {
> > # here copy the body of 'sub exec' from Argv.pm, but replace
> > # the two calls to 'exec(@cmd)' with '$exec->(@cmd)'.
> > };
> > }
>
> > before anything else tries to use Argv.
>
> Argh. This is ugly (no offense).
> But I can see that it might indeed be the only way.
It is deeply ugly, not to mention seriously fragile if you upgrade Argv
and anything internal changes. I don't think there's anything else you
can do, though (well, beyond using XS to switch out pp_exec for
something else in PL_ppaddr... actually that's not a stupid idea. I
wonder if there's a CPAN module that will do that for you?).
> I.e. I do not override the builtin at all, but
> only the method, right?
> I just don't care anymore for the builtin...
Yes, that's right.
> And this is only for Argv, for the other cases,
> the previous scheme should work?
Yes.
Ben
------------------------------
Date: Mon, 01 Mar 2010 12:31:15 +0000
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: OT, blowing off steam
Message-Id: <4b8bb397$0$2539$da0feed9@news.zen.co.uk>
On 27/02/2010 01:15, ccc31807 wrote:
> On Feb 26, 1:54 pm, sreservoir<sreserv...@gmail.com> wrote:
>>> Typing control-k, LF in vim inserts ^@. Typing control-k, NU inserts
>>> ^U.
>>
>>> Look at the symbols using the :digraphs command.
>>
>> NU ^@
>>
>> ?
>
> I'm using gVim on Windows XP at work, and I'm not at that machine so I
> can't check it now. I could have made a mistake, and if so I'll admit
> it. But I did check it twice, and it's LF = ^@ and NU = ^U.
>
> Like I said, if I made a mistake, I'll let you know on Monday.
>
In vim, insert your character using Control+K L F then position the
cusror over the caret (^) and press ga then press g8. Finally write the
text to a file and look at a hex dump
I inserted aaa control+K L F bbb
ga said 0x00
g8 said 0x0a
On Unix, hd gave
0000 61 61 61 00 62 62 62 0a aaa.bbb.
On Windows, od -t x1 gave
0000000 61 61 61 00 62 62 62 0d 0a
0000011
I conclude that inserting a linefeed into the middle of a line in a text
file is a confusing thing to do in a text editor.
I don't understand how Vim is interpreting these actions or whether it's
display makes sense in context.
I think Keith's right to interpret ^@ as null. ^J would be LF.
--
RGB
------------------------------
Date: Sun, 28 Feb 2010 16:29:59 -0800 (PST)
From: Ninja Li <nickli2000@gmail.com>
Subject: Parsing HTML with HTML::Tree
Message-Id: <0ebbc023-4153-4ec3-81b1-c8bfa6ef51d0@t20g2000yqe.googlegroups.com>
Hi,
I am trying to parsing the following HTML content:
-- first part
<td class="storyTitle"> @0.1.7.4.0.0.5.0.0.11.1
<a href="/GeneralContent/MySearch.aspx?PagePrefix=IN&
target="_new"> @0.1.7.4.0.0.5.0.0.11.1.0
"Chicago"
-- second part
<td class="storyTitle"> @0.1.7.4.0.0.5.0.0.17.1
<b> @0.1.7.4.0.0.5.0.0.17.1.0
"Something here"
I am using HTML:Tree to parse the HTML and what I would like to do is
that whenever there isn't any <a href=.....> segment as in the second
part of the HTML, I will print something else, such as "Error
occurred". Notice that both first and second parts of the HTML have
common text of "<td class="storyTitle">", which I use for search
criteria.
My problem is that I don't know what the following code will return
whenever <a href=...> is not found. I tried to test against "" or
undef, but doesn't seem to work.
The following is some of my code and it doesn't work as I wish.
use strict;
use LWP::Simple;
use HTML::Tree;
if ($td->attr('class') eq 'storyTitle')
{
if (my $sym = $td->find('a'))
{
if ($sym->as_text() ne '')
{
print $sym->as_text() . "\n";
}
else
{
print "Error Occurred" . "\n";
}
}
}
------------------------------
Date: Sun, 28 Feb 2010 21:33:46 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Parsing HTML with HTML::Tree
Message-Id: <slrnhomd5n.7fm.tadmc@tadbox.sbcglobal.net>
Ninja Li <nickli2000@gmail.com> wrote:
> I am using HTML:Tree to parse the HTML and what I would like to do is
> that whenever there isn't any <a href=.....> segment as in the second
> part of the HTML, I will print something else, such as "Error
> occurred".
> My problem is that I don't know what the following code will return
> whenever <a href=...> is not found.
You have a logic problem.
You have written:
if ( found a <a> )
# do something
__END__
So your code cannot to anything if an <a> is not found.
> if ($td->attr('class') eq 'storyTitle')
> {
> if (my $sym = $td->find('a'))
If an <a> is not found then this if-condition is false and the program
is done, none of the code below here will be executed. So you want your
code to be structured something like this:
if (my $sym = $td->find('a')) {
print $sym->as_text(), "\n";
}
else {
print "Error Occurred\n";
}
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Mon, 01 Mar 2010 12:36:24 +0000
From: Simon Andrews <simon.andrews@bbsrc.ac.uk>
Subject: Recursion without lists
Message-Id: <hmgccb$6h4$1@south.jnrs.ja.net>
In a discussion about interview questions the following problem came up:
"Take the numbers 123456789. Insert between each number either a + * or
nothing and find the two equations whose answer is 2002"
Of course I had a go and managed to find a solution, but I'm not really
happy with the way I did it. In particular I'm not wild about:
1) Having to build the full set of symbols before performing the actual
calculations. Is there a simple way to do this which only holds one
equation in memory at a time?
2) The use of eval. In this particular case it's probably the nicest
way to do it, but is it really necessary?
Any thoughts?
Simon.
#!perl
use warnings;
use strict;
my @numbers = (1,2,3,4,5,6,7,8,9);
my @symbols = ('','+','*');
my $options = build_symbol_options([[]]);
foreach my $option (@$options) {
my $equation = '';
for my $index(0..$#numbers-1) {
$equation .= $numbers[$index];
$equation .= $option->[$index];
}
$equation .= $numbers[$#numbers];
my $answer = eval($equation);
if ($answer == 2002) {
print $equation," = 2002\n";
}
}
sub build_symbol_options {
my ($options) = @_;
if (@{$options->[0]} == 8) {
return $options;
}
my $new_options;
foreach my $option (@$options) {
foreach my $symbol (@symbols) {
my @new_symbols = @$option;
push @new_symbols,$symbol;
push @$new_options, \@new_symbols;
}
}
build_symbol_options($new_options);
}
------------------------------
Date: Mon, 01 Mar 2010 05:17:58 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Recursion without lists
Message-Id: <f9eno5t9gp9ldde1pqn7ehmavaaasr1tp6@4ax.com>
Simon Andrews <simon.andrews@bbsrc.ac.uk> wrote:
>In a discussion about interview questions the following problem came up:
>
>"Take the numbers 123456789. Insert between each number either a + * or
>nothing and find the two equations whose answer is 2002"
Is this a test about guessing customer intentions from vague and
actually meaningless or impossible specificiations?
Onehundredtwentythreemillionfourhundredfiftysixthousandsevenhundredeightynine
is only one number. Where is/are the other number(s) this task is
talking about? How do you insert anything between a single element?
There are no equal signs mentioned anywhere in that spec. Where do you
get those "two equations" if you don't have an equal sign?
What does some "answer" have to do with a (non-existing) equation?
Equations don't have answers, at most they may have a solution or are
true or false.
jue
------------------------------
Date: 1 Mar 2010 13:55:10 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Recursion without lists
Message-Id: <7v1v9uFnivU1@mid.uni-berlin.de>
Simon Andrews <simon.andrews@bbsrc.ac.uk> wrote:
> In a discussion about interview questions the following problem came up:
> "Take the numbers 123456789. Insert between each number either a + * or
> nothing and find the two equations whose answer is 2002"
> Of course I had a go and managed to find a solution, but I'm not really
> happy with the way I did it. In particular I'm not wild about:
> 1) Having to build the full set of symbols before performing the actual
> calculations. Is there a simple way to do this which only holds one
> equation in memory at a time?
> 2) The use of eval. In this particular case it's probably the nicest
> way to do it, but is it really necessary?
Concerning your point 2) I have no better idea at the moment
(or at least things would look quite a bit more complicated),
but building the complete list of possible strings first and
only then iterating over them can be avoided if you use re-
cursion (and you can stop once you have the requested two
solutions), e.g. like this:
#!/usr/bin/perl
use strict;
use warnings;
my $cnt = 0;
add_op( '1', 2 );
sub add_op {
my ( $s, $n ) = @_;
add_num( $s . $_, $n ) for ( '', '+', '*' );
}
sub add_num {
my ( $s, $n ) = @_;
if ( $n < 9 ) {
add_op( $s . $n, $n + 1 );
} elsif ( eval $s . $n == 2002 ) {
print "$s$n = 2002\n";
if ( ++$cnt == 2 ) {
exit 0;
}
}
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Sun, 28 Feb 2010 10:34:34 -0800
From: sln@netherlands.com
Subject: Re: Requesting regular expression help
Message-Id: <4p8lo5psv6sflaekv82q04bai5qs2315pi@4ax.com>
On Sun, 28 Feb 2010 09:55:20 -0600, Tad McClellan <tadmc@seesig.invalid> wrote:
>sln@netherlands.com <sln@netherlands.com> wrote:
>> On Sat, 27 Feb 2010 12:33:34 +0100, "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote:
>>
>
>>> while (<FH>)
>>>is actually a shorthand for
>>> while (defined($_ = <FH>))
>>>so even if the last line of a file is "0" without a trailing newline,
>>>the loop will still be executed.
>
>> while (($bar = <DATA>) && $flag )
>> produces traditional behavior (no shortcut), with a nice
>> little unwanted message:
>> Value of <HANDLE> construct can be "0"; test with defined() at ..
>
>
>The warning is wanted for the reason already given, this loop
>might exit earlier than intended (because $flag will not be
>evaluated if $bar eq '0').
>
Thats not why the warning is there. The same warning shows up with
if ($bar=<DATA>) # a conditional
which has no $flag and no shorthand. Perl has to make that distinction,
when asigning to a scalar from <> and used in a conditional, that '0'
is a falsehood. Its no different then
$bar=<DATA>; # not a conditional and no message
if ($bar) # same thing
I'm testing for the truth of $bar. As you mentioned, there
are more than one falsehood, undefined, '', '0', empty list, etc,
everything else is true.
if (defined($bar)) only tests for one truth - is it defined,
but if ($bar) can still be false even though its defined and
with the <> construct, it can only be '0' that makes it false.
So, its more confusing than not when Perl goes behind your back
with this shortcut within a conditional and only within a
while loop, not other constructs. And only then if there
is the single term conditional as a result of an assignment,
ie: $bar=<FH> or just <> ($_=<FH>).
The result is that Perl alters the conditional, significantly,
to get around '0' as a natural *false* condition.
I would rather have it all one way or all the other as far as
conditionals are concerned.
Personally, I always write mine as:
while (defined($bar=<FH>) {}
just incase I want to come back later and add a term to the conditional:
while ($flag && defined($bar=<FH>) {}
Instead, the distinction could be confusing:
while( $bar=<FH> )
{
if (!$bar) {
print "why am I still in this loop\n";
if (defined $bar) {
print "because i'm defined?\n";
if ($bar eq '0') {
print "'$bar' was there any doubt?\n";
}
}
}
...
last if !($bar=<FH>);
...
$bar = <FH>;
last if !bar;
...
}
# or
while( $flag && $bar=<FH> )
{}
The docs don't really say why the shorthand is dropped if
using multiple terms in the conditional. I'm not so sure why
it is done this way, the shorhand, the rules, what the rules are, etc..
Maybe its to get people guessing and to use Deparse.
-sln
------------------------------
Date: Sun, 28 Feb 2010 23:42:16 -0800 (PST)
From: sonia <mabaloch31@gmail.com>
Subject: SCIENCE AND SCIENTISTS
Message-Id: <83be0ea2-eaf7-4247-9275-cf9c7bbb17c6@z11g2000yqz.googlegroups.com>
http://scitechnologyinfo.blogspot.com/
------------------------------
Date: Sun, 28 Feb 2010 10:22:51 -0800 (PST)
From: "A. Farber" <alexander.farber@gmail.com>
Subject: Use of freed value in iteration - how to workaround?
Message-Id: <bbdcc8ab-b35e-4c33-8221-45fb2f980f1e@a18g2000yqc.googlegroups.com>
Hello,
I poll() over TCP-connected clients and sometimes
have to remove them. The following code works ok
for me (since many months):
# add the new client
$pkg->add($tcpSocket) if $Poll->events($tcpSocket) & POLLIN;
for my $child (values %Kids) {
# XXX a child can get removed inbetween
next unless $child;
my $fh = $child->{FH};
my $mask = $Poll->events($fh);
if ($mask & (POLLERR | POLLHUP)) {
$child->remove();
} elsif ($mask & POLLIN) {
unless ($child->read()) {
$child->remove();
next;
}
} elsif ($mask & POLLOUT) {
unless ($child->write()) {
$child->remove();
next;
}
}
}
It probably works ok, because I only remove
the current element ($child) of the array.
Now I have new situation, that I sometimes
have to remove an other element of the array,
while I'm in the $child->read() method.
I've added next unless $child; above,
but still my script dies with the error:
"Use of freed value in iteration".
From searching I know, that this is because
I'm removing an element of an array
whil iterating over it.
But my question is, how could I workaround it?
Thank you
Alex
------------------------------
Date: Sun, 28 Feb 2010 13:59:14 -0500
From: Sherm Pendley <sherm@debian.shermpendley.com>
Subject: Re: Use of freed value in iteration - how to workaround?
Message-Id: <87sk8lp4ot.fsf@debian.shermpendley.com>
"A. Farber" <alexander.farber@gmail.com> writes:
> From searching I know, that this is because
> I'm removing an element of an array
> whil iterating over it.
>
> But my question is, how could I workaround it?
Instead of removing elements immediately, push them
onto another array. Once you're done iterating over
the "main" array, iterate over the "hit list" array
and remove the elements in it from the "main" array.
sherm--
------------------------------
Date: Sun, 28 Feb 2010 11:04:38 -0800 (PST)
From: "A. Farber" <alexander.farber@gmail.com>
Subject: Re: Use of freed value in iteration - how to workaround?
Message-Id: <33e24747-d933-4d54-a1ac-011e2f81f3a0@q21g2000yqm.googlegroups.com>
On Feb 28, 7:59=A0pm, Sherm Pendley <sh...@debian.shermpendley.com>
wrote:
> "A. Farber" <alexander.far...@gmail.com> writes:
> > From searching I know, that this is because
> > I'm removing an element of an array
> > whil iterating over it.
>
> Instead of removing elements immediately, push them
> onto another array. Once you're done iterating over
> the "main" array, iterate over the "hit list" array
> and remove the elements in it from the "main" array.
Good idea, thank you!
------------------------------
Date: Sun, 28 Feb 2010 13:05:53 -0800
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: Use of freed value in iteration - how to workaround?
Message-Id: <4b8ae788$0$11408$ed362ca5@nr5-q3a.newsreader.com>
A. Farber wrote:
> Hello,
>
> I poll() over TCP-connected clients and sometimes
> have to remove them. The following code works ok
> for me (since many months):
>
> # add the new client
> $pkg->add($tcpSocket) if $Poll->events($tcpSocket) & POLLIN;
>
> for my $child (values %Kids) {
> # XXX a child can get removed inbetween
> next unless $child;
If the value has been removed from %Kids, $child will not magically turn
false. (And even if it did, the "next" would occur too late to help).
I don't understand the rest of what you are doing (your code seems to be
using objects, but you don't give any indication of what modules they
are objects of), but perhaps this could help:
foreach my $key (keys %Kids) {
next unless exists $Kids{$key};
my $child=$kids{$key};
Xho
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 V11 Issue 2841
***************************************