[22264] in Perl-Users-Digest
Perl-Users Digest, Issue: 4485 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 30 00:06:10 2003
Date: Wed, 29 Jan 2003 21:05:08 -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, 29 Jan 2003 Volume: 10 Number: 4485
Today's topics:
Re: alarm() implemented? (Sisyphus)
Re: array slicing <thens@nospam.com>
Re: Email address RE <mbear@uq.net.au>
Re: hash ordering. <dha@panix2.panix.com>
Re: hash ordering. (Tad McClellan)
Re: hash ordering. <uri@stemsystems.com>
Re: how to print error messages to html (Ben Morrow)
Re: how to print error messages to html (Tad McClellan)
Re: how to print error messages to html (Bryan Castillo)
Re: Mailing List CGI <nobody@dev.null>
Newbie questions (Kwan)
Re: Newbie questions (Tad McClellan)
Re: Newbie questions <REMOVEsdnCAPS@comcast.net>
Re: Newbie questions <krahnj@acm.org>
Re: Next problem. <jurgenex@hotmail.com>
Re: Next problem. <REMOVEsdnCAPS@comcast.net>
Re: OO problem <rajkothary@hotmail.com>
Re: OO problem (Tad McClellan)
Possible flock/POSIX::close/__END__ bug in 5.8.0 <cees@sitesuite.net>
Re: Possible flock/POSIX::close/__END__ bug in 5.8.0 <goldbb2@earthlink.net>
Premature end of script headers errors (Chumley the Walrus)
Re: Premature end of script headers errors <jurgenex@hotmail.com>
Re: String to float casting? (Tad McClellan)
Re: wondering why this doesn't work <swen@news.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 29 Jan 2003 18:51:34 -0800
From: kalinabears@hdc.com.au (Sisyphus)
Subject: Re: alarm() implemented?
Message-Id: <e615828f.0301291851.3028d8f8@posting.google.com>
spark <spark@dcwis.nodurnspam.com> wrote in message news:<MPG.18a1bbf086a3da8a989695@news-central.giganews.com>...
> Is alarm() implemented for Win32 in the latest version of Perl?
Seems to be impemented to some degree, at least.
With 5.8:
perl -e "alarm(2);for(;;){}"
That will produce (after a couple of seconds) the message:
Terminating on signal SIGALRM(14)
Then exits immediately.
With 5.6.1
perl -e "alarm(2);for(;;){}"
That will exit immediately with the message:
The Unsupported function alarm function is unimplemented at -e line 1.
Just how far the support extends, I don't know :-)
Cheers,
Rob
------------------------------
Date: Thu, 30 Jan 2003 09:53:19 +0530
From: Thens <thens@nospam.com>
Subject: Re: array slicing
Message-Id: <20030130095319.3c0e7775.thens@nospam.com>
On 29 Jan 2003 17:26:17 GMT
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote in comp.lang.perl.misc:
> > Bart Lateur <bart.lateur@pandora.be> wrote in comp.lang.perl.misc:
> > > Anno Siegel wrote:
> > >
> > > >print "@{[grep { $_ eq 'THREE' .. !defined } @numbers]}\n";
> > >
> > > I don't think the range operator will flop back to false at the end of
> > > the list.
> > >
> > > my @numbers = ( 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE' );
> > > for(\@numbers, [qw(A B C)]) {
> > > print "@{[grep { $_ eq 'THREE' .. !defined } @$_]}\n";
> > > }
> > > -->
> > > THREE FOUR FIVE
> > > A B C
> > >
> > > Like I said... :-)
> >
> > Right, another reason not to use this joke in earnest.
> >
> > "!defined" is not an attempt to reset the operator. I would have
> > used "0" or "''" instead of "!defined" if the range operator didn't
> > insist in comparing a constant against $. I wanted a non-constant
> > expression that returns false, and nothing simpler came to mind. In
> > fact, it returns false only most of the time :)
>
> I guess "do { 0 }" is what I was looking for.
>
> print "@{[grep { $_ eq 'THREE' .. do { 0 } } @$_]}\n";
>
Can you explain how this works or can you point me to any section of
perldoc that will explain this??
> It leaves the ".." operator hanging, as it were, but that's the least
> problem of this obfuscated bit of code.
>
> Anno
------------------------------
Date: Thu, 30 Jan 2003 13:37:22 +1000
From: Matthew Braid <mbear@uq.net.au>
Subject: Re: Email address RE
Message-Id: <b1a6li$vqt$1@bunyip.cc.uq.edu.au>
news@roaima.freeserve.co.uk wrote:
>Matthew Braid <mbear@uq.net.au> wrote:
>
>
>>In fact, try to find a single word in the last 2
>>sentences that _isn't_ a 'perfectly valid email address', other than
>>"doesn't" and "you're"o
>>
>>
>
>And if you include the double quotes, then those are potentially perfectly
>valid email addresses, too.
>
>Chris
>
>
D'oh! :)
------------------------------
Date: Wed, 29 Jan 2003 23:45:01 +0000 (UTC)
From: "David H. Adler" <dha@panix2.panix.com>
Subject: Re: hash ordering.
Message-Id: <slrnb3gprt.su8.dha@panix2.panix.com>
In article <SuSZ9.68$cu4.38@nwrddc02.gnilink.net>, Jürgen Exner wrote:
> Richard S Beckett wrote:
>> Is there any way to force a hash to be in the same order as you
>> defined it?
>
> Hashes are mappings. They do not have an order. Asking for the order of a
> hash is like asking for the weight of the color green.
Strictly speaking, they *do* have an order. It's just not a *useful*
order. :-)
dha
--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
In the silence that followed, Homer was heard to mutter,
"Mmmm... context- dependent semantics..."
- Darrin Edwards in c.l.p.m.
------------------------------
Date: Wed, 29 Jan 2003 18:27:43 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: hash ordering.
Message-Id: <slrnb3gsbv.d0v.tadmc@magna.augustmail.com>
David H. Adler <dha@panix2.panix.com> wrote:
> In article <SuSZ9.68$cu4.38@nwrddc02.gnilink.net>, Jürgen Exner wrote:
>> Richard S Beckett wrote:
>>> Is there any way to force a hash to be in the same order as you
>>> defined it?
>>
>> Hashes are mappings. They do not have an order. Asking for the order of a
>> hash is like asking for the weight of the color green.
>
> Strictly speaking, they *do* have an order. It's just not a *useful*
> order. :-)
It _is_ a useful order. It is what allows O(1) lookups to happen. :-)
The order of a hash is useful for a perl programmer,
but not for a Perl programmer.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 30 Jan 2003 02:57:21 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: hash ordering.
Message-Id: <x7ptqf4a5r.fsf@mail.sysarch.com>
>>>>> "DHA" == David H Adler <dha@panix2.panix.com> writes:
DHA> In article <SuSZ9.68$cu4.38@nwrddc02.gnilink.net>, Jürgen Exner
DHA> wrote:
>> Richard S Beckett wrote:
>>> Is there any way to force a hash to be in the same order as you
>>> defined it?
>> Hashes are mappings. They do not have an order. Asking for the
>> order of a hash is like asking for the weight of the color green.
DHA> Strictly speaking, they *do* have an order. It's just not a
DHA> *useful* order. :-)
but the order does matter if you use keys and values together. the
critical point is that at a given point in time, a hash's key order is
fixed and repeatable. so you can do things like this:
@new_hash{ keys %old_hash } = values %old_hash ;
we don't care about the order of the keys in %old_hash but it had better
not change during that statement.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Wed, 29 Jan 2003 23:14:56 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: how to print error messages to html
Message-Id: <b19n9g$m1n$1@wisteria.csv.warwick.ac.uk>
csejl@yahoo.com (James Lee) wrote:
>Hi,
>
>I'd appreciate it if someone could clarify this. I have a script that
>runs off of cgi-bin on my web server. There are two places in this
>snippet that could print out $dbh->errstr. How would I get this error
>message to be printed to the web page. Right now, when it fails it
>just displays a blank page. The whole thing below is inside
><html><body> tags. Thanks.
use CGI::Carp qw/fatalsToBrowser/;
Ben
------------------------------
Date: Wed, 29 Jan 2003 18:35:27 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: how to print error messages to html
Message-Id: <slrnb3gsqf.d0v.tadmc@magna.augustmail.com>
James Lee <csejl@yahoo.com> wrote:
> I'd appreciate it if someone could clarify this.
We would appreciate it if you would check the Perl FAQ before
posting to the Perl newsgroup.
> I have a script that
> runs off of cgi-bin
> How would I get this error
^^^^^
> message to be printed to the web page.
perldoc -q error
"How can I get better error messages from a CGI program?"
Read the answer all the way down to the end, where it answers
your question.
> if (($st =~ /^create/i) || ($st =~ /^insert/i) || ($st =~
> /^drop/i)) {
This does the same thing:
if ( $st =~ /^(create|insert|drop)/i ) {
This does the same thing and eases the maintainer's eye strain:
if ( $st =~ /^( create | insert | drop )/ix ) {
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 29 Jan 2003 19:19:53 -0800
From: rook_5150@yahoo.com (Bryan Castillo)
Subject: Re: how to print error messages to html
Message-Id: <1bff1830.0301291919.2ef34433@posting.google.com>
csejl@yahoo.com (James Lee) wrote in message news:<146277ca.0301291418.289aa799@posting.google.com>...
> Hi,
>
> I'd appreciate it if someone could clarify this. I have a script that
> runs off of cgi-bin on my web server. There are two places in this
> snippet that could print out $dbh->errstr. How would I get this error
> message to be printed to the web page. Right now, when it fails it
> just displays a blank page. The whole thing below is inside
> <html><body> tags. Thanks.
>
> -j.
# You can wrap your code in an eval block
# If you do this, you might also want to use the RaiseError
# attribute for DBI->connect.
#
# DBI->connect($datasource, $username, $password,
# {RaiseError=>1, AutoCommit=>1});
#
# (You should probably be specifying the value for AutoCommit explicity.)
eval {
>
> $dbh = DBI->connect($datasource, $username, $password)
> or die "Can't connect to $datasource: $dbh->errstr\n";
>
> @pieces = split(/ /, $st);
> $tablename = $pieces[2];
>
> if ($iteration > 0) {
> for ($i=0; $i<$iteration; $i++) {
> $dbh->do($st)
> || die "Couldn't execute statement: " . $dbh->errstr . "\n";
> # if it's 'select' we don't need to modify anything
> if (($st =~ /^create/i) || ($st =~ /^insert/i) || ($st =~
> /^drop/i)) {
> ($name, $number) = $tablename =~
> /^([a-zA-Z_]+)(\d+)$/;
> $number++;
> $pieces[2] = $tablename = $name.$number;
> $st = join (" ", @pieces);
> }
> }
> } else {
> $now = time;
> more code here...
};
if ($@) { # catch errors
print <<EOF;
<pre>
<font color="red">
$@
</font>
</pre>
EOF
}
------------------------------
Date: Thu, 30 Jan 2003 04:18:19 GMT
From: Andras Malatinszky <nobody@dev.null>
Subject: Re: Mailing List CGI
Message-Id: <3E38A798.4080507@dev.null>
Joel Konkle-Parker wrote:
> I have a web site hosted by Hypermart, and I want to set up a mailing
> list. Can anyone recommend a package for doing that sort of thing with
> no machine access apart from my own webroot directory? I do have
> Perl/CGI and sendmail access.
>
> - Joel
>
How 'bout the recommendations in perlfaq9, under the title "How do I send mail?"
If you have no place to type perldoc -q send mail at, try
http://www.perldoc.com/perl5.6/pod/perlfaq9.html
------------------------------
Date: 29 Jan 2003 18:45:06 -0800
From: ckkwan@my-deja.com (Kwan)
Subject: Newbie questions
Message-Id: <df57e84d.0301291845.5c326769@posting.google.com>
I am not even sure if this is perl script.
There is a text file:
----
test
! test
test
test
----
I would like to change the first and last line from "test" to "no
test". So I am searching into the text file, and search for "test"
that appear in the first column of a line. Did I made any miskates
below?
Thanks.
my ($bstr) = "";
my (@Lines) = split '\n';
my ($iter);
for $iter (@Lines)
{
if($iter =~ /(test.*$)/)
{
$bstr = $bstr . "no " . $1 . "\n";
}
}
------------------------------
Date: Wed, 29 Jan 2003 21:34:39 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Newbie questions
Message-Id: <slrnb3h7af.dcf.tadmc@magna.augustmail.com>
Kwan <ckkwan@my-deja.com> wrote:
> Subject: Newbie questions
Please put the subject of your article in the Subject of your article.
> So I am searching into the text file, and search for "test"
> that appear in the first column of a line.
------------------
#!/usr/bin/perl
use strict;
use warnings;
while ( <DATA> ) {
s/^test/no test/;
print;
}
__DATA__
test
! test
test
test
------------------
> Did I made any miskates
> below?
Yes. At least four of them.
> my ($bstr) = "";
> my (@Lines) = split '\n';
1) you haven't put anything in $_, so splitting it won't be very useful.
2) you are splitting on the two-character sequence: backslash, "n".
If you meant to split on a newline, then:
split /\n/;
3) you did not enable warnings. It would have pointed out mistake #1.
> my ($iter);
> for $iter (@Lines)
> {
> if($iter =~ /(test.*$)/)
The .* serves no purpose.
4) you did not anchor your pattern to the beginning of the string.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 29 Jan 2003 21:43:36 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Newbie questions
Message-Id: <Xns9312E72EDE07sdn.comcast@216.166.71.239>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
ckkwan@my-deja.com (Kwan) wrote in
news:df57e84d.0301291845.5c326769@posting.google.com:
> There is a text file:
> ----
> test
> ! test
> test
> test
> ----
>
> I would like to change the first and last line from "test" to "no
> test". So I am searching into the text file, and search for "test"
> that appear in the first column of a line. Did I made any miskates
> below?
Yes. :-)
> my ($bstr) = "";
> my (@Lines) = split '\n';
> my ($iter);
The parentheses in the above lines are completely unnecessary.
But they don't hurt anything.
> for $iter (@Lines)
> {
> if($iter =~ /(test.*$)/)
Here's your problem. The above pattern will find "test" anywhere on
the line you're looking at. All of the example lines you've shown
from your text input file will match this pattern.
You need to anchor the pattern at the front of the string with ^
if ($iter =~ /^(test.*$)/)
> {
> $bstr = $bstr . "no " . $1 . "\n";
The above works, but I think the following would be simpler:
$bstr .= "no $iter\n";
and then you could eliminate the wasteful ".*$" and parentheses from
your pattern match:
if ($iter =~ /^test/)
> }
> }
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
iD8DBQE+OJ9fY96i4h5M0egRAh8xAKCWnpnbnF9NN+Z1OmxqC8SACrGjDACg3K5D
czDwMbQnHpJ28VoGIwRIWo8=
=g2dv
-----END PGP SIGNATURE-----
------------------------------
Date: Thu, 30 Jan 2003 05:01:29 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Newbie questions
Message-Id: <3E38B1A7.62E4B379@acm.org>
Kwan wrote:
>
> I am not even sure if this is perl script.
>
> There is a text file:
> ----
> test
> ! test
> test
> test
> ----
>
> I would like to change the first and last line from "test" to "no
> test". So I am searching into the text file, and search for "test"
> that appear in the first column of a line. Did I made any miskates
> below?
>
> my ($bstr) = "";
> my (@Lines) = split '\n';
^^^^^^^^^^
It looks like the $_ variable contains the contents of the file?
> my ($iter);
> for $iter (@Lines)
> {
> if($iter =~ /(test.*$)/)
> {
> $bstr = $bstr . "no " . $1 . "\n";
> }
> }
If the complete contents of the file are in $_ then this will work (no
need for the split or for loop):
s/^(?=test)/no /gm;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 30 Jan 2003 03:37:37 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Next problem.
Message-Id: <561_9.2795$cu4.2136@nwrddc02.gnilink.net>
Ben Morrow wrote:
> "Jürgen Exner" <jurgenex@hotmail.com> wrote:
>> Tad McClellan wrote:
>>> Jürgen Exner <jurgenex@hotmail.com> wrote:
>>>> Richard S Beckett wrote:
>>>
>>>>> next if ($_ eq ('Fred' || 'Wilma'));
>>>
>>>> BTW: I think if you would have used warnings then perl would have
>>>> warned you about his blunder.
>>>
>>> Nope, warnings won't catch that one.
>>
>> Hmm, I wonder if they should.
>> After all, if I try to e.g. add two strings, then they will warn me
>> that probably I'm doing something stupid. Why not warn the user if
>> he tries to apply a boolean operation (instead of numerical
>> operation) to text strings, too?
>> Considering that this mistake pops up about every month it may be
>> worth thinking about a change in warnings.
>
> But then
> my $var = shift || $ENV{PARAM} || 'default';
> wouldn't work.
I see your point. Seems || has a dual nature. It can be used as a boolean
operator. And it can be used as a kind of selector for text.
Maybe that's the root of the problem and we need a different || for
selecting text? Just like you got == for numbers and eq for text?
jue
------------------------------
Date: Wed, 29 Jan 2003 22:45:48 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Next problem.
Message-Id: <Xns9312F1BAB4B26sdn.comcast@216.166.71.239>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
"Jürgen Exner" <jurgenex@hotmail.com> wrote in
news:561_9.2795$cu4.2136@nwrddc02.gnilink.net:
> I see your point. Seems || has a dual nature. It can be used as a
boolean
> operator. And it can be used as a kind of selector for text.
> Maybe that's the root of the problem and we need a different ||
for
> selecting text? Just like you got == for numbers and eq for text?
This is leading towards Perl 6 Syndrome, where everything covered in
your basic undergraduate Data Structures course and your basic
undergraduate Algorithms course must be given its own operator. :-)
"The problem", in your paragraph above, is that someone didn't
understand precedence. I suspect the solution is not to add new
operators to the precedence list!
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
iD8DBQE+OK3wY96i4h5M0egRAg7ZAJ9rutUOZpsz7lM0fMLUWLReMkrzFACdEKEM
9Ozo44aeOq6qAjFzh4YCa2s=
=o5Ml
-----END PGP SIGNATURE-----
------------------------------
Date: Thu, 30 Jan 2003 02:52:00 -0000
From: "Raj" <rajkothary@hotmail.com>
Subject: Re: OO problem
Message-Id: <b1a40h$8k0$1$8302bc10@news.demon.co.uk>
"Brian McCauley" <nobull@mail.com> wrote in message
news:u91y2vvi5o.fsf@wcl-l.bham.ac.uk...
> "Raj" <rajkothary@hotmail.com> writes:
>
> Probably but there's no way to tell. To quote the posting guidelines...
>
> First make a short (less than 20-30 lines) and *complete* program
> that illustrates the problem you are having. People should be able
> to run your program by copy/pasting the code from your article.
(You
> will find that doing this step very often reveals your problem
> directly. Leading to an answer much more quickly and reliably than
> posting to Usenet.)
>
> I suggest you go read the whole of the guidelines - they are very good.
Hi,
Ok, I'm sorry I didn't read those. I will try to as soon as I get the
chance, but I was hoping to get some help because I'm desperate! Thanks for
your comments, though, I will take them on board for next time.
In the mean time, could anyone suggest a regular expression to convert:
22/03/2002 00:00:00
to
20020322T00:00:00
? (if that's not too cheeky considering my last post!)
Thanks,
Raj
------------------------------
Date: Wed, 29 Jan 2003 21:27:05 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: OO problem
Message-Id: <slrnb3h6s9.dcf.tadmc@magna.augustmail.com>
Raj <rajkothary@hotmail.com> wrote:
> could anyone suggest a regular expression to convert:
>
> 22/03/2002 00:00:00
>
> to
>
> 20020322T00:00:00
s#(\d+)/(\d+)/(\d+) #$3$2$1T#;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 29 Jan 2003 21:57:19 -0500
From: Cees Hek <cees@sitesuite.net>
Subject: Possible flock/POSIX::close/__END__ bug in 5.8.0
Message-Id: <hA0_9.55924$L47.8485819@read2.cgocable.net>
I just want to see if someone can confirm what I am seeing.
I am trying to use the Proc::Daemon module and the Proc::PID::File module
together and am running into what I think is a bug in perl 5.8.0. I can't
reproduce the problem in perl 5.6.1.
The following code reproduces the problem:
------------------
#!/usr/bin/perl
use Proc::Daemon;
use Proc::PID::File;
Proc::Daemon::Init; # Fork and disassociate from parent
my $pf = new Proc::PID::File(dir => '/tmp');
die "Already running!" if $pf->alive(); # Create PIDfile
__END__
------------------
(Note that the __END__ is important to reproduce the bug)
The above code should fork and disassociate from the parent and then try to
create a pid file in /tmp and exit. If you run it and check your process
list you will see a stuck process (the child process). Do a strace -p of
the pid and you will see:
flock(4, LOCK_EX
ie it is waiting on a exclusive lock so it can write the pid file. The
program is effectively deadlocked.
I have reduced the code found in those modules that trigger the bug to the
following code which will hang on the second flock (according to strace):
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl qw(:DEFAULT :flock);
use POSIX;
POSIX::close(3);
{
sysopen FH, '/tmp/test.pl.pid', O_RDWR|O_CREAT
|| die "Cannot open pid file '/tmp/test.pl.pid': $!\n";
flock FH, LOCK_EX;
my ($pid) = <FH> =~ /^(\d+)/;
# flock FH, LOCK_UN; # Should be automatic on close FH
close FH;
}
{
sysopen FH, '/tmp/test.pl.pid', O_RDWR|O_CREAT
|| die "Cannot open pid file '/tmp/test.pl.pid': $!\n";
flock FH, LOCK_EX;
sysseek FH, 0, 0;
truncate FH, 0;
syswrite FH, "$$\n", length("$$\n");
close FH || die "Cannot write pid file '/tmp/test.pl.pid': $!\n";
}
__END__
The above code deadlocks on the second call to flock. But if you uncomment
the:
lock FH, LOCK_UN;
then everything works fine and the lock is released properly. From my
understanding, calling close on a filehandle should release the lock for
you automatically.
Removing the POSIX::close(3); call will stop the deadlock from happening,
but this is what the Proc::Daemon module uses to disassociate all
filehandles from the parent process.
There is one more 'very strange' observation. If you remove the __END__
from the code everything works OK. This makes absolutely no sense to me,
but that is what I am seeing.
So to summarize, if your code has an __END__ block and you use
POSIX::close() on a filehandle, and then open a new file with that
filehandle and flock the filehandle, then the flock will not automatically
be released when you close the filehandle. This will result in a deadlock
if you try to flock the file again later in the code.
Can someone confirm that this is a bug, or explain to me what I am doing
wrong... I am asking here first, because that's what the perl5-porters FAQ
says to do :)
Cheers
Cees Hek
Following are some system particulars:
I am using the debian version of perl and the package version is 5.8.0-15
perl -V gives the following:
Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
Platform:
osname=linux, osvers=2.4.19, archname=i386-linux-thread-multi
uname='linux cyberhq 2.4.19 #1 smp sun aug 4 11:30:45 pdt 2002 i686
unknown unknown gnulinux '
config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN
-Dcccdlflags=-fPIC -Darchname=i386-linux -Dprefix=/usr
-Dprivlib=/usr/share/perl/5.8.0 -Darchlib=/usr/lib/perl/5.8.0
-Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5
-Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local
-Dsitelib=/usr/local/share/perl/5.8.0 -Dsitearch=/usr/local/lib/perl/5.8.0
-Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dman1ext=1
-Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Uusesfio
-Uusenm -Duseshrplib -Dlibperl=libperl.so.5.8.0 -Dd_dosuid -des'
hint=recommended, useposix=true, d_sigaction=define
usethreads=define use5005threads=undef useithreads=define
usemultiplicity=define
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN
-fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O3',
cppflags='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing'
ccversion='', gccversion='3.2.2 20021231 (Debian prerelease)',
gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=8
alignbytes=4, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lgdbm -ldb -ldl -lm -lpthread -lc -lcrypt
perllibs=-ldl -lm -lpthread -lc -lcrypt
libc=/lib/libc-2.3.1.so, so=so, useshrplib=true,
libperl=libperl.so.5.8.0
gnulibc_version='2.3.1'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: MULTIPLICITY USE_ITHREADS USE_LARGE_FILES
PERL_IMPLICIT_CONTEXT
Built under linux
Compiled at Jan 11 2003 19:36:18
@INC:
/etc/perl
/usr/local/lib/perl/5.8.0
/usr/local/share/perl/5.8.0
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.8.0
/usr/share/perl/5.8.0
/usr/local/lib/site_perl
------------------------------
Date: Wed, 29 Jan 2003 23:14:41 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Possible flock/POSIX::close/__END__ bug in 5.8.0
Message-Id: <3E38A6B1.2A214ED0@earthlink.net>
Cees Hek wrote:
[snip]
> use strict;
> use warnings;
> use Fcntl qw(:DEFAULT :flock);
> use POSIX;
>
> POSIX::close(3);
This closes the DATA filehandle, which is created by having an __END__
or a __DATA__ token in your file.
>
> {
> sysopen FH, '/tmp/test.pl.pid', O_RDWR|O_CREAT
> || die "Cannot open pid file '/tmp/test.pl.pid': $!\n";
This opens FH, using the next available filedescriptor, which, since
you've closed #3, and 0, 1, 2 (STD{IN,OUT,ERR}) are still open, is 3,
the same fd as the DATA filehandle was at.
But since you didn't directly *close* the DATA filehandle, only the
underlying filedescriptor, perl thinks that the DATA handle is still
open, and that the FH and DATA handles are aliases to each other.
> flock FH, LOCK_EX;
> my ($pid) = <FH> =~ /^(\d+)/;
> # flock FH, LOCK_UN; # Should be automatic on close FH
> close FH;
Some versions of perl perform refcounting on filedescriptors, so that to
get the underlying fd to *really* close, then number of handles which
are open to it has to go to zero. I'd be willing to bet that a
'close(DATA)' at this point in the program will make the deadlock later
on go away.
[PS: This is all OTTOMH, so might be complete and utter BS. Does strace
show a close(3) occuring around here?]
> }
>
> {
> sysopen FH, '/tmp/test.pl.pid', O_RDWR|O_CREAT
> || die "Cannot open pid file '/tmp/test.pl.pid': $!\n";
Since filedescriptor 3 is still open (visible through perl's DATA
handle), this sysopen creates FH with filedescriptor 4.
> flock FH, LOCK_EX;
This tries to lock filedescriptor 4, while the lock is still held on 3.
The operating system isn't smart enough to know that both of these locks
are held by the same process.
[snip]
> Can someone confirm that this is a bug, or explain to me what I am
> doing wrong... I am asking here first, because that's what the
> perl5-porters FAQ says to do :)
I suppose that it's a bug in that perl can't distinguish between:
open( A, ">x" );
open( B, ">=&A" ); # fileno(A) == fileno(B)
close B; # shouldn't cause fileno(A) to get closed.
and:
open( A, ">x" );
POSIX::close(fileno(A));
# A still appears to be open, even though it's fd is closed.
open( B, ">x" );
# Now that the fd is reused, perl doesn't know that A ought to
# be considered closed. Oh, and fileno(B) == fileno(A)
close B; # should this cause fileno(A) to get closed?
If you try to fix the behavior of the latter situation, there's a good
chance that you'll break the behavior of the former situation.
--
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);
------------------------------
Date: 29 Jan 2003 19:47:05 -0800
From: springb2k@yahoo.com (Chumley the Walrus)
Subject: Premature end of script headers errors
Message-Id: <1ef65641.0301291947.1639ef18@posting.google.com>
I have all my files chmoded correctly, but I keep getting an internal
server error when trying to access my perl script/ page. In my error
logs, I get a:
Premature end of script headers
...which tells me little or nothing.
Any ideas? Thanks for help
------------------------------
Date: Thu, 30 Jan 2003 03:54:41 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Premature end of script headers errors
Message-Id: <5m1_9.27440$uR.5360@nwrddc01.gnilink.net>
Chumley the Walrus wrote:
> I have all my files chmoded correctly, but I keep getting an internal
> server error when trying to access my perl script/ page. In my error
> logs, I get a:
>
> Premature end of script headers
>
> ...which tells me little or nothing.
>
> Any ideas? Thanks for help
Yes. Please see "perldoc -q 500"
jue
------------------------------
Date: Wed, 29 Jan 2003 18:24:18 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: String to float casting?
Message-Id: <slrnb3gs5i.d0v.tadmc@magna.augustmail.com>
Gomac <gomac_the_1st@yahoo.co.uk> wrote:
> is it possible to cast a string to a float so I can do operations such
> as
>
> if($n>=0 && $n<=16)
You can do operations like that on strings, perl will automatically
do the Right Thing.
What happened when you tried it?
> I was just wondering if it was possible
> to cast to a float.
Why do you think that you need to cast to a float?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 29 Jan 2003 18:53:51 -0800
From: swen <swen@news.com>
Subject: Re: wondering why this doesn't work
Message-Id: <3E3893BF.9F587A46@news.com>
thanks for the responses.
swen wrote:
> file A.pm:
> ----------
> package A;
> $var = "I was set in A";
> 1;
>
> file B.pm:
> ----------
> package B;
> use A;
> $A::var = "I was set in B";
> 1;
>
> file C.pm:
> ----------
> package C;
> use A;
> require B;
> sub print {
> print "value was set to: ".$A::var;
> }
> 1;
>
> file run.pl:
> ----------
> use C;
> &C::print();
>
> running run.pl prints "value was set to: I was set in A". so when print
> is called it doesn't read the value of $A::var as modified in package B.
> why not?
------------------------------
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 4485
***************************************