[18828] in Perl-Users-Digest
Perl-Users Digest, Issue: 996 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun May 27 09:05:36 2001
Date: Sun, 27 May 2001 06:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <990968708-v10-i996@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sun, 27 May 2001 Volume: 10 Number: 996
Today's topics:
Re: How to create a text file in Windows/DOS with PERL? <krahnj@acm.org>
Re: How to create a text file in Windows/DOS with PERL? <pne-news-20010527@newton.digitalspace.net>
Locking files in CGI fails (or not?) (Klax)
Re: Locking files in CGI fails (or not?) <news@_NOSPAM_mtvwebdesign.hypermart.net>
Re: Match Parsing Glitch <abe@ztreet.demon.nl>
perl and cookies (Hans)
Re: perl and cookies <nobody@dev.null>
Re: Perl Compilers <steve@peachy.com>
Re: Perl Compilers <bart.lateur@skynet.be>
Re: Perl Compilers <carvdawg@patriot.net>
Reading a whole file in with one read() call <goldbb2@earthlink.net>
Search and Replace (felan)
Search and Replace (felan)
Shuffler <goldbb2@earthlink.net>
Re: Shuffler <pne-news-20010527@newton.digitalspace.net>
Too late for "-T" option at comment_form.cgi line 1. <davsoming@lineone.net>
Re: wwwboard.pl - Taint and Use Strict (Jill)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 27 May 2001 08:15:31 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: How to create a text file in Windows/DOS with PERL?
Message-Id: <3B10B7C6.F9E9A1B7@acm.org>
Philip Newton wrote:
>
> On Sun, 27 May 2001 05:33:48 GMT, "John W. Krahn" <krahnj@acm.org>
> wrote:
>
> > I can show you how to do it in perl, I don't know anything about PERL.
>
> I think you actually showed him how to do it in Perl, rather than perl,
> if you're going to pick nits.
>
> > #!/usr/bin/perl -w
> > use strict;
> > open FILE, '> file.txt' or die "Cannot open file.txt: $!";
> > print "This is a text file.\n";
> > __END__
>
> I think you need s/print/print FILE/ there (or maybe s/print/select
> FILE; print/).
oops
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sun, 27 May 2001 13:16:00 +0200
From: Philip Newton <pne-news-20010527@newton.digitalspace.net>
Subject: Re: How to create a text file in Windows/DOS with PERL?
Message-Id: <7co1htsspn4rafi07io4g0iec4fmi7hs1q@4ax.com>
On Sun, 27 May 2001 02:14:06 -0400, rabbits77@my-deja.com (rabbits77)
wrote:
> open(FILEHANDLE,">temp.txt") || die "cannot open file.";
^
Insert here: temp.txt: $!
This way you'll know which file went wrong, and what the error was.
> #print FILEHANDLE "Hello\n";
> close(FILEHANDLE);
^
Insert here: or die "Error closing file temp.txt: $!";
You should check your closes, especially if you open the file for
writing. For example, you may not notice a "disk full" error until you
close the file (especially if you ignore the return value from print).
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: 27 May 2001 04:07:25 -0700
From: galileoweb@hotmail.com (Klax)
Subject: Locking files in CGI fails (or not?)
Message-Id: <3ae04c6c.0105270307.21c5fd74@posting.google.com>
Hello,
I'm using a CGI written in Perl that receives the data from an HTML
form and it usually FAILS the FIRST time it is executed. After an
error, you can press BACK and submit the form again, and it always
works.
This CGI opens a text file in the UNIX system, writes some stuff and
closes the file. I think the problem might be related with the
"get_the_lock" and "drop_the_lock" functions.
What are these functions for? Are they neccesary? This is part the
code:
$lockfile="/tmp/bnbform.lck";
&get_the_lock;
open(OUT_FILE,">>$fields{'outputfile'}");
print OUT_FILE "Some stuff:\n";
print OUT_FILE "---------------------------------------------------\n";
close(OUT_FILE);
&drop_the_lock;
sub get_the_lock
{
local ($endtime);
$endtime = 60;
$endtime = time + $endtime;
while (-e $lockfile && time < $endtime)
{
# Do Nothing
}
open(LOCK_FILE, ">$lockfile");
}
sub drop_the_lock
{
close($lockfile);
unlink($lockfile);
}
Thank you very much,
Luis
------------------------------
Date: Sun, 27 May 2001 14:13:05 +0200
From: "Maarten Veerman" <news@_NOSPAM_mtvwebdesign.hypermart.net>
Subject: Re: Locking files in CGI fails (or not?)
Message-Id: <9eqr0h$63q$1@news.tudelft.nl>
why not use flock? see perldoc
"Klax" <galileoweb@hotmail.com> wrote in message
news:3ae04c6c.0105270307.21c5fd74@posting.google.com...
> Hello,
>
> I'm using a CGI written in Perl that receives the data from an HTML
> form and it usually FAILS the FIRST time it is executed. After an
> error, you can press BACK and submit the form again, and it always
> works.
>
> This CGI opens a text file in the UNIX system, writes some stuff and
> closes the file. I think the problem might be related with the
> "get_the_lock" and "drop_the_lock" functions.
>
> What are these functions for? Are they neccesary? This is part the
> code:
>
> $lockfile="/tmp/bnbform.lck";
>
> &get_the_lock;
> open(OUT_FILE,">>$fields{'outputfile'}");
> print OUT_FILE "Some stuff:\n";
> print OUT_FILE "---------------------------------------------------\n";
> close(OUT_FILE);
> &drop_the_lock;
>
> sub get_the_lock
> {
> local ($endtime);
> $endtime = 60;
> $endtime = time + $endtime;
> while (-e $lockfile && time < $endtime)
> {
> # Do Nothing
> }
> open(LOCK_FILE, ">$lockfile");
> }
>
> sub drop_the_lock
> {
> close($lockfile);
> unlink($lockfile);
> }
>
> Thank you very much,
>
> Luis
------------------------------
Date: Sun, 27 May 2001 14:30:36 +0200
From: Abe Timmerman <abe@ztreet.demon.nl>
Subject: Re: Match Parsing Glitch
Message-Id: <mqs1htg1focbaftui14kgf230mfer83hct@4ax.com>
On Sat, 26 May 2001 23:30:10 -0700, in comp.lang.perl.misc you wrote:
> Abe:
Yes master :)
> This is "partially" working now (as long as I don't use 'strict' and
> ignore thee following errors AFTER I've run the script:
>
> ERROR FOR EACH FILE RECORD READ: readline() on unopened filehandle input
> at line 67
>
> LINE 67 IS: for ( 3..11 ) { $line = <INPUT> }
>
> Part of the problem was some lines that had 'INPUT' (I changed these to
> 'INFILE' and also decided to parse two more fields and modified the 'for
> ( 3...x)' line to provide for the extra line for parsing.
I believe you just answered your own question there.
> Now with 5.6.1 (Build 626) loaded, I tried perldoc -q FAX define ...
> (and defined) but nada, zip squat came up. If I put back in 'use
> strict;' then it won't run and I still get those undefined filehandle
> errors.
That is a warning and can also be found in perldiag.pod
perldoc perldiag
If you like, you can read all documentation that comes with ActivePerl
in HTML format with your browser:
Start->Programs->ActiveState ActivePerl->documentation
> All I know is that I'm getting the .csv file output for importing into
> Excel now, and greatly appreciate all the help. However, it would be
> nice to know how to resolve the nasty errors and understand WHAT is
> still wrong.
>
> Eric
>
> --- Retweaked Script Below ---
>
> #!/usr/bin/perl -w
> #use strict; ###### will only work with this commented out !!!
Put it back in and read about strict (strict 'vars' in this case):
perldoc strict
[ snipped description of record structure ]
> $R=1; #start number for record counter
strict 'vars' complains that you didn't declare $R:
my $R = 1;
> print "Enter File Name To Parse: "; #instructions for entering file name
> $filename = <STDIN>; #enter file name to parse
my $filename = <STDIN>;
> print "Enter Output File Name: "; #instructions to enter out file name
> $datafile = <STDIN>; #enter output file name
my $datafile = <STDIN>;
But they'll both have the end_of_line still stuck to it. You really
should chomp() them off.
chomp( my $datafile = <STDIN> );
(ditto for $filename)
> open (INFILE, "$filename")
> or die "Could not open $filename as read: $!"; #open input file
>
> my $line;
> while ( defined( $line = <INFILE> ) )
If you are now running a newer Perl, you can put the original statement
(for these two lines) back there:
while ( my $line = <INFILE> )
>
[ snipped some code ]
> ####### The next line is LINE 67
> for ( 3..11 ) { $line = <INPUT> }
Remove this one, I presented that as an alternative way to write the
next line. (This is where you try to read from the unopened filehandle:
INPUT)
> $line = <INFILE> for 3..11; # skip some lines
[ snipped rest of code ]
In general (and please don't take this the wrong way), I get the feeling
you are trying to run before you can walk. You just made two big changes
to code that didn't run to begin with. Try to make it run first, then
extend to your needs step by step.
--
Good luck, Abe
Amsterdam Perl Mongers http://amsterdam.pm.org
perl -e '$_=sub{split//,pop;print pop while@_};&$_("rekcah lreP rehtona tsuJ")'
------------------------------
Date: 27 May 2001 01:03:40 -0700
From: h.born@fh-aargau.ch (Hans)
Subject: perl and cookies
Message-Id: <8d27eb70.0105270003.2b11fc65@posting.google.com>
Hi there,
I definetly need a hand with perl and cookies.
I wrote two very small skripts but wasn't retrieving the cookie back.
Skript one sets a cookie (this works just fine, I also checked the cookies.txt
on the client and the entry is there, this shows me the cookie has been placed).
But retrieving the cookie back, with skript two, doesn't work, the value is always
empty.
What am I doing wrong? Can somebody please help me out? I appreciate it!
skript one (set the cookie):
#!/usr/bin/perl
# cookie1
use strict;
use CGI qw/:standard/;
use CGI::Cookie;
my $CGIquery = CGI::new();
my $id = "7";
my $exp='+1d';
my $your_cookie = new CGI::Cookie(
-name=>'dva',
-value=>"6676",
-expires=>$exp
);
print $CGIquery->header(-cookie=>$your_cookie);
print "cookie set";
$CGIquery->end_html();
skript two (retrieve the cookie):
#!/usr/bin/perl
# cookie2
use strict;
use CGI qw/:standard/;
use CGI::Cookie;
my $CGIquery = CGI::new();
my $retreived_cookie = cookie('dva');
print $CGIquery->header();
print "cookie content: $retreived_cookie";
$CGIquery->end_html();
------------------------------
Date: Sun, 27 May 2001 08:28:40 -0400
From: Andras Malatinszky <nobody@dev.null>
Subject: Re: perl and cookies
Message-Id: <3B10F2F8.8060002@dev.null>
Hans wrote:
> Hi there,
>
> I definetly need a hand with perl and cookies.
> I wrote two very small skripts but wasn't retrieving the cookie back.
> Skript one sets a cookie (this works just fine, I also checked the cookies.txt
> on the client and the entry is there, this shows me the cookie has been placed).
> But retrieving the cookie back, with skript two, doesn't work, the value is always
> empty.
>
> What am I doing wrong? Can somebody please help me out? I appreciate it!
>
>
> skript one (set the cookie):
>
> #!/usr/bin/perl
> # cookie1
>
> use strict;
> use CGI qw/:standard/;
> use CGI::Cookie;
>
>
> my $CGIquery = CGI::new();
>
> my $id = "7";
> my $exp='+1d';
> my $your_cookie = new CGI::Cookie(
> -name=>'dva',
> -value=>"6676",
> -expires=>$exp
>
> );
>
> print $CGIquery->header(-cookie=>$your_cookie);
>
> print "cookie set";
>
> $CGIquery->end_html();
>
> skript two (retrieve the cookie):
>
> #!/usr/bin/perl
> # cookie2
>
> use strict;
> use CGI qw/:standard/;
> use CGI::Cookie;
>
>
> my $CGIquery = CGI::new();
>
> my $retreived_cookie = cookie('dva');
>
> print $CGIquery->header();
>
> print "cookie content: $retreived_cookie";
>
> $CGIquery->end_html();
Could it be something as simple as not having a start_html line in your
skript two? Do you see the words 'cookie content:' in your browser? If
yes, your problem is somewhere else. If no, it's possible that the
server does see the cookie being returned by the client, but the HTML
it's sending back to the client is malformed so you can't see it in the
browser.
------------------------------
Date: Sun, 27 May 2001 09:54:21 +0100
From: "Steve Kay" <steve@peachy.com>
Subject: Re: Perl Compilers
Message-Id: <9eqfdm$bpt$1@uranium.btinternet.com>
perldoc -q "How can I compile my Perl"
"Peter Reid" <peter.reid2000@ntlworld.com> wrote in message
news:1103_990892961@peterrei...
> Does anyone know of any decent FREEWARE compilers that will take a Perl
script and compile it into a standalone .exe? URLs would be good. Email them
to me as my
> computer is dodgy and I can't always connect to the newsgroup.
>
> Thanks,
>
> Peter
>
> peter.reid2000@ntlworld.com
>
------------------------------
Date: Sun, 27 May 2001 11:10:01 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Perl Compilers
Message-Id: <57o1ht0un3ofef4dc6spvh7esstuppa29i@4ax.com>
Peter Reid wrote:
>Does anyone know of any decent FREEWARE compilers that will take a Perl
>script and compile it into a standalone .exe? URLs would be good.
First of all, those are not compilers. They simply wrap a perl
interpreter and the script into one EXE file. The only thing it's really
good for, is hiding your code. And not too well.
Why should anybody help you hide your code for free? You don't seem to
be willing to do some sharing of code yourself.
--
Bart.
------------------------------
Date: Sun, 27 May 2001 08:24:32 -0400
From: H C <carvdawg@patriot.net>
Subject: Re: Perl Compilers
Message-Id: <3B10F200.FA6DACD@patriot.net>
On the one hand, that question is asked a lot (ie, 'compilers'), so the OP
should have
checked the FAQ or docs.
On the other hand, I wrote a pretty decent Perl script once for a company I
worked for.
The management wanted to have it compiled, or even ported to VB, before
sending it to
another part of the company...members of our own 'team'. The reasoning was
leakage of
IP, and they didn't want anyone monkeying with it...they just wanted them to
run it and
that's it.
Bart Lateur wrote:
> Peter Reid wrote:
>
> >Does anyone know of any decent FREEWARE compilers that will take a Perl
> >script and compile it into a standalone .exe? URLs would be good.
>
> First of all, those are not compilers. They simply wrap a perl
> interpreter and the script into one EXE file. The only thing it's really
> good for, is hiding your code. And not too well.
>
> Why should anybody help you hide your code for free? You don't seem to
> be willing to do some sharing of code yourself.
>
> --
> Bart.
------------------------------
Date: Sun, 27 May 2001 06:56:49 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Reading a whole file in with one read() call
Message-Id: <3B10DD71.D7E6CF2D@earthlink.net>
Joe Schaefer wrote:
>
> Benjamin Goldberg <goldbb2@earthlink.net> writes:
>
> > Suppose I open a file from a mounted system whose reads are very
> > slow, for example the following with a slow network link:
> >
> > open( FILE, "/afs/something.jp/foo/blah.gif" );
> >
> > (With me being in the US). If I then do:
> >
> > read( FILE, $buf, -s FILE );
> >
> > Am I garunteed to get all of the file?
>
> No- but you are guaranteed that an error occurred if perl's
> read() is short in this situation.
So, I'm guaranteed to get *either* the whole file, or an error, right?
Erm, no, looking at your code, I see that, *if* I get only part of the
file, read() will return the amount gotten, *and* set $! ... or at
least, that's what the code seems to imply.
> > What if the filesystem presents a few bytes of data, then blocks for
> > a few seconds transfering more, then continues? Is there any chance
> > that read() will block until *some* data is there, and return it,
> > even if that amount is less than the amount I asked for?
>
> I don't think so, but as perl's read() is usually a single call to
> stdio's fread(3), the manpage for fread should provide the answer.
> Mine says fread should continue until either the buffer is full, eof,
> or error.
>
> >
> > If so, I might want to do:
> >
> > for( $buf = '', $want = -s FILE; $want; $want -= $got ) {
> > $got = read( FILE, $buf, $want, length $buf ) or die "$!";
>
> Better:
>
> my $want = -s FILE;
> my $data;
> $want == read (FILE, $data, $want) or die "partial read error: $!";
Are you sure that the error is going to be in $! ? The fread man page
indicates that an error can be tested for with ferror, but it doesn't
mention it going into errno (which is what $! represents).
> > This should read chunks as big as I can get, trying again if I
> > didn't get everything.
> >
> > But is it *needed* for read()? What about for sysread()?
>
> No for read(); yes for sysread().
>
> sysread is perl's interface to read(2), which may return a short
> read without an error occurring. read(2) returns -1 on error, which
> sysread returns as an undef. Hence the loop might look like
>
> my ($want, $data) = (-s FILE, "");
> 1 while sysread( FILE, $data, $want, length $data );
> die "partial read error: $!" unless $want == length $data;
Ok, this makes sense, assuming that the file is not growing while we are
reading. Of course, if the file is a pipe, this can have *really*
unexpected results, but hey, nothing's perfect. My code (with read
replaced with sysread) would handle a pipe ok... -s tells how much is
currently in the pipe, then it reads *exactly* that much data, then
finishes. Yours will read from the pipe until the pipe closes.
Anyway, I guess the final version would be:
my ($want, $buf) = (-s FILE, "");
while( 1 ) {
my $got = sysread( FILE, $buf, $want, length $buf );
defined $got or die "Error reading from file: $!";
$got or die "Partial read error: Unexpected eof!";
last if !($want -= $got);
} continue {
if( -p FILE ) {
die "Somebody's been reading from my pipe!"
if -s FILE < $want;
} elsif( -s FILE < ($want + length $buf) ) (
die "Somebody truncated my file!";
}
}
warn "More data in pipe" if( -p FILE && -s FILE );
warn "File has grown!" if( !-p FILE && -s FILE > length $buf );
This is more robust, though obviously the error checking stuff in the
continue block can slow things down can slow things down.
Does anybody know if this code breaks if FILE is defined as being utf8
data? If -s, length, and sysread's return values aren't measuring the
same things, this could get very confused.
--
#! perl -- This took a heck of a lot of work to make. I hope it works.
@p{split//,'justaperlhacker'}=map hex,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);
------------------------------
Date: Sun, 27 May 2001 12:25:17 GMT
From: felan_66@hotmail.com (felan)
Subject: Search and Replace
Message-Id: <3b10f21d.163396495@news.uio.no>
I have the following program that prompts for a file name and searches
and replaces strings in a given filen:
print "Input file name: ";
chop($infilename = <STDIN>);
print "Output file name: ";
chop($outfilename = <STDIN>);
open(IN,$infilename) ||
die "cannot open $infilename for reading: $!";
die "will not overwrite $outfilename" if -e $outfilename;
open (OUT,">$outfilename") ||
die "cannot create $outfilename: $!";
while (<IN>) { # read a line from file IN into $_
s/Mother:/Father:/gi;
s/Children: <BR>/ : ÚClÛqp¾ ÖDÛ<BR>/gi;
print OUT $_; # print that line to file OUT
}
close(IN);
close(OUT);
Now I have two problems:
1) I would like to search the text:
Father
and Mother
and replace it with
Parents
with another words I would like to get rid of the linebreak. I have
tried to add the following without success:
s/Father and Mother/Parents/s;
I get the following error:
Substitution pattern not terminated in file
search-replace.pl at line 42, next char ^>
parse error in file search-replace.pl at line 42, next 2 tokens
"s/Father and Mother/Parents/s;"
Execution of search-replace.pl aborted due to compilation errors.
so I have problems with the s-modifier
2) I would like to run this program for all the files in a given
directory, so that the search-replace operation can be done on all the
.html files in a given directory
------------------------------
Date: Sun, 27 May 2001 12:58:26 GMT
From: felan_66@hotmail.com (felan)
Subject: Search and Replace
Message-Id: <3b10f9e5.165388468@news.uio.no>
I have the following program that prompts for a file name and searches
and replaces strings in a given filen:
print "Input file name: ";
chop($infilename = <STDIN>);
print "Output file name: ";
chop($outfilename = <STDIN>);
open(IN,$infilename) ||
die "cannot open $infilename for reading: $!";
die "will not overwrite $outfilename" if -e $outfilename;
open (OUT,">$outfilename") ||
die "cannot create $outfilename: $!";
while (<IN>) { # read a line from file IN into $_
s/Mother:/Father:/gi;
s/Children: <BR>/ : ÚClÛqp¾ ÖDÛ<BR>/gi;
print OUT $_; # print that line to file OUT
}
close(IN);
close(OUT);
Now I have two problems:
1) I would like to search the text:
Father
and Mother
and replace it with
Parents
with another words I would like to get rid of the linebreak. I have
tried to add the following without success:
s/Father and Mother/Parents/s;
I get the following error:
Substitution pattern not terminated in file
search-replace.pl at line 42, next char ^>
parse error in file search-replace.pl at line 42, next 2 tokens
"s/Father and Mother/Parents/s;"
Execution of search-replace.pl aborted due to compilation errors.
so I have problems with the s-modifier
2) I would like to run this program for all the files in a given
directory, so that the search-replace operation can be done on all the
.html files in a given directory
tnx
felan_66@hotmail.com
------------------------------
Date: Sun, 27 May 2001 06:05:40 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Shuffler
Message-Id: <3B10D174.2CE4551C@earthlink.net>
This is a multi-part message in MIME format.
--------------31F5D3B0BE01967DF5220440
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I've created a module for my block-cipher shuffler. I'm posting it here
first, for comments on correctness and improvements. If you all like
it, I'll put it on cpan.
Also, Benchmark can only show relative execution speeds... does anyone
know how I would write a program to compare memory usage?
A Shuffler for 10_000 values *should* use 1/20 of the memory that an
ordinary shuffle of 10_000 values would require. It should also take
about 1/20 of the time to create (but much more time to iterate
through). I want to show this with tests.
PS I don't have perl on my machine, and don't have room to install it
and all the standrd modules. Could anyone suggest some place where I
could get a shell account where they have perl, so I can do my
development and testing?
PPS I created this sig a few days ago. Could someone comment on what
they think of it? I don't even know if it works... It's actually
pretty straightforward code.
--
#! perl -- This took a heck of a lot of work to make. I hope it works.
@p{split//,'justaperlhacker'}=map hex,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);
--------------31F5D3B0BE01967DF5220440
Content-Type: text/plain; charset=us-ascii; name="Shuffler.pm"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Shuffler.pm"
#! perl -w
package Algorithm::Numerical::Shuffler;
use strict;
use POSIX qw(ceil);
use Algorithm::Numerical::Shuffle qw(shuffle);
sub new {
my ($class) = shift;
my ($min, $range) = 0;
if( @_ == 2 ) {
$min = $_[0]; $range = $_[1] - $min + 1;
} else if( @_ == 1 ) {
$range = $_[0];
} else {
require Carp;
Carp::croak("Usage:\n".
"\tShuffler->new(from,to)\nor".
"\tShuffler->new(range)");
}
my $sboxsize = ceil(sqrt($range));
my %self = { range => $range, min => $min, index => [0,0] };
push @$self{sbox}, scalar shuffle(0..$sboxsize-1) for(0..3);
bless \%self, $class;
}
sub reset {
@{$_[0]->{index}} = (0,0);
}
sub next {
my $self = shift;
my $index = $self->{index};
my $sboxes = $self->{qw(sbox0 sbox2 sbox3 sbox4);
my ($range,$result) = $self->{range};
my $mod = @{$sboxes->[0]};
do {
if( $index->[1] == $mod ) {
@$index = (0,0);
return undef;
}
my ($c, $d) = @$index;
$c = $c + $sboxes->[0][$d] % $mod;
$d = $d + $sboxes->[1][$c] % $mod;
$c = $c + $sboxes->[2][$d] % $mod;
$d = $d + $sboxes->[3][$c] % $mod;
$result = $c * $mod + $d;
if( ++$index->[0] == $m } {
++$index->[1];
$index->[0] = 0;
}
} until( $result < $range );
return $result + $self->{min};
}
1;
__END__
=head1 NAME
Shuffler - Produce a psuedo-random permutation on a range of integers.
=head1 SYNOPSIS
use Shuffler;
my $permutation = Shuffler->new(1, 10000);
# produces numbers in the range 1..10000
while( defined $_ = $permutation->next ) {
print $_,"\n";
}
$permutation = Shuffler->new(50);
# produces numbers in the range 0..49
for(0..40) {
print $permutation->next,"\n";
}
$permutation->reset;
=head1 DESCRIPTION
C<Shuffler> produces a psuedo-random permutation on a range of integers,
using significantly less memory than a Durstenfeld. It does this by
creating a small block cipher, and enciphering successive values of a
counter, discarding those results outside of the range.
To produce a random permutation on N integers, Durstenfeld requires an
array of size N. For the same N, Shuffler produces 4 arrays, size
ceil(sqrt(N)). For a 10_000 element permutation, this is 4 arrays of
size 100: 20 times smaller! For a 1000_000 element permutation,
Shuffler uses 4 arrays size 1000, which is 0.4% the size that
Durstenfeld would require.
=head1 LITERATURE
The cipher used is known as a Fiestel block cipher. There is a large
body of work which has been done to analyse this structure, but the
relevant results can be stated simply: If 4 rounds are used, with
random functions as the F functions (the sboxes in this case),
distinguishing the psuedo-random permutation produced from a truly
random permutation is an NP-complete problem. Thus, a shuffled array
may be safely replaced by the use of a Shuffler without significant loss
of security.
=head1 CAVEAT
The number of possible permutations of N items is N!. To be able to
uniformly select from all N! permutations, we would need either a true
random number generator (which we haven't), or a prng with at least
log2(N!) bits of state in our random number generator. What we have is
a prng with 32 or perhaps 48 bits of state.
This means that a shuffle of a list of 13 elements will not generate
certain permutations, as 13! > 2^32.
Even with 48 bits bit of state, the limit is not much more. If we have
17 items, we cannot generate all possible permutations, as 17! > 2^48.
Also, due to how we save on memory, for small permutations, we produce
*significantly* fewer permutations than are possible. I would advise
only using this package if you are shuffling ranges of numbers greater
than 256. For smaller permutations, use Algorithm::Numerical::Shuffle.
=head1 AUTHOR
This package was written by Benjamin Goldberg
=head1 COPYRIGHT
Copyright 2001 by Benjamin Goldberg.
=head1 LICENSE
This program is copyright 2001 by Benjamin Goldberg.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
=cut
--------------31F5D3B0BE01967DF5220440--
------------------------------
Date: Sun, 27 May 2001 13:29:45 +0200
From: Philip Newton <pne-news-20010527@newton.digitalspace.net>
Subject: Re: Shuffler
Message-Id: <l0p1htsj569nqfvfjcpptr7er34fc5nveh@4ax.com>
On Sun, 27 May 2001 06:05:40 -0400, Benjamin Goldberg
<goldbb2@earthlink.net> wrote:
> This is a multi-part message in MIME format.
> --------------31F5D3B0BE01967DF5220440
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
Huh? What's wrong with including the code inline or including an URL?
> PS I don't have perl on my machine, and don't have room to install it
> and all the standrd modules. Could anyone suggest some place where I
> could get a shell account where they have perl, so I can do my
> development and testing?
digitalspace.net? Panix?
> PPS I created this sig a few days ago. Could someone comment on what
> they think of it? I don't even know if it works... It's actually
^^^^^
> pretty straightforward code.
Well, that's straightforward to test, isn't it? Run it and compare the
expected results with the actual results.
And if you don't have access to Perl, it's interesting to me why you're
coding in it.... Makes testing so much harder, too.
> =head1 NAME
>
> Shuffler - Produce a psuedo-random permutation on a range of integers.
pseudo
> =head1 DESCRIPTION
>
> C<Shuffler> produces a psuedo-random permutation on a range of integers,
pseudo
> using significantly less memory than a Durstenfeld. It does this by
^^^^^^^^^^^
Do you want to explain what that is, or assume that anyone who might
want to use your module already know about block ciphers (or whatever a
Durstenfeld is)?
> To produce a random permutation on N integers, Durstenfeld requires an
> array of size N. For the same N, Shuffler produces 4 arrays, size
> ceil(sqrt(N)). For a 10_000 element permutation, this is 4 arrays of
> size 100: 20 times smaller! For a 1000_000 element permutation,
> Shuffler uses 4 arrays size 1000, which is 0.4% the size that
^
of
> Durstenfeld would require.
>
> =head1 LITERATURE
>
> The cipher used is known as a Fiestel block cipher.
"Feistel", no?
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Sun, 27 May 2001 09:24:09 +0100
From: "David Soming" <davsoming@lineone.net>
Subject: Too late for "-T" option at comment_form.cgi line 1.
Message-Id: <th1e1up1errqf0@corp.supernews.co.uk>
Hi,
I got as far as line 1 before I stumbled! lol
Just testing this locally and got "Too late for "-T" option at
comment_form.cgi line 1."
What exactly is #!/usr/bin/perl -wT
If you want the script ill post it. Thanks David.
------------------------------
Date: 27 May 2001 04:16:07 -0700
From: jmjlampert@aol.com (Jill)
Subject: Re: wwwboard.pl - Taint and Use Strict
Message-Id: <9287c79.0105270316.62e2c44a@posting.google.com>
Henry Hartley <hartleh1@westat.com> wrote in message news:<3B0E9968.193D27FB@westat.com>...
>
> When I started out in the web field, I didn't know any better and I
> downloaded a few of Matt's scripts. I'm not particularly brilliant when
> it comes to all the security issues but I am smart enough to recognize
> good advice when I hear it. It didn't take me long to realize that lots
> of the folks here on clpm are pretty bright and if they say "don't use
> this, it sucks" I figure that even if I cannot see the problem, it's
> probably there.
..........
> Hope this helps. Just because something seems easy and lots of people
> do it that way, doesn't mean its a good idea. Security can be tricky.
> Listen to the experts. You'll be glad you did.
Thanks Henry. I appreciate you taking so much trouble to explain
things, and also for your practical suggestions.
Jill
------------------------------
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 996
**************************************