[12536] in Perl-Users-Digest
Perl-Users Digest, Issue: 6136 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 26 14:07:13 1999
Date: Sat, 26 Jun 99 11:00:16 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 26 Jun 1999 Volume: 8 Number: 6136
Today's topics:
Re: CGI.pm versus twelve or so lines of code.... <gellyfish@gellyfish.com>
Re: Filter Modules <rootbeer@redcat.com>
Re: HELP recursion, directories & PCNFS <rootbeer@redcat.com>
Re: I need help with pack! (Larry Rosler)
Index of an array-item (Peter Johansson)
Re: Index of an array-item <tchrist@mox.perl.com>
Re: Index of an array-item (Larry Rosler)
Re: Looking for very small, basic bulletin board system <gellyfish@gellyfish.com>
Re: Multiple user login (Larry Rosler)
Re: Multiple user login <rick.delaney@home.com>
My script works! PLEASE HELP!!! (Anno Siegel)
Re: Need some help on Sockets BSDI 4.0 (ZepHead)
Re: rename dose not work (Jason Q.)
Re: Unix CGI > Win/Mac <rootbeer@redcat.com>
Re: validating a regexp from a CGI form (Bart Lateur)
Re: Very easy integer question <tchrist@mox.perl.com>
Re: Viral matters [completely off-topic] <John.Thompson@ibm.net>
wwwboard on webjump.com luvencl@my-deja.com
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 26 Jun 1999 17:17:52 -0000
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: CGI.pm versus twelve or so lines of code....
Message-Id: <7l3200$4gn$1@gellyfish.btinternet.com>
On Wed, 23 Jun 1999 00:14:20 GMT hiro wrote:
> $value =~ s/~!/ ~!/g;
What is *that* all about ?
I never saw that in the RFC ...
/J\
--
Jonathan Stowe <jns@gellyfish.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
Hastings: <URL:http://www.newhoo.com/Regional/UK/England/East_Sussex/Hastings>
------------------------------
Date: Sat, 26 Jun 1999 10:58:17 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Filter Modules
Message-Id: <Pine.GSO.4.02A.9906261050540.26514-100000@user2.teleport.com>
On Fri, 25 Jun 1999, Tri Tram wrote:
> I have been trying to learn how to use the Filter module from CPAN.
You mean, for source filtering, right? That would be the Filter::*
modules, I think.
> I also downloaded the module, but there wasn't much
> documentation in that either.
That's not what I found. Is this the file you got?
http://www.cpan.org/authors/id/PMQS/Filter-1.16.tar.gz
There are many comments within the example files. Those are also
documentation. Does that give you what you needed?
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Sat, 26 Jun 1999 10:41:30 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: HELP recursion, directories & PCNFS
Message-Id: <Pine.GSO.4.02A.9906261040040.26514-100000@user2.teleport.com>
On Wed, 23 Jun 1999, william wrote:
> find sub{print $File::Find::name, -d && '/', "\n"}, @dirname;
>
> This works fine on the SUN, but as soon as I change the
> @dirname to a PC NFS link all I get is the directory names
> and file name directly under that directory. It no longer
> recurses down into the sub directories.
Does it help to set $File::Find::dont_use_nlink near the top of your
program? Good luck!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Sat, 26 Jun 1999 08:59:32 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: I need help with pack!
Message-Id: <MPG.11de98a7b7076a08989c4f@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <3774963C.E5F@packet.net> on Sat, 26 Jun 1999 04:58:36 -0400,
esalmon@packet.net <esalmon@packet.net> says...
> I am trying to pack a string into a urlencoded or x-www-form-urlencoded
> using the pack() function.
>
> What am I doung wrong here:
>
> $string =~ s/(\W)/pack("CH", ord('%'), ord($1))/eg;
You can do it with unpack:
$string =~ s/(\W)/'%' . unpack 'H2', $1/eg;
But I prefer sprintf:
$string =~ s/(\W)/sprintf '%%%.2x', ord $1/eg;
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Sat, 26 Jun 1999 17:20:50 +0200
From: pjotor@kurir.net (Peter Johansson)
Subject: Index of an array-item
Message-Id: <MPG.11df0e40cd287f29989680@news.canit.se>
Hi,
I've got this array @items which contains a list of, well, items. Now I'd
like to known the index of, say, the item named "foo". Is there a nice
way to do this in Perl? (I'm quite sure there is, I just haven't found it
yet...)
Regards, Peter
------------------------------
Date: 26 Jun 1999 10:30:44 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Index of an array-item
Message-Id: <37750034@cs.colorado.edu>
[courtesy cc of this posting mailed to cited author]
Cursed to torture himself and others with MicroPlanet Gravity v2.11,
pjotor@kurir.net (Peter Johansson) writes in comp.lang.perl.misc:
:I've got this array @items which contains a list of, well, items. Now I'd
:like to known the index of, say, the item named "foo". Is there a nice
:way to do this in Perl? (I'm quite sure there is, I just haven't found it
:yet...)
NAME
perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date:
1999/05/23 20:37:49 $)
DESCRIPTION
The section of the FAQ answers question related to the manipulation
of data as numbers, dates, strings, arrays, hashes, and miscellaneous
data issues.
[verba deleta]
Data: Arrays
[verba deleta]
How can I tell whether a list or array contains a certain element?
Hearing the word "in" is an *in*dication that you probably
should have used a hash, not a list or array, to store your
data. Hashes are designed to answer this question quickly and
efficiently. Arrays aren't.
That being said, there are several ways to approach this. If you
are going to make this query many times over arbitrary string
values, the fastest way is probably to invert the original array
and keep an associative array lying about whose keys are the
first array's values.
@blues = qw/azure cerulean teal turquoise lapis-lazuli/;
undef %is_blue;
for (@blues) { $is_blue{$_} = 1 }
Now you can check whether $is_blue{$some_color}. It might have
been a good idea to keep the blues all in a hash in the first
place.
If the values are all small integers, you could use a simple
indexed array. This kind of an array will take up less space:
@primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
undef @is_tiny_prime;
for (@primes) { $is_tiny_prime[$_] = 1 }
# or simply @istiny_prime[@primes] = (1) x @primes;
Now you check whether $is_tiny_prime[$some_number].
If the values in question are integers instead of strings, you
can save quite a lot of space by using bit strings instead:
@articles = ( 1..10, 150..2000, 2017 );
undef $read;
for (@articles) { vec($read,$_,1) = 1 }
Now check whether `vec($read,$n,1)' is true for some `$n'.
Please do not use
$is_there = grep $_ eq $whatever, @array;
or worse yet
$is_there = grep /$whatever/, @array;
These are slow (checks every element even if the first matches),
inefficient (same reason), and potentially buggy (what if there
are regex characters in $whatever?). If you're only testing
once, then use:
$is_there = 0;
foreach $elt (@array) {
if ($elt eq $elt_to_find) {
$is_there = 1;
last;
}
}
if ($is_there) { ... }
--
"C++ makes good programmers better, and bad programmers obvious."
- Anonymous researcher at IBM-TJ Watson
------------------------------
Date: Sat, 26 Jun 1999 10:01:16 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Index of an array-item
Message-Id: <MPG.11dea72e630a2dd1989c50@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <37750034@cs.colorado.edu> on 26 Jun 1999 10:30:44 -0700, Tom
Christiansen <tchrist@mox.perl.com> says...
> Cursed to torture himself and others with MicroPlanet Gravity v2.11,
> pjotor@kurir.net (Peter Johansson) writes in comp.lang.perl.misc:
>
> :I've got this array @items which contains a list of, well, items. Now I'd
> :like to known the index of, say, the item named "foo". Is there a nice
> :way to do this in Perl? (I'm quite sure there is, I just haven't found it
> :yet...)
>
> NAME
> perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date:
> 1999/05/23 20:37:49 $)
...
> How can I tell whether a list or array contains a certain element?
Cursed to torture myself and others with MicroPlanet Gravity v2.11, I
wonder why you think that this FAQ is responsive to the particular
question. Where in the array is not the same as whether in the array.
I don't know how to do this besides using a dumb for-loop linear search
with breakout on match, do you?
my $i;
for ($i = 0; $i < @array; ++$i) {
last if $array[$i] eq 'foo';
}
print $i < @array ? "Found at $i\n" : "Not found.";
The index might actually be useful, for example to splice stuff in after
that element, or at the end if not found.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 26 Jun 1999 15:59:09 -0000
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Looking for very small, basic bulletin board system
Message-Id: <7l2tcd$4gd$1@gellyfish.btinternet.com>
On 26 Jun 1999 00:01:54 GMT Rich Campbell wrote:
> ivanhead@my-deja.com wrote in article <7kospg$jos$1@nnrp1.deja.com>...
>> Oh, I'm ecstatic.
>>
>> Could you perhaps give me a some more useful information?
>>
>> In article <376FD6EE.C7E6486F@mail.cor.epa.gov>,
>> David Cassell <cassell@mail.cor.epa.gov> wrote:
>> > yvan@wharris.com wrote:
>> > >
>> > > I need a very small and basic bulletin board system for use in a web
>> > > page. What I would like is a cgi script that would allow me to do
> [snip]
>> > You'll be happy to know that there's a thousand of these on the
>> > web. Just don't go to Matt's Script Archive. Happy searching!
>> >
>
> heh heh - I think there's a "bot" on here that randomly answers questions
> with
> "try reading PerlFaqXXXX"
>
I dont believe anyone said anything about any FAQ here - although this is
a Frequently Asked Question. What we do have here is a bunch of people
who see these questions over and over and over and over again but nonetheless
still take the time to suggest to the questioner the proper places to
start looking for what they want.
> you might try http://www.worldwidemart.com/scripts/wwwboard.shtml
> I've used it and customized it a little (it's sort of plain - but works
> great)
>
I would be recommending that - please see my post:
<http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=479798920&fmt=text>
I will be giving your e-mail address to everyone who asks come next
january why this crappy program is printing funny dates OK.
/J\
--
Jonathan Stowe <jns@gellyfish.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
Hastings: <URL:http://www.newhoo.com/Regional/UK/England/East_Sussex/Hastings>
------------------------------
Date: Sat, 26 Jun 1999 08:14:04 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Multiple user login
Message-Id: <MPG.11de8dfe73575900989c4e@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <M25d3.4793$ll5.36576@server1.news.adelphia.net> on Sat, 26
Jun 1999 14:01:16 GMT, David Schoen <dschoen@adelphia.net> says...
...
> if (($input{'pass'} ne "library") || ($input{'id'} != "123"))
>
> This script works with the line like this. If you type on the form
> "library" as the password and then the number "123" as the ID, the action
> is processed in the way I want it to be.
>
> The problem is that I will have multiple ids, so how do I write that line so
> that
> the computer will look for "library as the password and either of any of a
> series of numbers that I enable ahead of time. In regular boolean logic:
>
> library and (123 or 125 or 120 or 130)
>
> I have tried a variety of ways to string id numbers with the II operator to
> the last part of the statement, but I am failing. Any guidance would be
> appreciated.
Here is the most straightforward way (not necessarily the best way) to
solve the problem. It gets you through your difficulty with the Boolean
logic. I have left off all the superfluous punctuation.
if ($input{pass} ne 'library' || $input{id} != 123
&& $input{id} != 125
&& $input{id} != 120
&& $input{id} != 130
) { ...
There seems to be no rationale to your numbers that would support
something as simple as a range check. But if those lists get long, the
ladder becomes slow, ugly, and hard to maintain, so I would use a hash
lookup.
my %ids;
@ids{ 123, 125, 120, 130 } = ();
if ($input{pass} ne 'library' || !exists $ids{$input{id} + 0}) { ...
where the 0 is added to allow for silly forms of the input that might
not match exactly as strings against the hash keys (e.g., 0123 or
123.0). You might also throw in an 'lc' on $input{pass}; general policy
when dealing with arbitrary user input should be to be as accepting as
possible of things that make some sense.
Finally, I would invert the negative logic like this:
unless (lc $input{pass} eq 'library' && exists $ids{$input{id} + 0}) {
...
Think positively!
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Sat, 26 Jun 1999 15:23:16 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: Multiple user login
Message-Id: <3774F027.2F4CD379@home.com>
[posted & mailed]
David Schoen wrote:
>
> if (($input{'pass'} ne "library") || ($input{'id'} != "123"))
^^^^^^^^^^^^ ^^^^^^^^
Be careful about using string comparison operators on numbers and
numeric comparison operators on strings. The first one you have up
there is good; the second should be either
$input{'id'} != 123)
or
$input{'id'} ne "123")
> The problem is that I will have multiple ids, so how do I write that line so
> that [TOM CHRISTIANSEN WAS HERE. KILROY]
> the computer will look for "library as the password and either of any of a
> series of numbers that I enable ahead of time. In regular boolean logic:
>
> library and (123 or 125 or 120 or 130)
>
> I have tried a variety of ways to string id numbers with the II operator to
> the last part of the statement, but I am failing.
It looks like you learned about the || (not II) operator from a document
with a terrible font. The 'pipe' key is usually on the upper right-hand
side of your keyboard.
$pass eq 'library' && ($id == 123 || $id == 125 || $id == 120 || $id ==
130)
If you have lots and lots of IDs this can get pretty tedious. Use a
hash when you want to check the properties of a scalar.
my %is_valid;
@is_valid{123, 125, 120, 130} = ();
Then the above test becomes
$pass eq 'library' && exists $is_valid{$id}
Remember what I said about mixing up strings and numbers, though, since
the key to %is_valid is always treated as a string. This means IDs like
0123 won't be considered valid so you might want to do an explicit
conversion first:
$is_valid{$id + 0};
perldoc perldata
perldoc perlop
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: 26 Jun 1999 17:35:53 -0000
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: My script works! PLEASE HELP!!!
Message-Id: <7l331p$rqr$1@lublin.zrz.tu-berlin.de>
Beg your pardon for the flippant subject...
I have a bit of code which I'd like to present to the combined scrutiny
of the group. As the subject says, it works, but I utterly dislike it.
I'm using strings as sets of integers; $i is in a set $set when
vec( $set, $i, 1) == 1. The idea is to iterate over all the elements
of a set, returning undef once when all elements are done. After
that, a new set will be accepted. Sets are largish, about 5 K each,
so I want to avoid copying. Here is what I wrote:
1 { my ( $set, $byte, $bit ); # private variables, therefore
# ...bare block around
2 sub next_element {
3 if ( not defined $byte ) { # see if we need a new byte
4 if ( not defined $set ) { # ... or if we were done last
# time around
5 $set = shift; # if so, accept a new set
6 }
7 if ( $set =~ m/([^\000])/g ) { # is there another nonzero byte?
8 $byte = $1; # if so, catch it
9 $bit = -1; # ...and initialize bit number
10 } else { # all remaining bytes are zero
11 undef $set; # so we're done with this set
12 undef $byte; # ...and this byte too
13 return; # signalize end of loop
14 }
15 }
16 while ( ++$bit < 8 ) { # find another nonzero bit
17 last if vec( $byte, $bit, 1); # gotcha
18 }
19 if ( $bit < 8 ) { # loop left via last?
20 return 8*( pos( $set) - 1) + $bit; # return bit position in vec
21 } else { # this byte is used up
22 undef $byte; # ...so undefined it
23 return &next_element; # ...and try again
24 }
25 }
26 }
The say a program is well written when every routine does almost nothing.
In this light, next_element does way too much. I'd like to decompose
the job into, say, next_byte (to retrieve the next non-zero byte from
a string) and next_bit (to retrieve the next non-zero bit from a byte),
but went through even worse contortions when I tried.
Also, while I'm ordinarily quite fond of recursion, the one in line 23
doesn't look right. It's really a cop-out to avoid repeated code and
I feel, if the overall logic was clearer the necessity to repeat code
wouldn't come up in the first place.
Oh, and I realize that I can only iterate over one set at a time with
this design. Instead of using a single set of private variables,
a closure would solve this, but at the moment that's beside the point.
Wide open to suggestions,
Anno
------------------------------
Date: Sat, 26 Jun 1999 17:36:49 GMT
From: groovyt@erols.com (ZepHead)
Subject: Re: Need some help on Sockets BSDI 4.0
Message-Id: <groovyt-2606991349340001@cn226598-b.wall1.pa.home.com>
>You should be using the Socket module - that defines the constants and
>helper functions that will help to mek your code more independent of
>the underlying system - most likely some of your hard coded values
>have changed ....
>
>/J\
yeah I guess it time. : )
Thanks
Russ
------------------------------
Date: Sat, 26 Jun 1999 16:18:29 GMT
From: pigs_can_fly@mindless.com (Jason Q.)
Subject: Re: rename dose not work
Message-Id: <3778fb96.40024527@news.cyberway.com.sg>
vitanut@my-deja.com wrote:
>I am new to Perl and am having difficulty with the rename function.
>
>Example:
>rename("/usr/www/vitanut/old.txt","/usr/www/vitanut/new.txt");
>My script lives in:
>/usr/www/vitanut/htdocs/cgibin/
>
>Why doesn't the above code work? My file permissions are all correct.
>
>Any help will be greatly appreciated.
>
>
>Sent via Deja.com http://www.deja.com/
>Share what you know. Learn what you don't.
If you are calling the script directly via the browser, your identity
might be "nobody" and thus you aren't allowed to rename any files.
Check if your server requires that you to call such scripts with
cgiwrap and if so, what the proper url you should use.
e.g. http://www.domain.com/cgi-bin/cgiwrap/username/script.cgi
Jason Q.
http://www.generationterrorists.com
------------------------------
Date: Sat, 26 Jun 1999 10:31:10 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Unix CGI > Win/Mac
Message-Id: <Pine.GSO.4.02A.9906260945060.26514-100000@user2.teleport.com>
On Sat, 26 Jun 1999, Thomas Fischer wrote:
> You think this is right(?):
>
> if ($PLATFORM eq "WIN") {$SOURCE =~ s/\n/\r\n/g;}
> if ($PLATFORM eq "MAC") {$SOURCE =~ s/\n/\r/g;}
>
> I wonder why this doesn't work!??!?!?!
Maybe it does work. Just for testing, just during development, have your
program write the generated $SOURCE into a (world-writable) file on your
machine before sending it out. Then, when you examine that file with 'od
-xc file' or similar, do you see the right stuff in it?
If that file is correct, it means that some browser (or maybe server or
proxy or...) isn't doing what you think it's doing. But maybe it's simply
following the HTTP/1.1 standard,
http://www.cis.ohio-state.edu/htbin/rfc/rfc2068.html
which says in section 3.7.1:
When in canonical form, media subtypes of the "text" type use CRLF as
the text line break. HTTP relaxes this requirement and allows the
transport of text media with plain CR or LF alone representing a line
break when it is done consistently for an entire entity-body. HTTP
applications MUST accept CRLF, bare CR, and bare LF as being
representative of a line break in text media received via HTTP.
I may be misreading this or reading it out of context. But it seems to be
saying to me that, whichever species of line ends your program sends, your
browser should accept it. Perhaps the server, a proxy, or the browser is
making your line ends into CRLFs.
IMHO, a browser which lets you copy text should convert the line ends
automatically for the platform it's on. This seems to be what some
browsers are doing in my (limited) testing.
Cheers!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Sat, 26 Jun 1999 16:13:46 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: validating a regexp from a CGI form
Message-Id: <3774fb24.276547@news.skynet.be>
Randal L. Schwartz wrote:
>Bart> B) Delay the compilation of the regex. Hey, I might throw in closures as
>Bart> well. :-)
>
>Bart> sub regexsub ($) {
>Bart> my($regex) = @_;
>Bart> return eval "sub { /\$regex/o }";
>Bart> }
>
>Yes, this looks extremely familiar. :)
>[See Effective Perl Programming.]
It also won't work, I guess. As the compilation of the regex is delayed
until the first time the generated sub is ran, syntax checking is
delayed until then, too. So it can still crash at the inappropriate
place.
my $foo = regexsub('fo(+') or die "Couldn't compile \$foo";
while(<DATA>) {
$foo->() and print "Match in $_";
}
-->
/fo(+/: ?+*{} follows nothing in regexp at (eval 1) line 1, <DATA> chunk
1.
Bart.
------------------------------
Date: 26 Jun 1999 10:24:29 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Very easy integer question
Message-Id: <3774febd@cs.colorado.edu>
[courtesy cc of this posting mailed to cited author]
Cursed to torture himself and Usenet with Mozilla 3.01 (Win95; I),
Cal Bond <cal@spacemoose.com> writes in comp.lang.perl.misc:
:Could anyone tell me the command/prefix or whatever to get a number from
:a calculation to be calculated to 2 decimal places. Ive just cut and
:pasted bits of code in so don't know how to actually change this myself,
:right now Ive got
:$blah=int($x/$y)
:but I'd like it to go to 2 decimals. Thanks lots if you can help me out
Two choices:
1) Hire a programmer.
2) RTFFAQ that's waiting for you on your very own computer.
Up to you.
NAME
perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date:
1999/05/23 20:37:49 $)
DESCRIPTION
The section of the FAQ answers question related to the
manipulation of data as numbers, dates, strings, arrays, hashes,
and miscellaneous data issues.
Data: Numbers
Why am I getting long decimals (eg, 19.9499999999999) instead of the
numbers I should be getting (eg, 19.95)?
The infinite set that a mathematician thinks of as the real
numbers can only be approximate on a computer, since the
computer only has a finite number of bits to store an infinite
number of, um, numbers.
Internally, your computer represents floating-point numbers in
binary. Floating-point numbers read in from a file or appearing
as literals in your program are converted from their decimal
floating-point representation (eg, 19.95) to the internal binary
representation.
However, 19.95 can't be precisely represented as a binary
floating-point number, just like 1/3 can't be exactly
represented as a decimal floating-point number. The computer's
binary representation of 19.95, therefore, isn't exactly 19.95.
When a floating-point number gets printed, the binary floating-
point representation is converted back to decimal. These decimal
numbers are displayed in either the format you specify with
printf(), or the current output format for numbers (see the
section on "$#" in the perlvar manpage if you use print. `$#'
has a different default value in Perl5 than it did in Perl4.
Changing `$#' yourself is deprecated.
This affects all computer languages that represent decimal
floating-point numbers in binary, not just Perl. Perl provides
arbitrary-precision decimal numbers with the Math::BigFloat
module (part of the standard Perl distribution), but
mathematical operations are consequently slower.
To get rid of the superfluous digits, just use a format (eg,
`printf("%.2f", 19.95)') to get the required precision. See the
section on "Floating-point Arithmetic" in the perlop manpage.
Why isn't my octal data interpreted correctly?
Perl only understands octal and hex numbers as such when they
occur as literals in your program. If they are read in from
somewhere and assigned, no automatic conversion takes place. You
must explicitly use oct() or hex() if you want the values
converted. oct() interprets both hex ("0x350") numbers and octal
ones ("0350" or even without the leading "0", like "377"), while
hex() only converts hexadecimal ones, with or without a leading
"0x", like "0x255", "3A", "ff", or "deadbeef".
This problem shows up most often when people try using chmod(),
mkdir(), umask(), or sysopen(), which all want permissions in
octal.
chmod(644, $file); # WRONG -- perl -w catches this
chmod(0644, $file); # right
Does Perl have a round() function? What about ceil() and floor()? Trig functions?
Remember that int() merely truncates toward 0. For rounding to a
certain number of digits, sprintf() or printf() is usually the
easiest route.
printf("%.3f", 3.1415926535); # prints 3.142
The POSIX module (part of the standard perl distribution)
implements ceil(), floor(), and a number of other mathematical
and trigonometric functions.
use POSIX;
$ceil = ceil(3.5); # 4
$floor = floor(3.5); # 3
In 5.000 to 5.003 Perls, trigonometry was done in the
Math::Complex module. With 5.004, the Math::Trig module (part of
the standard perl distribution) implements the trigonometric
functions. Internally it uses the Math::Complex module and some
functions can break out from the real axis into the complex
plane, for example the inverse sine of 2.
Rounding in financial applications can have serious
implications, and the rounding method used should be specified
precisely. In these cases, it probably pays not to trust
whichever system rounding is being used by Perl, but to instead
implement the rounding function you need yourself.
To see why, notice how you'll still have an issue on half-way-
point alternation:
for ($i = 0; $i < 1.01; $i += 0.05) { printf "%.1f ",$i}
0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7
0.8 0.8 0.9 0.9 1.0 1.0
Don't blame Perl. It's the same as in C. IEEE says we have to do
this. Perl numbers whose absolute values are integers under
2**31 (on 32 bit machines) will work pretty much like
mathematical integers. Other numbers are not guaranteed.
etc
--
GNU Emacs is a LISP operating system disguised as a word processor.
--Doug Mohney, in comp.arch
------------------------------
Date: Sat, 26 Jun 1999 07:37:54 -0600
From: John Thompson <John.Thompson@ibm.net>
Subject: Re: Viral matters [completely off-topic]
Message-Id: <3774D7B2.C8C80DB7@ibm.net>
Dan Carson wrote:
>
> On 24 Jun 1999, Dale Henderson wrote:
>
> > I am very aware of why there are few to no virus written for Unix
> > systems. It is because it would be impractical. As I've said
> > before, the only way to do any real damage on a Unix box is with
> > root access. And the practicality of hacking root in a virus is
> > nil.
>
> Unfortunately, "hacking root" is getting more practical all the time. For
> some reason, some people want Linux to "compete" with Microsoft. So in the
> interest of making Linux "easy to use", the Standard Operating Procedure
> is becoming:
>
> 1. Download anything you see on the net with a '.rpm' extension.
>
> 2. su root.
>
> 3. Type rpm -whatever.
>
> 4. Voila! Something happened! I have no idea what, but whatever it was,
> it ran as root!
>
> Now maybe /bin/ls is suid root and does something in addition to the
> standard functionality.
>
> Am I just being paranoid?
That's why many people use pgp signatures with the rpm's
they build.
--
-John (John.Thompson@ibm.net)
------------------------------
Date: Sat, 26 Jun 1999 16:05:36 GMT
From: luvencl@my-deja.com
Subject: wwwboard on webjump.com
Message-Id: <7l2tob$2dn$1@nnrp1.deja.com>
I am looking for help with placing wwwboard on the webjump.com server.
I followed all instructions, but I get:
CGI Script Error
An error has occurred in processing your CGI script.
This indicates the error is not the result of a server malfunction.
Please review your CGI scripts.
Anyone have any experience with this?
Your help would be appreciated..
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
------------------------------
Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>
Administrivia:
Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing.
]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body. Majordomo will then send you instructions on how to confirm your
]subscription. This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.
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.misc (and this Digest), send your
article to perl-users@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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 6136
**************************************