[23743] in Perl-Users-Digest
Perl-Users Digest, Issue: 5948 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 17 11:05:46 2003
Date: Wed, 17 Dec 2003 08:05: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 Wed, 17 Dec 2003 Volume: 10 Number: 5948
Today's topics:
Re: Creating a list of HASHes (Patrick Paquet)
Re: Creating a list of HASHes (Anno Siegel)
Re: How to determine end-of-line sequence? (Anno Siegel)
Re: HTML::Form->Parse (Perl) not working under Fedora C <noreply@gunnar.cc>
Re: HTML::Form->Parse (Perl) not working under Fedora C (Jeffrey J. Kosowsky)
Re: HTML::Form->Parse (Perl) not working under Fedora C (Jeffrey J. Kosowsky)
Re: HTML::Form->Parse (Perl) not working under Fedora C <noreply@gunnar.cc>
Re: Login to site with random image code? <tcurrey@no.no.i.said.no>
Re: Login to site with random image code? <dmcbride@naboo.to.org.no.spam.for.me>
Q about a module containing more than one class <bik.mido@tiscalinet.it>
Re: reading from an url <jwillmore@remove.adelphia.net>
Re: recursive closures? (Anno Siegel)
Re: recursive closures? <tassilo.parseval@rwth-aachen.de>
Re: recursive closures? <nobull@mail.com>
Re: recursive closures? <uri@stemsystems.com>
Re: recursive closures? <lmai@mytum.de>
Re: recursive closures? <uri@stemsystems.com>
Redirect url user@domain.invalid
Re: Redirect url <noreply@gunnar.cc>
RegExp check for nothing or pattern <Eric@nowhere.com>
Re: RegExp check for nothing or pattern <carsten@welcomes-you.com>
Re: RegExp check for nothing or pattern <noreply@gunnar.cc>
RegExp to match pattern or BLANK? <Eric@nowhere.com>
Re: RegExp to match pattern or BLANK? <zawrotny@jaguar.sb.fsu.edu>
Re: Shrikage: regexp rookie <bik.mido@tiscalinet.it>
Re: Simple text file operation (Tad McClellan)
Re: ticks and FreeBSD <bik.mido@tiscalinet.it>
Re: ticks and FreeBSD (Tad McClellan)
Re: Where do -T warnings go? <jwillmore@remove.adelphia.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 17 Dec 2003 03:10:47 -0800
From: jack_luminous@hotmail.com (Patrick Paquet)
Subject: Re: Creating a list of HASHes
Message-Id: <14e35cbb.0312170310.22a6ac66@posting.google.com>
Hi,
I'm replying to the original message because both Richard Gration and
A. Sinan Unur have provided interesting and useful information, I hope
this isn't a major breach of group etiquette.
I should've probably said this in the original post, sorry, my bad.
What I am aiming to do in short, is this:
1. Fetch list of workstations from the domain controller,
2. Perform a series of tests on each machine,
3. Write out (or update) the information to an SQL db.
To do this, I felt that creating a hash that included all the test
results for each machine would be the simplest since I can use SQL
column headers for $Keys, I can then easily create SQL statements from
the HASH. However, the problem seems to be that I can't pass the HASH
that was created to a subroutine
because it wasn't defined when USE STRICT is ON, it works fine without
the parma, but I'd rather do it 'right'. As was pointed out, if I
pass a HASH by value, it will be flattened to a list, which is not
what I'm looking for. I'm open to suggestions on a different
approach.
Both Richard Gration and A. Sinan Unur suggested that perhaps I was
trying to pass %MachineInfo to &EcritureSQL. This was not the case
originally, &MachineInfo was meant to contains the existing (if any)
information from the database. I realized, after a bit of thought,
that there really isn't a reason to create a second HASH, since the
first one already contains all of the column headers as $Keys, all I
need to do is update the existing one with the new results and write
that to the SQL db. And that's one point for simplification.
I've modified the code slightly to reflect your suggestions (properly
scoped variables thru my, and proper formating for easy copy/paste
execution). But now the error message has moved to the SUB level, so
I guess that the HASH is being passed on correctly, except I can't
access the elements in it, I get the same message as before, "Can't
use string ("0") as a HASH ref while "strict refs" in use at...", line
is noted in the code below.
Thanks again for your time and insight.
Patrick Paquet
################################################################
use strict;
use Win32::ODBC;
use File::Find;
use Win32API::Net;
use Win32::NetAdmin;
use Net::ping;
use Net::hostent;
use Socket;
my @ListDOM = qw/Machine1 Machine2 Machine3/;
foreach my $Machine (@ListDOM)
{
my $CheckMachine = 0;
my %MachineInfo;
# my %MachineInfo = &ReadSQL($ConnectionSQL,$Machine);
# Previous line commented to allow execution, otherwise runs OK
if ($MachineInfo{MachineName} ne $Machine)
{
$CheckMachine = 1;
print "$Machine n'existe pas dans la base.($CheckMachine)\n";
}
my $ValeurCle = 123456;
# Previous line normally gets data from: &GetValeurCle($Machine)
if ($MachineInfo{Valeur_CiPatches} != $ValeurCle)
{
$CheckMachine = 1;
print "$Machine: valeur cle differente.($CheckMachine)\n";
}
my ($Ping,$ReverseLookup,$AdresseIP) = ("OK","OK","10.10.10.10");
# previous line normally gets data from: &GetIP($Machine)
if ($MachineInfo{IP_address} ne $AdresseIP)
{
$CheckMachine = 1;
print "$Machine: adresse IP differente.($CheckMachine)\n";
}
if ($CheckMachine == 0)
{
print "Base a jour pour $Machine, machine suivante...\n\n";
}
else
{
print "Checks values on workstation...\n\n";
EcritureSQL(%MachineInfo);
# previous line passes HASH to subroutine to write to SQL db
} # Fin de la verification d'une machine
}
sub EcritureSQL # Ecriture des infos dans la base SQL
{
# Doesn't do much at this point, until I get that error sorted out.
my ($Key, $Value, %InfosMachine);
my $InfosMachine = @_;
### ERROR IS ON FOLLOWING LINE
while (($Key, $Value)=each %$InfosMachine)
{
print "Key: $Key \->Value: $Value\n";
}
my $Pause = <STDIN>;
}
------------------------------
Date: 17 Dec 2003 12:00:07 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Creating a list of HASHes
Message-Id: <brpgg7$f83$1@mamenchi.zrz.TU-Berlin.DE>
Patrick Paquet <jack_luminous@hotmail.com> wrote in comp.lang.perl.misc:
[...]
> information from the database. I realized, after a bit of thought,
> that there really isn't a reason to create a second HASH, since the
> first one already contains all of the column headers as $Keys, all I
> need to do is update the existing one with the new results and write
> that to the SQL db. And that's one point for simplification.
Okay, but you ought to have some means to tell new information (that
isn't in the DB yet) from old information. Otherwise you'd have to
write back everything on every update.
> I've modified the code slightly to reflect your suggestions (properly
> scoped variables thru my, and proper formating for easy copy/paste
> execution). But now the error message has moved to the SUB level, so
Yes. Let's concentrate on that.
[snip]
So you have a subroutine:
> sub EcritureSQL # Ecriture des infos dans la base SQL
> {
> # Doesn't do much at this point, until I get that error sorted out.
> my ($Key, $Value, %InfosMachine);
Declare your variables in the smallest possible scope. Declaring them
together with the first use (if possible) guarantees that. Scratch
the last line.
> my $InfosMachine = @_;
Note that at this point you have declared two variables with the name
"InfosMachine", one hash (%InfosMachine) and one scalar ($InfosMachine).
These variables have nothing to do with each other, and setting one
will leave the other alone.
Here you are setting (the scalar) $InfosMachine to the array value of
@_. An array in scalar context is the number of its elements, so you
have just set $InfosMachine to the number of arguments your sub was
called with. Not useful.
Change the last line to
my %InfosMachine = @_;
> ### ERROR IS ON FOLLOWING LINE
> while (($Key, $Value)=each %$InfosMachine)
Now you need
while ( my ($Key, $Value) = each %$InfosMachine )
> {
> print "Key: $Key \->Value: $Value\n";
> }
> my $Pause = <STDIN>;
> }
>
> EcritureSQL(%MachineInfo);
Now this call should work.
When you have this up and running you may want to change the passing of
the hash to a reference (which may win a little speed), but I suggest
you try the straight-forward way first.
Let me add that you have done a good job with the code you prepared.
Even though I didn't actually run it, it's a far easier read when you
can see the author made an effort to make it self-contained.
Anno
------------------------------
Date: 17 Dec 2003 11:06:33 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How to determine end-of-line sequence?
Message-Id: <brpdbp$a00$2@mamenchi.zrz.TU-Berlin.DE>
David Efflandt <efflandt@xnet.com> wrote in comp.lang.perl.misc:
[...]
> is. Following is a script I wrote awhile ago that converts any text file
> type to what you want based on the script name, but could be easily
> modified (shortened) to convert to a single type:
>
> #!/usr/bin/perl -w
> # txtconv - convert text to or from other OS
> # Symlink or rename any of: tounix, 2unix, todos, 2dos, tomac, 2mac
> # follow with list of files on commandline
> # "tomac" may conflict with a program name
> while(@ARGV) {
> my $file = shift @ARGV;
> unless (open(FILE,"+< $file")) {warn "Can't open $file: $!\n"; next;}
> flock(FILE,2); seek(FILE,0,0); binmode FILE;
^^^^^^^^^^^^^
> print "$file before ", -s $file;
> my @lines = <FILE>; seek(FILE,0,0); truncate(FILE,0);
> if (lc($0) =~ /2unix|tounix/) { $end = "\012" }
> elsif (lc($0) =~ /2dos|todos/) { $end = "\015\012" }
> elsif (lc($0) =~ /2mac|tomac/) { $end = "\015" }
> else { die "file not converted, read $0\n" }
> foreach (@lines) { s/(\015\012|[\015\012])/$end/g; print FILE $_; }
> close FILE;
> print " after ", -s $file,"\n";
> }
Just out of interest -- why are you locking the file? I mean, EOL
conversion is not something that is normally done concurrently :)
Anno
------------------------------
Date: Wed, 17 Dec 2003 15:09:28 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: HTML::Form->Parse (Perl) not working under Fedora Core 1
Message-Id: <brpoci$5og5m$1@ID-184292.news.uni-berlin.de>
Jeffrey J. Kosowsky wrote:
> Gunnar Hjalmarsson writes:
>> Jeffrey J. Kosowsky wrote:
>>> I have been having trouble getting the HTML::Form library to
>>> work
>>
>> You are printing the object reference!
>
> I know that -- but the fact is that it returns an empty reference
> -- i.e. no form! (which is all I was trying to show with this short
> dummy script)
It worked fine when I tried it, which probably is the reason why I
misunderstood the nature of the problem. Sorry!
> So... my question still stands...
Okay, let me try again:
First I'd add the -w switch to the shebang line (to enable warnings
dynamically) and enable strictures. Then instead of
my $form = HTML::Form->parse($res->content, $res->base());
I'd do:
my $form;
eval { $form = HTML::Form->parse($res->content, $res->base()) };
die $@ if $@;
to try to capture the explanation in the error log.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 17 Dec 2003 14:57:44 GMT
From: kosowsky@consult.pretender (Jeffrey J. Kosowsky)
Subject: Re: HTML::Form->Parse (Perl) not working under Fedora Core 1
Message-Id: <m2iskfwkyf.fsf@consult.pretender>
kosowsky@consult.pretender (Jeffrey J. Kosowsky) writes:
> I have been having trouble getting the HTML::Form library to work
> under Fedora Core 1 (perl 5.8.1-92 with perl-libwww-perl-5.65-6). For
> some reason, the function HTML::Form->Parse doesn't return any forms
> for me.
>
> For example, the following simple perl script:
> #! /usr/bin/perl
>
> use HTML::Form;
> use HTTP::Request;
> use LWP;
>
> my $ua = new LWP::UserAgent;
> my $uri="http://www.google.com";
> my $req = HTTP::Request->new(GET => $uri);
> my $res = $ua->request($req);
> print $res->content;
> my $form = HTML::Form->parse($res->content, $res->base());
> print "Form: ${form}\n";
>
> Returns the source of the www.google.com page but fails to return the
> obvious embedded form.
Definitely seems to be a problem with Fedora Core1 version of Perl
(and/or anything I may have done subsequently to screw up my Perl
installation :), because when I 'chroot'ed over to my old RH8.0
installation, everything worked again as expected.
On the other hand, I really have a pretty stock Fedora installation
and I have not done anything to perl except to install one package
(perl-Video-DVDRip_0.50.16-2.fr_i386.rpm). Also, my other basic perl
scripts seem to still work.
Any suggestions?
------------------------------
Date: Wed, 17 Dec 2003 15:00:49 GMT
From: kosowsky@consult.pretender (Jeffrey J. Kosowsky)
Subject: Re: HTML::Form->Parse (Perl) not working under Fedora Core 1
Message-Id: <m2ekv3wkta.fsf@consult.pretender>
Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
> Jeffrey J. Kosowsky wrote:
> > Gunnar Hjalmarsson writes:
> >> Jeffrey J. Kosowsky wrote:
> >>> I have been having trouble getting the HTML::Form library to
> >>> work
> Okay, let me try again:
>
> First I'd add the -w switch to the shebang line (to enable warnings
> dynamically) and enable strictures. Then instead of
>
> my $form = HTML::Form->parse($res->content, $res->base());
>
> I'd do:
>
> my $form;
> eval { $form = HTML::Form->parse($res->content, $res->base()) };
> die $@ if $@;
>
> to try to capture the explanation in the error log.
Thanks for the suggestion.
Tried it, but still get the following error message:
Can't call method "form" on an undefined value at ./myscript.pl line 17.
You mention an error log, but not sure which log you are referring to.
I have a pretty stock Fedora Core 1 implementation with only one added
perl package (perl-Video-DVDRip_0.50.16-2.fr_i386.rpm) beyond the
standard perl packages that come with Fedora. Also, when I 'chroot'ed
over to my old RedHat8.0 installation, it all worked fine.
My other perl scripts seem to work fine, so I am assuming perl is
basically intact.
Any help on how to debug/fix further?
------------------------------
Date: Wed, 17 Dec 2003 16:09:46 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: HTML::Form->Parse (Perl) not working under Fedora Core 1
Message-Id: <brpru8$6928d$1@ID-184292.news.uni-berlin.de>
Jeffrey J. Kosowsky wrote:
> Gunnar Hjalmarsson writes:
>> First I'd add the -w switch to the shebang line (to enable
>> warnings dynamically) and enable strictures. Then instead of
>>
>> my $form = HTML::Form->parse($res->content, $res->base());
>>
>> I'd do:
>>
>> my $form;
>> eval { $form = HTML::Form->parse($res->content, $res->base()) };
>> die $@ if $@;
>>
>> to try to capture the explanation in the error log.
>
> Thanks for the suggestion.
> Tried it, but still get the following error message:
> Can't call method "form" on an undefined value at ./myscript.pl
> line 17.
So then we know that the module does not generate a fatal error. (I
assume that line 17 is after the die statement.)
> You mention an error log, but not sure which log you are referring
> to.
Well, maybe I'm 'thinking CGI'... If you run the program from the
command line, you should see any error or warning messages.
> Any help on how to debug/fix further?
I'm out of ideas now. Sorry.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 17 Dec 2003 05:57:50 -0800
From: "Trent Curry" <tcurrey@no.no.i.said.no>
Subject: Re: Login to site with random image code?
Message-Id: <brpni8$gq4$1@news.astound.net>
pkent wrote:
> In article <oTGDb.323$Z57.278@newssvr27.news.prodigy.com>,
> "Lucas Van Hieng" <l.v.g@moskuetiez.de> wrote:
>
>> (http://www.starlance.us/MW4/)
>>
>> They recently redid the site, so now it requires that you enter the
>> random 5 digit number shown on a png image. (Before there was no such
>> extra security and I was able to POST with LWP:UserAgent.)
>>
>> Is there way at all around these things? I'm guessing that this
>> would be easier than with what MSN and Yahoo use, which is often a
>> scrambled mess. This site however uses uniform text (as if just
>> typed into the image with text tool and saved.) Is there anyway to
>> do some sort of OCR (char recognition) on the fly?
>
> Funnily enough we're looking at implementing a similar system at work.
> Aaanyway...
>
> From looking at it it does appear that the image uses only 2 colours -
> the foreground and the background. There seems to be a 1 pixel gap
> between each digit. The code appears to use only the digits 0 to 9.
> The font doesn't vary and seems to be a variable-width font. The
> image seems to be the same as long as you have the same PHPSESSIONID
> cookie from them.
Why not use that sessionid? If it changes each time perhaps there is a
corralation?
--
Trent Curry
perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'
------------------------------
Date: Wed, 17 Dec 2003 14:12:52 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: Login to site with random image code?
Message-Id: <EvZDb.735843$pl3.83706@pd7tw3no>
Trent Curry wrote:
> Darin McBride wrote:
>> Lucas Van Hieng wrote:
>>
>> I suppose the point is that if they're attempting to block scripts for
>> a reason, then anything your script can do, the scripts they're
>> attempting to block can do as well. Thus, if you find a way to OCR
>> the image, they'll simply change to an image type that you can't OCR.
>
> Well keep in mind that the image format needs to be displayable by any
> (visual) web browser, so that really limits the types (png, jpg, and gif
> mainly.) My point is thers only so manay ways they go in that respect.
Not quite what I meant. I meant that they could change from using a
font that was easy to OCR (e.g., an image that looks typed), to a
"font" that, perhaps, colour-blind people may not be able to discern,
(I use "font" very loosely here), or perhaps to a wavy pattern that
vaguely looks like words. Depending on how good your OCR software is,
you may or may not be able to programmatically recognise the text.
>> That's not to say it's no fun trying... :->
>
> True :)
------------------------------
Date: Thu, 18 Dec 2003 14:01:38 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Q about a module containing more than one class
Message-Id: <d583uvomvch5r79qlnpouoh5p3p8o7qfp6@4ax.com>
I apologize in advance if this is a bogus question. I'm just starting
to understand Perl's OO programming.
I have a class that relies on other classes (as suggested in many
docs) that are to be used *exclusively* for this, i.e. not as
standalone classes.
Now I want to put all these classes in the same (separate) module so
that only the "main" one is made (explicitly) available to the final
user, and my question is wether there's any convention/direction for
assigning a name to the "other" classes:
# $SOMEWHERE_IN_LIB/cool/module.pm
package foo;
# or would it be better to call it 'cool::module::foo'?
...
package cool::module;
sub new {
my $class = shift;
...
bless {_this => [...],
_foo => foo->new(...) }, $class;
}
...
1;
__END__
TIA,
Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
------------------------------
Date: Wed, 17 Dec 2003 13:49:12 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: reading from an url
Message-Id: <20031217084912.1c2209e4.jwillmore@remove.adelphia.net>
On 16 Dec 2003 23:36:10 -0800
raj1_iit@yahoo.com (Rajesh) wrote:
> Hi,
>
> I checked the archives and faq in Perl. ostly from the archives, I
> got to put up the following code to read from an url. But, if i run
> this code i get the error,
> 500: Can't connect to www.yahoo.com:80 ((Bad hostname
> www.yahoo.com)
Looks like (and I could be wrong) your DNS lookup failed. Possible
causes (on *NIX) is your resolv.conf file is fould or you have no
route to the host (firewall maybe?). No sure what could be going on
if your using WinXP (as stated in the sniped portion of your post). I
think netstat (at the command prompt) will give you routing
information (WinXP is trying to "play nice" with *NIX systems).
All in all, this doesn't appear to be a Perl issue, but an issue with
your system. Check your settings, try connecting with a browser, and
then see if your script works. If all else fails, change the URL in
your script to see if you can connect anywhere at all.
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
Mandrell: "You know what I think?" Doctor: "Ah, ah that's a
catch question. With a brain your size you don't think,
right?" -- Dr. Who
------------------------------
Date: 17 Dec 2003 11:29:17 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: recursive closures?
Message-Id: <brpemd$e0s$1@mamenchi.zrz.TU-Berlin.DE>
Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
[...]
> Of course if you want $sub to be a variable that is forcably undefined
> implicitly at the end of a scope scope then Perl has just such a thing
> - the local() variable!
>
> Now, of course, it is an annoying miss-feature that local() can't be
> applied to lexical variables so if you want to use local() you are for
> forced to use package variables. ...
(Just a side note)
That's not entirely true. Vexingly, you can use anonymous parts of
lexical aggregates just fine with local:
my @a = ( 123);
{
local $a[ 0] = 456;
print $a[ 0]; # 456
}
print $a[ 0]; # 123
I agree that the inability to use local() with lexicals is sometimes
annoying. It wouldn't be good style to use it all the time, but
occasionally the dynamic restoration behavior of local() is just what
the doctor ordered.
Anno
------------------------------
Date: 17 Dec 2003 12:22:19 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: recursive closures?
Message-Id: <brphpr$28l$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Anno Siegel:
> Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
>
> [...]
>
>> Of course if you want $sub to be a variable that is forcably undefined
>> implicitly at the end of a scope scope then Perl has just such a thing
>> - the local() variable!
>>
>> Now, of course, it is an annoying miss-feature that local() can't be
>> applied to lexical variables so if you want to use local() you are for
>> forced to use package variables. ...
>
> (Just a side note)
>
> That's not entirely true. Vexingly, you can use anonymous parts of
> lexical aggregates just fine with local:
>
> my @a = ( 123);
> {
> local $a[ 0] = 456;
> print $a[ 0]; # 456
> }
> print $a[ 0]; # 123
>
> I agree that the inability to use local() with lexicals is sometimes
> annoying. It wouldn't be good style to use it all the time, but
> occasionally the dynamic restoration behavior of local() is just what
> the doctor ordered.
Actually there is a nice solution available for the given problem. our()
has very similar scoping rules to my(). As long as the self-referential
closure isn't meant to span over several packages, it should work as
desired because now we can in fact localize.
I am quite amazed that I did eventually find a situation where our()
isn't totally useless.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 17 Dec 2003 13:14:26 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: recursive closures?
Message-Id: <u9oeu7lh71.fsf@wcl-l.bham.ac.uk>
Uri Guttman <uri@stemsystems.com> writes:
> >>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
>
> > Uri Guttman <uri@stemsystems.com> writes:
> >> >>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
> >>
> >> > Uri Guttman <uri@stemsystems.com> writes:
> >> >> my $sub ;
> >> >> $sub = sub{ blah; $sub->() }
> >>
> >> > That leaks.
> >> it would only leak if you let it fall out of scope without destroying it
> >> (but how?).
>
> > "You only need weak references if you don't want use some other way to
> > take care of tracking and breaking the circular dependancy on every
> > possible exit path" is true of anything that uses weak references.
>
> > but someone pointed out that it would leak without weakening one of
> > its refs (it is circular of course).
>
> well, damian posted this back to me. he refutes the need for the weak
> ref or an explicit breaking of the circular refs. i haven't tried this
> but i trust him to have.
>
> ------------------------
>
> They're not correct.
Who is "they" here? Damian or "someone".
> At least, not under 5.8.0 or later (and probably earlier too). And
> you can verify that by blessing the closure and seeing it die:
The code you cite demonstrates that destruction will take place if you
_do_ explicitly break the cirular ref.
> package Bang;
>
> sub DESTROY { print "Bang!\n" }
package main;
> # Test...
>
> my $fib;
> $fib = bless sub {
> my ($n) = @_;
> return 1 if $n < 2;
> return $fib->($n-1) + $fib->($n-2);
> }, 'Bang';
>
#############
> print $fib->(7), "\n";
>
> $fib = 7; # Should "Bang!" here
Yes indeed because here you are explicity breaking the circular
reference. If you just let $fib fall out of scope without explicitly
breaking the circular reference first then the closure would not be
destroyed (until the global destrustion phase).
Using assignment to break the circular reference like this is very
much a second-class approach. Firstly on aesthetic/maintainability
grounds: you really want whatever you have do to prevent the leak to
be lexically close to the declaration of $fib. Secondly on reliablity
grounds: in the general case you have to ensure that all exit paths
(last, next, die, return) from the code marked ############# above
pass though the statement where the circular reference is explicitly
broken.
In practice this mean that if you want to avoid weak references or
local() then you have to put the assignment in an AtExit object (or
something with equivalent semantics).
> the key to this idea in general is that you have to declare the var
> (with my or our) before you refer to it inside the closure code.
I think you are mixing issues here. That is necessary for a
completely different reason. The scope of Perl variable declarations
starts at the next statement.
> that was what i was telling the OP and damian just showed a running
> example with normal destruction and non-leaking semantics.
Where can I see this example?
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 17 Dec 2003 14:42:03 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: recursive closures?
Message-Id: <x78ylbjykk.fsf@mail.sysarch.com>
>>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
>> ------------------------
>>
>> They're not correct.
> Who is "they" here? Damian or "someone".
they being those (you?) who claim that recursive closures will leak.
>> At least, not under 5.8.0 or later (and probably earlier too). And
>> you can verify that by blessing the closure and seeing it die:
> The code you cite demonstrates that destruction will take place if you
> _do_ explicitly break the cirular ref.
that is no difference from $fib going out of scope.
i just changed the code to do just that:
{
my $fib;
$fib = bless sub {
my ($n) = @_;
return 1 if $n < 2;
return $fib->($n-1) + $fib->($n-2);
}, 'Bang';
# Test...
print $fib->(7), "\n";
}
package Bang;
sub DESTROY { print "Bang!\n" }
it prints:
21
Bang!
so it sure looks like it got destroyed upon exiting scope. what more do
you want? i see no leak there
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 17 Dec 2003 15:28:26 +0000 (UTC)
From: Lukas Mai <lmai@mytum.de>
Subject: Re: recursive closures?
Message-Id: <brpsmq$5sq$2@sunsystem5.informatik.tu-muenchen.de>
Uri Guttman <uri@stemsystems.com> wrote:
> i just changed the code to do just that:
> {
> my $fib;
> $fib = bless sub {
> my ($n) = @_;
> return 1 if $n < 2;
> return $fib->($n-1) + $fib->($n-2);
> }, 'Bang';
> # Test...
> print $fib->(7), "\n";
> }
# add this:
print "still running...\n";
END {print "global destruction:\n";}
> package Bang;
> sub DESTROY { print "Bang!\n" }
> it prints:
> 21
still running...
global destruction:
> Bang!
> so it sure looks like it got destroyed upon exiting scope. what more do
> you want? i see no leak there
It's destroyed during global destruction, not upon exiting scope.
HTH, Lukas
------------------------------
Date: Wed, 17 Dec 2003 15:37:41 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: recursive closures?
Message-Id: <x7vfofihfe.fsf@mail.sysarch.com>
>>>>> "LM" == Lukas Mai <lmai@mytum.de> writes:
> # add this:
> print "still running...\n";
> END {print "global destruction:\n";}
>> package Bang;
>> sub DESTROY { print "Bang!\n" }
>> it prints:
>> 21
> still running...
> global destruction:
>> Bang!
> It's destroyed during global destruction, not upon exiting scope.
interesting. and if you do assign something else to $fib DESTROY is
called immediately. anyhow, in stem i have tons of circular refs
(objects inside callbacks to those object) and i use explicit loop
breaking in all the right places. i just don't like the feeling of using
weak refs.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 17 Dec 2003 16:41:58 +0200
From: user@domain.invalid
Subject: Redirect url
Message-Id: <brppvm$ksp$1@ctb-nnrp2.saix.net>
I keep getting "The document has moved here" when trying to redirect
a browser using mod_perl (2) and apache 2. Is there a way around this?
------------------------------
Date: Wed, 17 Dec 2003 15:51:39 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Redirect url
Message-Id: <brpqs0$61fu7$1@ID-184292.news.uni-berlin.de>
user@domain.invalid wrote:
> I keep getting "The document has moved here" when trying to
> redirect a browser using mod_perl (2) and apache 2. Is there a way
> around this?
There is at least a way around making a fool of yourself: Don't post
such a question without showing relevant code!
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 17 Dec 2003 14:00:38 -0000
From: "Eric" <Eric@nowhere.com>
Subject: RegExp check for nothing or pattern
Message-Id: <brpnht$kt1$1@newsg1.svr.pol.co.uk>
Hi,
I want to write a regexp that matches either a blank value or a pattern. I'm
checking user data for a 'telephone' field which I want to allow to be blank
OR have at least 11 digits.
I've tried
if ($data =~ m/{0}|\d{11,}/) { etc etc
But it doesn't work
OK I know I can do it with some code, but my system reads regexps from a
config file and I'd like to keep my system versatile.
Cheers,
Eric
------------------------------
Date: Wed, 17 Dec 2003 15:17:14 +0100
From: Carsten Aulbert <carsten@welcomes-you.com>
Subject: Re: RegExp check for nothing or pattern
Message-Id: <brpoht$5vigo$1@ID-213226.news.uni-berlin.de>
Hi Eric,
Eric wrote:
> if ($data =~ m/{0}|\d{11,}/) { etc etc
>
Try this
/^\s*(|\d{11,})\s*$/
Grouping in perl is done with parentheses. I also added extra whitespaces
which might be around your number (or your "nothing")
If you want to use 0 as well, try
/^\s*(|0|\d{11,})\s*$/
And of course, read more under 'man perlre'
HTH
Carsten
PS: Have you asked the owner of nowhere.com if you are alowed to use their
domain name for email? Just asking, because it looks like you use it as a
pseudo anti-spam address.
------------------------------
Date: Wed, 17 Dec 2003 15:30:16 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: RegExp check for nothing or pattern
Message-Id: <brppjm$60gbv$1@ID-184292.news.uni-berlin.de>
Eric wrote:
> I want to write a regexp that matches either a blank value or a
> pattern. I'm checking user data for a 'telephone' field which I
> want to allow to be blank OR have at least 11 digits.
>
> I've tried
>
> if ($data =~ m/{0}|\d{11,}/) { etc etc
/^(?:|\d{11,})$/
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 17 Dec 2003 14:30:05 -0000
From: "Eric" <Eric@nowhere.com>
Subject: RegExp to match pattern or BLANK?
Message-Id: <brpp92$144$1@newsg4.svr.pol.co.uk>
Hi,
I want to write a regexp that matches either a blank value or a pattern. I'm
checking user data for a 'telephone' field which I want to allow to be blank
OR have at least 11 digits.
I've tried
if ($data =~ m/{0}|\d{11,}/) { etc etc
But it doesn't work
OK I know I can do it with some code, but my system reads regexps from a
config file and I'd like to keep my system versatile.
Cheers,
Eric
passme
------------------------------
Date: 17 Dec 2003 14:35:56 GMT
From: Michael Zawrotny <zawrotny@jaguar.sb.fsu.edu>
Subject: Re: RegExp to match pattern or BLANK?
Message-Id: <slrnbu0qec.b2n.zawrotny@jaguar.sb.fsu.edu>
On Wed, 17 Dec 2003 14:30:05 -0000, Eric <Eric@nowhere.com> wrote:
> I want to write a regexp that matches either a blank value or a pattern. I'm
> checking user data for a 'telephone' field which I want to allow to be blank
> OR have at least 11 digits.
>
> I've tried
>
> if ($data =~ m/{0}|\d{11,}/) { etc etc
Try
if($data =~ /^(\d{11,})?$/) { ...
Mike
--
Michael Zawrotny
Institute of Molecular Biophysics
Florida State University | email: zawrotny@sb.fsu.edu
Tallahassee, FL 32306-4380 | phone: (850) 644-0069
------------------------------
Date: Thu, 18 Dec 2003 14:01:37 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Shrikage: regexp rookie
Message-Id: <9483uv45hh7btrlfs6t99n8jrg7nol9mfg@4ax.com>
On Tue, 16 Dec 2003 00:23:59 GMT, Don Stefani <groups@donstefani.com>
wrote:
>I'd like to know if/how I can shorten this regexp, so it's sexier. ;-)
[snip]
>My regexp:
>$ary2[2] =~ s/(Color:|Size:)//g;
>$ary2[2] =~ s/(\<br\>)/,/g;
>$ary2[2] =~ s/(^\s+)//;
Others have suggested some WTDI in one regex. To me it is sexier if it
is more simple and I find it easier to understand and possibly
maintain if three regex are used as you did. Only, do yourself a favor
and do not use three =~; take an advantage of foreach aliasing
instead: it is even in the faq IIRC! Try this (untested),
s/(?:Color|Size)://g, s/<br>/,/g, s/^\s*// for $ary2[2];
HTH,
Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
------------------------------
Date: Wed, 17 Dec 2003 07:28:55 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Simple text file operation
Message-Id: <slrnbu0mgn.g91.tadmc@magna.augustmail.com>
logic1 <logic1@esatclear.ie> wrote:
> Guys I have a bit of an issue.
If you state what your issue is, we have a much better chance
of being able to help you overcome it.
It is easiest for us to help you if you have question marks
at the end of one or more sentences.
What parts do you already have working?
What parts are you having trouble with?
> I need to alter these text files
Have you seen the Perl FAQ about changing the contents of files?
perldoc -q file
How do I change one line in a file/delete a line in a
file/insert a line in the middle of a file/append to the
beginning of a file?
> Any help appreciated.
Ask a question and we will (likely) respond with an answer.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 18 Dec 2003 14:01:37 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: ticks and FreeBSD
Message-Id: <0qe1uvovpj6g54kbsr2q8bj2k905kfmfvu@4ax.com>
On Mon, 15 Dec 2003 17:38:49 GMT, Erik Tank <jundy@jundy.com> wrote:
>I just migrated a program from RedHat 9 to FreeBSD 5.1. Everything is
>working well with the exception of the following:
> my @jmail_return_lines = `ps -aux | grep 'jmaild' | grep -v grep`;
Not an answer to your question, but since nobody has complained yet, I
feel like pointing out that while the above approach is perfectly
"legal", indeed it looks rather awkward too. Why not let Perl use its
own tools to extract the lines you need?
See this (untested) for example:
chomp(my @jmail_return_lines = grep /jmaild/, qx{ps -aux});
^^^^^ ^^^ ^
(1) (2)
(1) bonus chomp(), because you may want it anyway: if not so, then
throw it away,
(2) it's just me, but for some reason I can't stand the backticks
chars :-)
Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
------------------------------
Date: Wed, 17 Dec 2003 07:51:23 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: ticks and FreeBSD
Message-Id: <slrnbu0nqr.g91.tadmc@magna.augustmail.com>
Michele Dondi <bik.mido@tiscalinet.it> wrote:
> chomp(my @jmail_return_lines = grep /jmaild/, qx{ps -aux});
> ^^^^^ ^^^ ^
> (1) (2)
> (2) it's just me,
No it isn't. :-)
> but for some reason
Mine is:
There's about 2 pixels difference between "slants" of
single quotes and it makes me squint (ie. slows down
my reading of the code).
> I can't stand the backticks
> chars :-)
Me either.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 17 Dec 2003 14:46:42 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Where do -T warnings go?
Message-Id: <20031217094641.6a6b6f27.jwillmore@remove.adelphia.net>
On Wed, 17 Dec 2003 11:30:15 +0100
Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com> wrote:
> I ran into this resentful qx($cmd) problem on 5.8.0 in tainted
> mode. It took a very long time before I got to figure out it was
> a taint issue because I couldn't find the taint warnings back:
> not in $?, $!, $@, webserver error log or syslog. Is taint a
> special case here?
When running a script in taint mode, you need to do such things as set
your PATH and escape metacharacters. This is documented in
perldoc perlsec
and
http://www.w3.org/Security/Faq/www-security-faq.html
As far as getting errors about insecure dependencies, you *should* get
them like any other warnings and errors. Best bet is to test your
script at the command line first to insure *most* possible issues are
taken care of before running the script through the web server.
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
Hofstadter's Law: It always takes longer than you expect, even
when you take Hofstadter's Law into account.
------------------------------
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 5948
***************************************