[24981] in Perl-Users-Digest
Perl-Users Digest, Issue: 7231 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Oct 10 21:06:59 2004
Date: Sun, 10 Oct 2004 18:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 10 Oct 2004 Volume: 10 Number: 7231
Today's topics:
Re: C (I think) to Perl Conversion (Peter Scott)
Re: C (I think) to Perl Conversion <sammie-nospam@greatergreen.com>
Re: C (I think) to Perl Conversion (krakle)
Re: C (I think) to Perl Conversion <sammie-nospam@greatergreen.com>
Re: C (I think) to Perl Conversion <sammie-nospam@greatergreen.com>
Re: Is PHP still slower than Perl? <none@none.com>
Module Testing Question <aaa@sss.com>
Re: Module Testing Question <thepoet_nospam@arcor.de>
perl warnings <dmw@nospam.unete.cl>
Re: perl warnings <spamtrap@dot-app.org>
Re: possible mem leak ? <usenet@morrow.me.uk>
Re: To Tad: Great perl class, and question <ron.parker@povray.org>
Re: Top 10 list algorithm (Anno Siegel)
Re: Top 10 list algorithm <shawn.corey@sympatico.ca>
Using Perl from a PAM module (boot_DynaLoader undefined (Kyle Moffett)
Re: What does $base)->abs do? <tadmc@augustmail.com>
Re: While query <usenet@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 10 Oct 2004 14:11:04 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: C (I think) to Perl Conversion
Message-Id: <Ypbad.702696$gE.303583@pd7tw3no>
In article <l6CdnR_E38t7LfXcRVn-ow@comcast.com>,
"Brad Walton" <sammie-nospam@greatergreen.com> writes:
>I'm stuck here... I have a sample script which accomplishes the task I need
>done, but it's done in another programing language (of which I have no
>understanding). I have researched this for hours and hours, but no luck.
>
>The script I need 'deciphered' to Perl:
C developers call them 'programs' :-)
But you indicated you know Perl already, so you don't need
anyone to turn this program into Perl, you just need an
English translation so you can write the Perl. There are
issues such as byte ordering and length of an int that this
program glosses over.
>int main(int argc, char *argv[] )
>{
> int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
> if ( sock == -1 )
> {
> printf( "Socket creation failed" );
> exit(-1);
> }
>
> struct sockaddr_in rcon_server;
> rcon_server.sin_family = AF_INET;
> rcon_server.sin_addr.s_addr = inet_addr( "127.0.0.1" );
> rcon_server.sin_port = htons( 27015 );
>
> if ( connect( sock, (const struct sockaddr *)&rcon_server,sizeof(
>rcon_server ) )!= 0 )
> {
> printf( "Unable to connect\n");
> exit(-1);
> }
Connect to TCP port 27015 on the local host and die on error.
Suggest: IO::Socket.
> unsigned char send_buf[4096];
> unsigned char *send_ptr = send_buf + sizeof(int);
>
> *(int *)send_ptr = 0x0001; // request id 1
> send_ptr += sizeof(int);
> *(int *)send_ptr = 0x0003; // command id 3
> send_ptr += sizeof(int);
>
> strcpy( (char *)send_ptr, "password" ); // the rcon password
> send_ptr += strlen("password") +1; //+1 for null terminator
> *(int *)send_ptr = 0; // 2nd string is just null
> send_ptr++;
> (*(int *)send_buf) = (int)(send_ptr - send_buf - sizeof(int)); //
>now setup the size of this packet (NOTE the subtraction of sizeof(int)!!)
>
> printf( "Sending packet (%d bytes)\n", *(int *)(send_buf) );
>
> send( sock, send_buf, *(int *)(send_buf) + sizeof(int), 0 ); // send
>the auth request
Send a packet consisting of a 4-byte (assumption) count of the number
of bytes following, then 4 bytes containing a 1, 4 bytes containing a 3,
and the string "password" (which one assumes may get changed) and a null
byte. perldoc -f pack.
> sleep(1); // ugly hack to give the server time to respond
>
> long readLen;
> ioctl( sock, FIONREAD, &readLen );
> printf("Got %d bytes from server\n", readLen );
Read a response from the socket.
> int len = recv( sock, send_buf, readLen, 0);
> if ( len < 14 )
> {
> printf("Didn't read enough data (%i)( TODO: block on
>reads)\n", len);
> exit(-1);
> }
Which must be at least 14 bytes long.
> printf( "packet size: %d, request id:%d, command:%d\n",
>(int)send_buf[0],(int)send_buf[4],(int)send_buf[8]);
And starts with 3 integers of interest. perldoc -f unpack.
> if ( len > 14 ) // a 2nd packet is in the response
> {
> printf( "packet size: %d, request id:%d, command:%d\n",
>(int)send_buf[14],(int)send_buf[18],(int)send_buf[22]);
> }
If there are more than 14 bytes in the response, we are also
interested in the three integers starting at byte offset 14.
> close( sock );
>}
This program is not an interface; it's more of a debugging
print test. Presumably in real life it needs to be turned
into a subroutine that interprets the response.
Lincoln Stein's book on Network Programming with Perl is
invaluable for learning how to do this sort of thing.
Like Uri said, if you want someone to write the Perl for you
as well, there are plenty of people for hire...
--
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/
------------------------------
Date: Sun, 10 Oct 2004 11:34:42 -0700
From: "Brad Walton" <sammie-nospam@greatergreen.com>
Subject: Re: C (I think) to Perl Conversion
Message-Id: <zq2dnag6mIlcHfTcRVn-rg@comcast.com>
[snip]
>
> This program is not an interface; it's more of a debugging
> print test. Presumably in real life it needs to be turned
> into a subroutine that interprets the response.
>
> Lincoln Stein's book on Network Programming with Perl is
> invaluable for learning how to do this sort of thing.
>
> Like Uri said, if you want someone to write the Perl for you
> as well, there are plenty of people for hire...
>
> --
> Peter Scott
> http://www.perldebugged.com/
> *** NEW *** http://www.perlmedic.com/
Thanks! I don't need anyone to write it for me, I just needed some
understanding (or deciphering) of this script. You have been a great help..
I appreciate it!
Brad
------------------------------
Date: 10 Oct 2004 15:08:36 -0700
From: krakle@visto.com (krakle)
Subject: Re: C (I think) to Perl Conversion
Message-Id: <237aaff8.0410101408.599bd10@posting.google.com>
"Brad Walton" <sammie-nospam@greatergreen.com> wrote in message news:<l6CdnR_E38t7LfXcRVn-ow@comcast.com>...
> I'm stuck here... I have a sample script which accomplishes the task I need
> done, but it's done in another programing language (of which I have no
> understanding). I have researched this for hours and hours, but no luck.
>
> The script I need 'deciphered' to Perl:
Have you ever thought why someone would take the time to rewrite that
code in Perl for someone they don't know for no reason at all? No one
will. Good luck asking people to do your own work for free...
------------------------------
Date: Sun, 10 Oct 2004 16:58:17 -0700
From: "Brad Walton" <sammie-nospam@greatergreen.com>
Subject: Re: C (I think) to Perl Conversion
Message-Id: <XsOdnV0e4aEFUfTcRVn-jA@comcast.com>
"krakle" <krakle@visto.com> wrote in message
news:237aaff8.0410101408.599bd10@posting.google.com...
> "Brad Walton" <sammie-nospam@greatergreen.com> wrote in message
news:<l6CdnR_E38t7LfXcRVn-ow@comcast.com>...
> > I'm stuck here... I have a sample script which accomplishes the task I
need
> > done, but it's done in another programing language (of which I have no
> > understanding). I have researched this for hours and hours, but no luck.
> >
> > The script I need 'deciphered' to Perl:
>
> Have you ever thought why someone would take the time to rewrite that
> code in Perl for someone they don't know for no reason at all? No one
> will. Good luck asking people to do your own work for free...
Hey krakle, if you don't want to help, fine.. cya.
------------------------------
Date: Sun, 10 Oct 2004 17:01:36 -0700
From: "Brad Walton" <sammie-nospam@greatergreen.com>
Subject: Re: C (I think) to Perl Conversion
Message-Id: <P8qdnTkYG5f-UPTcRVn-ug@comcast.com>
"Brad Walton" <sammie-nospam@greatergreen.com> wrote in message
news:zq2dnag6mIlcHfTcRVn-rg@comcast.com...
> [snip]
> >
> > This program is not an interface; it's more of a debugging
> > print test. Presumably in real life it needs to be turned
> > into a subroutine that interprets the response.
> >
> > Lincoln Stein's book on Network Programming with Perl is
> > invaluable for learning how to do this sort of thing.
> >
> > Like Uri said, if you want someone to write the Perl for you
> > as well, there are plenty of people for hire...
> >
> > --
> > Peter Scott
> > http://www.perldebugged.com/
> > *** NEW *** http://www.perlmedic.com/
>
> Thanks! I don't need anyone to write it for me, I just needed some
> understanding (or deciphering) of this script. You have been a great
help..
> I appreciate it!
>
> Brad
Ok, this is what I came up with:
----
#!/usr/bin/perl -w
use strict;
use IO::Socket;
# Connect to the rcon server
my $sock = new IO::Socket::INET(PeerAddr => '209.209.44.30', PeerPort =>
'27015', proto => 'tcp',);
if (!$sock) {
die "Unable to connect $@\n";
}
# Pack packs the data according to a format string
# integer,integer,integer,null terminated string,null terminated string
my $login_packet = pack("iiiaa", 18, 1, 3, "password", "");
print "done: $login_packet\n";
# Send on the wire
print $sock $login_packet;
# Dirty hack from Alfred_Reynolds program
# sleep(1);
my $response_packet = <$sock>;
# Unknown response type but it may look something like this:
# protocol not defined in example script
my ($response_size, $response_id, $response) = unpack("iii",
$response_packet);
# more speculation follows:
if ($response_size != 12) {
die "Weird response size ! 12\n";
}
if ($response_id != 1) {
die "Got response for request we did not send\n";
}
if (!$response) {
die "Response == 0 login failed\n";
}
print "All is good!\n";
exit(0);
----
I'm not sure if I translated it properly, as I'm not getting a response from
the other end. Does anyone spot any translation errors?
Thanks for any help, Brad
------------------------------
Date: Sun, 10 Oct 2004 15:45:01 +0100
From: zaphod <none@none.com>
Subject: Re: Is PHP still slower than Perl?
Message-Id: <41694aea$0$44846$ed2619ec@ptn-nntp-reader02.plus.net>
Chris Hope wrote:
> One difference between this aspect of Perl vs PHP (and I'm speaking here as
> someone who used to develop Perl about 6 years ago and then switched to
> PHP) is that all the Perl developers seem to know about CPAN and it sounds
> like it's easy to contribute to, whereas the reverse seems to be true for
> PHP.
>
Then why did you switch to PHP?
zaphod
------------------------------
Date: Sun, 10 Oct 2004 16:56:38 GMT
From: "Eddie" <aaa@sss.com>
Subject: Module Testing Question
Message-Id: <aRdad.703002$gE.256310@pd7tw3no>
Hi,
I wanted to test all the subroutines in my module from a script,
but I have a problem. Some subroutines are called from only
inside the module and some are not. Therefore I have code
like:
1 my ($pkg, @args) = @_
and
2 my @args = @_
in other places. Unfortunately if i test my sub routine that has #2 above,
I get an error because the package name is passed into the sub and the code
isnt
expecting that. Is there a way that a script can call a method in a module
and not
pass the package name?
Thanks,
Eddie
------------------------------
Date: Sun, 10 Oct 2004 21:01:01 +0200
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: Module Testing Question
Message-Id: <416986c5$0$22619$9b4e6d93@newsread4.arcor-online.net>
Eddie wrote:
> Hi,
>
> I wanted to test all the subroutines in my module from a script,
> but I have a problem. Some subroutines are called from only
> inside the module and some are not. Therefore I have code
> like:
>
> 1 my ($pkg, @args) = @_
> and
> 2 my @args = @_
>
> in other places. Unfortunately if i test my sub routine that has #2 above,
> I get an error because the package name is passed into the sub and the code
> isnt
> expecting that. Is there a way that a script can call a method in a module
> and not
> pass the package name?
Of course there is.
Just leave away the OO-style "->" notation and call
your sub the "conventional" way in "::" notation.
You should propably have a look at "perldoc perloo" and
"perldoc perltoot" as well as "perldoc perlmod".
In fact, it's "->" notation that makes it possible at all
to do OO programming in Perl.
Just a little example to show the difference:
-----------------------------------------------------
#!perl
use strict;
use warnings;
package mymod;
sub displayargs {
print "Called with:".$/;
print " $_".$/ for( @_ );
}
1;
package main;
print "Classic notation: mymod::displayargs(...)".$/;
mymod::displayargs( 1, 2, 3 );
print $/;
print "OO notation: mymod->displayargs(...)".$/;
mymod->displayargs( 1, 2, 3 );
__END__
------------------------------------------------------
HTH
-Christian
------------------------------
Date: 10 Oct 2004 19:36:44 -0400
From: Daniel Molina Wegener <dmw@nospam.unete.cl>
Subject: perl warnings
Message-Id: <4169b97c@omega.ifxnw.cl>
Keywords: perl warning documentation faq
Hello,
Are the perl warnings documented anywhere?
I mean warnings like this:
"Use of uninitialized value in substitution (s///) at..."
Thanks...
Regards.
--
| Daniel Molina Wegener <dmw at unete dot cl>
| Developer | FreeBSD Power-User
| "God is real, unless declared integer."
------------------------------
Date: Sun, 10 Oct 2004 19:05:23 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: perl warnings
Message-Id: <I-adnUc4Zb6pXfTcRVn-sw@adelphia.com>
Daniel Molina Wegener wrote:
> Hello,
>
> Are the perl warnings documented anywhere?
>
> I mean warnings like this:
> "Use of uninitialized value in substitution (s///) at..."
Have you had a look at splain/diagnostics?
At run-time, 'use diagnostics' instead of 'use warnings' will emit more
verbose error messages:
#!/usr/bin/perl
use strict;
use diagnostics;
You can also dump stderr to a file, and use the 'splain' utility:
perl program 2>diag.out
splain diag.out
See 'perldoc diagnostics' for more. Also see 'perldoc perldiag' for a
complete list of all the extended warnings.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sun, 10 Oct 2004 17:09:36 +0100
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: possible mem leak ?
Message-Id: <0f3o32-ia5.ln1@osiris.mauzo.dyndns.org>
Quoth "arne" <news@sloeber.office.xs4all.be>:
>
> below is my rather short script that just takes STDIN and logs it to syslog
>
> if I attach this program to apache as my access-log (I know why i do it like
> this), after a few days, the process mem-size is 132Meg and above, which is
> too big for such a small program.
Use logger(1) instead.
Ben
--
We do not stop playing because we grow old;
we grow old because we stop playing.
ben@morrow.me.uk
------------------------------
Date: Sun, 10 Oct 2004 19:57:18 -0500
From: Ron Parker <ron.parker@povray.org>
Subject: Re: To Tad: Great perl class, and question
Message-Id: <slrncmjmje.58v.ron.parker@mail.parkrrrr.com>
On Sat, 09 Oct 2004 15:03:07 GMT, John W. Krahn wrote:
> Tad McClellan wrote:
>> It was my 1st time in Kentucky, I found I liked it a lot.
>
> Did you bring us back some J.D. :-)
You mean Jack Daniels' Old No. 7 TENNESSEE Sour Mash Whiskey?
Why would he bring that back from Kentucky when thay have all
those fine bourbons?
--
#macro R(L P)sphere{L __}cylinder{L P __}#end#macro P(_1)union{R(z+_ z)R(-z _-z)
R(_-z*3_+z)torus{1__ clipped_by{plane{_ 0}}}translate z+_1}#end#macro S(_)9-(_1-
_)*(_1-_)#end#macro Z(_1 _ __)union{P(_)P(-_)R(y-z-1_)translate.1*_1-y*8pigment{
rgb<S(7)S(5)S(3)>}}#if(_1)Z(_1-__,_,__)#end#end Z(10x*-2,.2)camera{rotate x*90}
------------------------------
Date: 10 Oct 2004 13:18:36 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Top 10 list algorithm
Message-Id: <ckbcrc$4pf$1@mamenchi.zrz.TU-Berlin.DE>
Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMLVI September
> MCMXCIII in <URL:news:ck6r6r$f55$1@mamenchi.zrz.TU-Berlin.DE>:
> \\ Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> \\ > Fatted (fatted@gmail.com) wrote on MMMMLV September MCMXCIII in
> \\ > <URL:news:2skn1fF1msrubU1@uni-berlin.de>:
> \\ > !! Is there a better (faster) way of implementing my top_sort function?
> \\ > !! (Creating a top 10 list of the highest numbers from a large set).
> \\ > !! Or did I get lucky? :)
> \\ >
> \\ >
> \\ > #!/usr/bin/perl
> \\ >
> \\ > use strict;
> \\ > use warnings;
> \\ > no warnings qw /syntax/;
> \\ >
> \\ > my @heap = map {int rand 1_000_000} 1 .. 100_000;
> \\ >
> \\ > sub heapify;
> \\ > sub heapify {
> \\ > my $index = shift;
> \\ >
> \\ > my ($c1, $c2) = (2 * $index + 1, 2 * $index + 2);
> \\ > my $max = $index;
> \\ > $max = $c1 if $c1 < @heap && $heap [$c1] > $heap [$max];
> \\ > $max = $c2 if $c2 < @heap && $heap [$c2] > $heap [$max];
> \\ >
> \\ > return if $max == $index;
> \\ >
> \\ > @heap [$index, $max] = @heap [$max, $index];
> \\ > heapify $max;
> \\ > }
> \\ >
> \\ >
> \\ > for (my $i = int (@heap / 2); $i >= 0; $i --) {heapify $i}
> \\ >
> \\ > if (@heap) {
> \\ > for (1 .. 10) {
> \\ > print $heap [0], "\n";
> \\ > my $tmp = pop @heap;
> \\ > last unless @heap;
> \\ > $heap [0] = $tmp;
> \\ > heapify 0;
> \\ > }
> \\ > }
> \\
> \\ Well, you're heapifying the entire list, which is still an n log n
> \\ operation. Might as well sort :)
>
> No! It's not. Look carefully, and do the math. It's an O (n) operation.
Well, I'll be damned, it is. It doesn't look like it ought to be :)
I didn't do the math, I looked it up in _Introduction to Algorithms_
(Cormen, Leiserson, Rivest) which, I suppose, you have also consulted.
So your simple solution, beautifully presented as always, is also
optimal, at least asymptotically.
I still think that in practice it would pay to keep the heap size down
to k (the number of elements to be extracted). You'd still have to
*inspect* all the input, but the number of "heap operations" (heap
insertions and original (non-recursive) calls to heapify()) would be
much smaller, and operate on a smaller heap.
Anno
------------------------------
Date: Sun, 10 Oct 2004 10:06:50 -0400
From: Shawn Corey <shawn.corey@sympatico.ca>
Subject: Re: Top 10 list algorithm
Message-Id: <Okbad.4659$hk6.30417@news20.bellglobal.com>
Anno Siegel wrote:
> I still think that in practice it would pay to keep the heap size down
> to k (the number of elements to be extracted). You'd still have to
> *inspect* all the input, but the number of "heap operations" (heap
> insertions and original (non-recursive) calls to heapify()) would be
> much smaller, and operate on a smaller heap.
>
> Anno
Unfortunately, no. I wrote some benchmarks just to see what happens in
practice. The subroutine heap_each() is commented out since it takes so
long to complete.
--- Shawn
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
# Un-comment for testing; do forget to un-comment
# the print statements in each suborutine.
# heap_once();
# heap_occasional();
# heap_each();
# sort_once();
# sort_occasional();
# sort_each();
# simple();
# exit;
timethese( 100,
{
simple => \&simple,
heap_once => \&heap_once,
heap_occasional => \&heap_occasional,
# heap_each => \&heap_each,
sort_once => \&sort_once,
sort_occasional => \&sort_occasional,
sort_each => \&sort_each,
},
);
sub heap_once {
my @top = ();
srand( 1 );
for my $i ( 1 .. 10_000 ){
my $num = rand();
push @top, $num;
my $i = $#top;
my $j = 0;
for(;;){
$j = int( $i / 2 );
if( $top[$j] < $top[$i] ){
( $top[$j] , $top[$i] ) = ( $top[$i] , $top[$j] );
}else{
last;
}
last if $j <= 0;
$i = $j;
}
}
my @list = ();
for my $i ( 0 .. 9 ){
push @list, $top[0];
my $j = 0;
while( $j*2+1 <= $#top ){
if( $j*2+2 <= $#top && $top[$j*2+1] < $top[$j*2+2] ){
$top[$j] = $top[$j*2+2];
$j = $j*2+2;
}else{
$top[$j] = $top[$j*2+1];
$j = $j*2+1;
}
}
$top[$j] = 0;
}
# print "@list\n";
}
sub heap_each {
my @top = ();
srand( 1 );
for my $i ( 1 .. 10_000 ){
my $num = rand();
push @top, $num;
my $i = $#top;
my $j = 0;
for(;;){
$j = int( $i / 2 );
if( $top[$j] < $top[$i] ){
( $top[$j] , $top[$i] ) = ( $top[$i] , $top[$j] );
}else{
last;
}
last if $j <= 0;
$i = $j;
}
my @list = ();
for my $i ( 0 .. 9 ){
push @list, $top[0];
my $j = 0;
while( $j*2+1 <= $#top ){
if( $j*2+2 <= $#top && $top[$j*2+1] < $top[$j*2+2] ){
$top[$j] = $top[$j*2+2];
$j = $j*2+2;
}else{
$top[$j] = $top[$j*2+1];
$j = $j*2+1;
}
}
$top[$j] = 0;
}
@top = @list;
}
# print "@top\n";
}
sub heap_occasional {
my @top = ();
srand( 1 );
for my $i ( 1 .. 10_000 ){
my $num = rand();
push @top, $num;
my $i = $#top;
my $j = 0;
for(;;){
$j = int( $i / 2 );
if( $top[$j] < $top[$i] ){
( $top[$j] , $top[$i] ) = ( $top[$i] , $top[$j] );
}else{
last;
}
last if $j <= 0;
$i = $j;
}
if( @top > 1_000 ){ # Change 1_000 to be as large as your memory
can handle
my @list = ();
for my $i ( 0 .. 9 ){
push @list, $top[0];
my $j = 0;
while( $j*2+1 <= $#top ){
if( $j*2+2 <= $#top && $top[$j*2+1] < $top[$j*2+2] ){
$top[$j] = $top[$j*2+2];
$j = $j*2+2;
}else{
$top[$j] = $top[$j*2+1];
$j = $j*2+1;
}
}
$top[$j] = 0;
}
@top = @list;
}
}
my @list = ();
for my $i ( 0 .. 9 ){
push @list, $top[0];
my $j = 0;
while( $j*2+1 <= $#top ){
if( $j*2+2 <= $#top && $top[$j*2+1] < $top[$j*2+2] ){
$top[$j] = $top[$j*2+2];
$j = $j*2+2;
}else{
$top[$j] = $top[$j*2+1];
$j = $j*2+1;
}
}
$top[$j] = 0;
}
# print "@list\n";
}
sub simple {
my @top = ( 0 ) x 10;
srand( 1 );
for my $i ( 1 .. 10_000 ){
my $num = rand();
my $i = -1;
for( $i = $#top; $i >= 0; $i -- ){
if( $top[$i] > $num ){
@top = ( @top[ 0 .. $i ], $num, @top[ $i + 1 .. $#top ] );
pop @top;
last;
}
}
if( $i < 0 ){
unshift @top, $num;
pop @top;
}
}
# print "@top\n";
}
sub sort_each {
my @top = ( 0 ) x 10;
srand( 1 );
for my $i ( 1 .. 10_000 ){
my $num = rand();
push @top, $num;
@top = sort { $b <=> $a } @top;
$#top = 9;
}
# print "@top\n";
}
sub sort_occasional {
my @top = ( 0 ) x 10;
srand( 1 );
for my $i ( 1 .. 10_000 ){
my $num = rand();
push @top, $num;
if( @top > 1_000 ){ # Change 1_000 to be as large as your memory
can handle
@top = sort { $b <=> $a } @top;
$#top = 9;
}
}
@top = sort { $b <=> $a } @top;
$#top = 9;
# print "@top\n";
}
sub sort_once {
my @top = ( 0 ) x 10;
srand( 1 );
for my $i ( 1 .. 10_000 ){
my $num = rand();
push @top, $num;
}
@top = sort { $b <=> $a } @top;
$#top = 9;
# print "@top\n";
}
__END__
------------------------------
Date: 10 Oct 2004 16:15:41 -0700
From: mrmacman_g4@mac.com (Kyle Moffett)
Subject: Using Perl from a PAM module (boot_DynaLoader undefined)
Message-Id: <5611cbde.0410101515.79398653@posting.google.com>
I'm writing a PAM module called pam_perl.so, designed to allow me to
call into Perl modules to do PAM auth. I have two test programs,
testauth.pl and testauth.c which act as PAM applications, the first
using Authen::PAM and the second using the C interface. When I run
the Perl version I get segfaults, but the C version can't load the
/lib/security/pam_perl.so file because it claims boot_DynaLoader is
undefined. Here is a snippet from make's output
kmoffett@hydrogen:~/projects/pam_perl$ make
[...]
cc -Iinc -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN
-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.8/CORE -DPERL_NO_SHORT_NAMES
-Iinc -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN
-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.8/CORE -UPERL_NO_SHORT_NAMES
-g -O0 -fPIC -Wall -Werror -D_REENTRANT -D_GNU_SOURCE
-DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -I/usr/local/include
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.8/CORE -g
-O0 -fPIC -Wall -Werror -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS
-DDEBIAN -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.8/CORE -o
obj/SHARED_OBJ_pam_perl/config/perlembed.o -c src/config/perlembed.c
[...]
cc -Wl,-E -L/usr/local/lib
/usr/lib/perl/5.8/auto/DynaLoader/DynaLoader.a
-L/usr/lib/perl/5.8/CORE -lperl -ldl -lm -lpthread -lc -lcrypt
-Wl,--shared -o dst/SHARED_OBJ/pam_perl.so
obj/SHARED_OBJ_pam_perl/config/perlembed.o
obj/SHARED_OBJ_pam_perl/pam_perl.o
[...]
As far as I can tell DynaLoader is being linked into the resultant .so
file, but when I run objdump on it it reports boot_DynaLoader as an
undefined symbol. objdump on the DynaLoader.a file _does_ work,
however, and shows a valid boot_DynaLoader symbol.
Cheers,
Kyle Moffett
------------------------------
Date: Sun, 10 Oct 2004 08:32:08 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: What does $base)->abs do?
Message-Id: <slrncmieeo.kbp.tadmc@magna.augustmail.com>
nntp <nntp@rogers.com> wrote:
[ snip 45 lines, with no attributions ]
> I copied the code from linkextor, it uses
> use URI::URL;
Please learn how to properly compose a followup.
Please do this soon before you become widely auto-ignored.
Provide attributions when you quote someone.
Quote only enough to establish the context for your comment.
Have you seen the Posting Guidelines that are posted here frequently?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 10 Oct 2004 17:27:41 +0100
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: While query
Message-Id: <tg4o32-q46.ln1@osiris.mauzo.dyndns.org>
Quoth Brian McCauley <nobull@mail.com>:
> Ben Morrow wrote:
>
> > Hmmm.... at the risk of sounding arrogant :), yes, I would say so. Perl
> > always compiles the comma operator into the same ops: those which build
> > a list. As a list-in-scalar-context evaluates to its last entry, the
> > two operators described in perlop will always produce the same results
> > as the one perl actually uses, making this perhaps an irrelevant
> > distinction;
>
> OK I'd like to address this without digging into at the internals of how
> the Perl compiler is implemented and looking only at the observable
> behaviour of Perl programs that would behave differently under the two
> cases.
>
> require AtExit;
>
> sub foo {
> my $msg = shift;
> print "+$msg";
> AtExit->new(sub{ print "-$msg"});
> }
>
> my $q = (foo(1),foo(2),foo(3),foo(4));
> print "|";
>
> The "two different comma operators" model predicts '+1-1+2-2+3-3+4|-4'.
>
> The "last element of list" model predicts '+1+2+3+4-1-2-3|-4' or
> '+1+2+3+4-3-2-1|-4'
No, it doesn't. A list-in-scalar-context evaluates all its elements but
the last in *void* context, meaning that they get destroyed straight
away (before the next entry in the list is evaluated). As I said, the
two models predict the same behaviour, since context is determined at
compile time, making the distinction irrelevant unless one finds one
model easier to understand than the other. I just find it a little odd
that the perl docs seem to have picked the model that is *not* the way
it is actually implemented: maybe most people (unlike me) find the
'separate operators' model easier?
> On Perl 5.8.4 I get '+1-1+2-2+3-3+4|-4'.
Yes, as I'd expect.
Something that puzzles me slightly is that if the above is modified to
sub bar {
(foo(1), foo(2), foo(3), foo(4));
}
my $q = bar;
print '|';
then we *do* get (5.8.2) '+1+2+3+4-3-2-1|-4'... it seems that 'returning
a list from a sub called in scalar context' does something slightly
different from 'evaluating a list in scalar context'; specifically, the
non-terminal elements of the list get scalar rather than void context,
and are destroyed later.
Ben
--
Although few may originate a policy, we are all able to judge it.
- Pericles of Athens, c.430 B.C.
ben@morrow.me.uk
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 7231
***************************************