[11334] in Perl-Users-Digest
Perl-Users Digest, Issue: 4934 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Feb 18 19:17:16 1999
Date: Thu, 18 Feb 99 16:00:26 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 18 Feb 1999 Volume: 8 Number: 4934
Today's topics:
Re: (blush) Stupid question about Perl in Win/DOS (shud (Larry Rosler)
Re: (blush) Stupid question about Perl in Win/DOS (shud (Steve Linberg)
Re: (blush) Stupid question about Perl in Win/DOS (shud <draeger@NOSPAM4ME.uiuc.edu>
Re: (blush) Stupid question about Perl in Win/DOS (shud (Larry Rosler)
Re: (blush) Stupid question about Perl in Win/DOS (shud (Steve Linberg)
Are there any distance locators? <poohba@io.com>
Re: DEMO: new qr// operator <tchrist@mox.perl.com>
Re: DOC: perlopentut.pod for 5.005_55 (Bart Lateur)
efficient , -> TAB substitution? <trent@mail.utexas.edu>
Re: efficient , -> TAB substitution? <stampes@xilinx.com>
Re: Escaping characters (Tad McClellan)
Re: FAQ 5.17: How can I lock a file? <tchrist@mox.perl.com>
Re: FAQ 5.25: How can I read a single character from a <dbc@dbc-pc.tc.fluke.com>
FAQ 5.26: How can I tell whether there's a character wa <perlfaq-suggestions@perl.com>
FAQ 5.28: How do I dup() a filehandle in Perl? <perlfaq-suggestions@perl.com>
FAQ 5.29: How do I close a file descriptor by number? <perlfaq-suggestions@perl.com>
Re: How do you format decimal numbers in Perl? <tchrist@mox.perl.com>
Re: howto read element from string.. (Tad McClellan)
Re: Matching for a null string <aqumsieh@matrox.com>
Re: Perl install on Solaris 2.6 <gellyfish@btinternet.com>
Re: Perl install on Solaris 2.6 <uri@ibnets.com>
Perl Installation Help Resource? ( Trevor Stone )@( )
Re: perl not always in /usr/bin/perl <gellyfish@btinternet.com>
Perl socket question biglife99@my-dejanews.com
Problems Executing Script from web. <rabinvp@nortelnetworks.com>
speed <haiti@stc.com>
Re: String concatenation, differences (Tad McClellan)
Urgent String Manip. Question ? <nrd1mmc@nrd.ups.com>
Re: Variable for variable names (Tad McClellan)
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 18 Feb 1999 14:00:30 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: (blush) Stupid question about Perl in Win/DOS (shudder)
Message-Id: <MPG.11362d59230c040b989a5a@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <linberg-1802991625100001@ltl1.literacy.upenn.edu> on Thu, 18
Feb 1999 16:25:10 -0500, Steve Linberg <linberg@literacy.upenn.edu>
says...
> In article <36CC7853.AE554FB2@NOSPAM4ME.uiuc.edu>, Erik Draeger
> <draeger@NOSPAM4ME.uiuc.edu> wrote:
>
> > P.S. Also, if someone could tell me how to specify a drive letter in a
> > path for Win32 Perl, that'd be super. I read in a faq somewhere that
> > you just use //, but it doesn't seem to work.
>
> Backslashes, not forward slashes.
>
> "c:\\foo\\bar\\baz.exe" or 'c:\foo\bar\baz.exe'
Oh, come on, now! Forward slashes work just as well, are easier to
write and to read, and are more portable. Just because they don't work
for the stupid so-called command interpreter doesn't mean they don't
work fine in Perl.
> > system("c://windows/calc.exe"); <---- doesn't.
The trouble with this is the double forward slash. With just one
forward slash, it works fine -- with either double-quotes or single-
quotes.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Thu, 18 Feb 1999 17:41:21 -0500
From: linberg@literacy.upenn.edu (Steve Linberg)
Subject: Re: (blush) Stupid question about Perl in Win/DOS (shudder)
Message-Id: <linberg-1802991741210001@ltl1.literacy.upenn.edu>
In article <MPG.11362d59230c040b989a5a@nntp.hpl.hp.com>, lr@hpl.hp.com
(Larry Rosler) wrote:
> Oh, come on, now! Forward slashes work just as well, are easier to
> write and to read, and are more portable. Just because they don't work
> for the stupid so-called command interpreter doesn't mean they don't
> work fine in Perl.
Jeez, Larry, relax a little! If what I wrote is wrong, then it's wrong;
just point it out, lessons learned all around. I recalled having a
similar problem when I was doing win32 perl a while back, and that was a
fix for my problem. The syntax I posted works fine, even if it wasn't to
your stylistic liking.
Forward slashes *are* more portable (within Perl), but "c:\" isn't
portable by any stretch of the imagination, right? So portability is
already out the window when using this type of construct.
> > > system("c://windows/calc.exe"); <---- doesn't.
>
> The trouble with this is the double forward slash. With just one
> forward slash, it works fine -- with either double-quotes or single-
> quotes.
Right, but with backslashes and double-quotes, you double the
backslashes. Either way works fine. Are forward slashes better to use
for directory seperators under Perl in Win32? Perhaps, because Perl/Win32
translates them to backslashes. Will forward-slashes work in DOS?
Outside the Perl environment in Win95/98 (like C programs)? Type "dir
c:/" in a Win95 DOS window and see what you get. Forward-slashes aren't
very portable in that context, so pick your side of the coin. What do you
want to get used to, Perl or your OS? Either answer is valid under
different circumstances.
There are times when it makes sense to write code that isn't OS-portable -
like when you're referring to system-specific binaries. If I write some
mac-specific Perl to access Toolbox routines and use MacPerl libraries
(like listing available 'FONT' resources in the System file), I'm not
going to go too far out of my way to make it portable, because by nature
it's OS-specific.
As an aside, let me repeat that I respect your knowledge and auto-hilight
your posts so that I may study and learn from them -- and I've been
programming for 20 years. Let's be polite unless there's a reason not to
be, OK? We're all here to learn, help, and improve.
--
Steve Linberg, Systems Programmer &c.
National Center on Adult Literacy, University of Pennsylvania
email: <linberg@literacy.upenn.edu>
WWW: <http://www.literacyonline.org>
------------------------------
Date: Thu, 18 Feb 1999 17:09:24 -0600
From: Erik Draeger <draeger@NOSPAM4ME.uiuc.edu>
Subject: Re: (blush) Stupid question about Perl in Win/DOS (shudder)
Message-Id: <36CC9DA4.D2C60F62@NOSPAM4ME.uiuc.edu>
Ahem.
Thank you for the single-slash resolution. I think the thing I read said to
use a double backslash (e.g. c:\\windows), but I didn't notice that it was of
course as an escape character in the double-quotes, so I promptly used single
backslashes for the rest of the directory name, which of course didn't work.
Any ideas about the \n?
Erik
Steve Linberg wrote:
> In article <MPG.11362d59230c040b989a5a@nntp.hpl.hp.com>, lr@hpl.hp.com
> (Larry Rosler) wrote:
>
> > Oh, come on, now! Forward slashes work just as well, are easier to
> > write and to read, and are more portable. Just because they don't work
> > for the stupid so-called command interpreter doesn't mean they don't
> > work fine in Perl.
>
> Jeez, Larry, relax a little! If what I wrote is wrong, then it's wrong;
> just point it out, lessons learned all around. I recalled having a
> similar problem when I was doing win32 perl a while back, and that was a
> fix for my problem. The syntax I posted works fine, even if it wasn't to
> your stylistic liking.
>
> Forward slashes *are* more portable (within Perl), but "c:\" isn't
> portable by any stretch of the imagination, right? So portability is
> already out the window when using this type of construct.
>
> > > > system("c://windows/calc.exe"); <---- doesn't.
> >
> > The trouble with this is the double forward slash. With just one
> > forward slash, it works fine -- with either double-quotes or single-
> > quotes.
>
> Right, but with backslashes and double-quotes, you double the
> backslashes. Either way works fine. Are forward slashes better to use
> for directory seperators under Perl in Win32? Perhaps, because Perl/Win32
> translates them to backslashes. Will forward-slashes work in DOS?
> Outside the Perl environment in Win95/98 (like C programs)? Type "dir
> c:/" in a Win95 DOS window and see what you get. Forward-slashes aren't
> very portable in that context, so pick your side of the coin. What do you
> want to get used to, Perl or your OS? Either answer is valid under
> different circumstances.
>
> There are times when it makes sense to write code that isn't OS-portable -
> like when you're referring to system-specific binaries. If I write some
> mac-specific Perl to access Toolbox routines and use MacPerl libraries
> (like listing available 'FONT' resources in the System file), I'm not
> going to go too far out of my way to make it portable, because by nature
> it's OS-specific.
>
> As an aside, let me repeat that I respect your knowledge and auto-hilight
> your posts so that I may study and learn from them -- and I've been
> programming for 20 years. Let's be polite unless there's a reason not to
> be, OK? We're all here to learn, help, and improve.
>
> --
> Steve Linberg, Systems Programmer &c.
> National Center on Adult Literacy, University of Pennsylvania
> email: <linberg@literacy.upenn.edu>
> WWW: <http://www.literacyonline.org>
------------------------------
Date: Thu, 18 Feb 1999 15:32:04 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: (blush) Stupid question about Perl in Win/DOS (shudder)
Message-Id: <MPG.113642d13fa4e402989a5b@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <linberg-1802991741210001@ltl1.literacy.upenn.edu> on Thu, 18
Feb 1999 17:41:21 -0500, Steve Linberg <linberg@literacy.upenn.edu>
says...
> In article <MPG.11362d59230c040b989a5a@nntp.hpl.hp.com>, lr@hpl.hp.com
> (Larry Rosler) wrote:
>
> > Oh, come on, now! Forward slashes work just as well, are easier to
> > write and to read, and are more portable. Just because they don't work
> > for the stupid so-called command interpreter doesn't mean they don't
> > work fine in Perl.
>
> Jeez, Larry, relax a little! If what I wrote is wrong, then it's wrong;
> just point it out, lessons learned all around. I recalled having a
> similar problem when I was doing win32 perl a while back, and that was a
> fix for my problem. The syntax I posted works fine, even if it wasn't to
> your stylistic liking.
I didn't imply that your answer was wrong, just that it was dogmatic and
incomplete. We do not want readers to get the impression that
Windows/DOS path names *must* or *should* use backslashes. Quite the
contrary.
...<SNIP> extensive useful analysis of portability of Perl programs
> As an aside, let me repeat that I respect your knowledge and auto-hilight
> your posts so that I may study and learn from them -- and I've been
> programming for 20 years. Let's be polite unless there's a reason not to
> be, OK? We're all here to learn, help, and improve.
Thank you for the kind words. I did not intend to be impolite, and I
regret that you took it that way.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Thu, 18 Feb 1999 23:44:43 GMT
From: slinberg@crocker.com (Steve Linberg)
Subject: Re: (blush) Stupid question about Perl in Win/DOS (shudder)
Message-Id: <slinberg-1802991844450001@cc11620-a.lwmrn1.pa.home.com>
In article <36CC9DA4.D2C60F62@NOSPAM4ME.uiuc.edu>, Erik Draeger
<draeger@NOSPAM4ME.uiuc.edu> wrote:
> Any ideas about the \n?
Oh, you want actual solutions? :)
I haven't tried this and I'm not a DOS expert, but I'd guess that you
could stick raw CRLF's in there. DOS doesn't understand "\n", but use
Perl to stuff real \n's into your command string. How about something
like this:
$result = system(qq(echo "line1\nline2\nline3\n" | foo.exe));
Or even
$result = system(qq(echo "line1\cJ\cMline2\cJ\cMline3\cJ\cM" | foo.exe));
I don't have a DOS system with Perl installed handy, so this is an
untested guess. There may be other system-specific stuff you can do, I
don't know how echo works. (Heck, I didn't even know you could pipe like
that in DOS.)
------------------------------
Date: Thu, 18 Feb 1999 17:33:15 -0500
From: Poohba <poohba@io.com>
Subject: Are there any distance locators?
Message-Id: <Pine.BSF.4.05.9902181732380.28205-100000@dillinger-2.io.com>
I am looking for a distance locator. If anyone has any info on this
please drop me a line.
* Web Page Designs *
/ poohba@io.com | www.io.com/~poohba\
---------------------------------------
\ For info about me send message with /
* subject "send file help" *
------------------------------
Date: 18 Feb 1999 14:02:10 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: DEMO: new qr// operator
Message-Id: <36cc7fd2@csnews>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
ilya@math.ohio-state.edu (Ilya Zakharevich) writes:
:And I repeat: there should be no need to use qr// in such simple
:contexts.
:
:*should* cache the results of compilation - behind the scenes (say, to
:speed up old programs, *and* to simplify new ones). A 3-line patch
:which implemented this is available on p5p archives.
That's completely irrelevant. That's not real perl. That's a private
little hack that you scribbled out. It's not in the standard release,
nor in the release qui venit. When it comes into the release, then we
can discuss the issue.
Hope this helps,
--tom
--
I've got plenty of inputs and outputs. I don't need the video. --Andrew Hume
------------------------------
Date: Thu, 18 Feb 1999 23:08:40 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: DOC: perlopentut.pod for 5.005_55
Message-Id: <36ce9d20.30984510@news.skynet.be>
Tom Christiansen wrote:
>>Even DOS boxes have seriously modern Perl ports now!
>
>Then they should get seriously modern filesystems.
The DJGPP people will be glad to hear that.
I prefer using the DOS filename system to depending on that ugly Win95
patch any time.
Bart.
------------------------------
Date: Thu, 18 Feb 1999 15:55:06 -0600
From: Robert D Trent <trent@mail.utexas.edu>
Subject: efficient , -> TAB substitution?
Message-Id: <Pine.GSO.3.96.990218154423.29401A-100000@piglet.cc.utexas.edu>
I've got these ~90MB files that contain comma-separated
values each surrounded by double quotes. For example:
"VAL1","VAL2","VAL3","VAL4","VAL5"
"VAL6","VAL7","VAL8","VAL9","VAL0"
...
I need the values to be TAB-separated instead. I have
written the following C-shell script to do the substitution
but it takes almost 90 minutes to complete.
#!/bin/csh
sed -n -e 's/","/" "/g' -e 'p' $argv[1] > $argv[2]
Can perl do this quicker? How?
--
Robert D Trent
trent@mail.utexas.edu
------------------------------
Date: 18 Feb 1999 22:43:27 GMT
From: Jeff Stampes <stampes@xilinx.com>
Subject: Re: efficient , -> TAB substitution?
Message-Id: <7ai52f$7q1@courier.xilinx.com>
Robert D Trent <trent@mail.utexas.edu> wrote:
: I've got these ~90MB files that contain comma-separated
: values each surrounded by double quotes. For example:
: "VAL1","VAL2","VAL3","VAL4","VAL5"
: "VAL6","VAL7","VAL8","VAL9","VAL0"
: ...
: I need the values to be TAB-separated instead. I have
: written the following C-shell script to do the substitution
: but it takes almost 90 minutes to complete.
: #!/bin/csh
: sed -n -e 's/","/" "/g' -e 'p' $argv[1] > $argv[2]
perl -pi.bak -e 's/,/\t/go' filename
--
Jeff Stampes -- Xilinx, Inc. -- Boulder, CO -- jeff.stampes@xilinx.com
------------------------------
Date: Thu, 18 Feb 1999 17:16:25 -0600
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Escaping characters
Message-Id: <907ia7.nue.ln@magna.metronet.com>
Patrick Fong (patfong@yoyo.cc.monash.edu.au) wrote:
: Was wondering if someone could tell me more about escaping characters.
: Well I think that is related to my problem
: the following line gives me errors..
: print FIND " <TR VALIGN=top ALIGN=center><TD
: BGCOLOR=#cccc99>$FORM{'filename'}<\/TD><TD
: BGCOLOR=#cccc99>$shortdate<\/TD>";
: all of them are on one line of course. We have to escape things like
: forward/backward slashes... fullstops... commas... what else?? And why?
You do not have to escape ANY of the characters you mention
there, except for backslash.
Inside of double quoted strings the _only_ characters you
should ever need to escape are:
"
\
$
@
That's it! (unless I forgot something, which is entirely possible)
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 18 Feb 1999 14:06:04 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: FAQ 5.17: How can I lock a file?
Message-Id: <36cc80bc@csnews>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
ilya@math.ohio-state.edu (Ilya Zakharevich) writes:
: 4 On some systems locking files is advisory only.
Perl locking is defined to be advisory only. This is obviously more
powerful, because with advisory locking you can implement things that with
mandatory locking you cannot. Therefore, the most flexible, lowest level
mechanism is what should be used to construct higher level mechanisms.
You should ignore those aberrant systems that use mandatory locking if
you care to be portable. Think about it. If you assume advisory and
it's not, you are merely annoyed and inconvenienced by some idiot making
your decisions for you, but nothing is broken. If you go the other way,
you're hosed. Therefore, ignore mandatory locking.
Don't you have something else to do, Ilya, like putting out CP/M-99?
--tom
--
"Twisted cleverness is my only skill as a programmer." --Elizabeth Zwicky
------------------------------
Date: Thu, 18 Feb 1999 15:28:18 -0800
From: Dan Carson <dbc@dbc-pc.tc.fluke.com>
Subject: Re: FAQ 5.25: How can I read a single character from a file? From the keyboard?
Message-Id: <Pine.LNX.4.00.9902181527180.22635-100000@dbc-pc.tc.fluke.com>
On 18 Feb 1999, Tom Christiansen wrote:
> (This excerpt from perlfaq5 - Files and Formats
> ($Revision: 1.34 $, $Date: 1999/01/08 05:46:13 $)
.......
>
> How can I read a single character from a file? From the keyboard?
>
.......
>
> For legacy DOS systems, Dan Carson <dbc@tc.fluke.COM> reports the
> following:
Since my name is attached to what follows, I should point out that this
is very old data. This is something that worked with a particular DOS
port of perl4 circa 1991, if I remember right. I don't think ioctl() is
supported in the currently popular builds. At least it isn't in the NT
version that's on some machines here. I haven't paid any attention to
this stuff since I started using Linux. Anybody know a better answer to
put in this spot?
-Dan
------------------------------
Date: 18 Feb 1999 13:27:15 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 5.26: How can I tell whether there's a character waiting on a filehandle?
Message-Id: <36cc77a3@csnews>
(This excerpt from perlfaq5 - Files and Formats
($Revision: 1.34 $, $Date: 1999/01/08 05:46:13 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq5.html
if your negligent system adminstrator has been remiss in his duties.)
How can I tell whether there's a character waiting on a filehandle?
The very first thing you should do is look into getting the
Term::ReadKey extension from CPAN. As we mentioned earlier, it now even
has limited support for non-portable (read: not open systems, closed,
proprietary, not POSIX, not Unix, etc) systems.
You should also check out the Frequently Asked Questions list in
comp.unix.* for things like this: the answer is essentially the same.
It's very system dependent. Here's one solution that works on BSD
systems:
sub key_ready {
my($rin, $nfd);
vec($rin, fileno(STDIN), 1) = 1;
return $nfd = select($rin,undef,undef,0);
}
If you want to find out how many characters are waiting, there's also
the FIONREAD ioctl call to be looked at. The *h2ph* tool that comes with
Perl tries to convert C include files to Perl code, which can be
`require'd. FIONREAD ends up defined as a function in the *sys/ioctl.ph*
file:
require 'sys/ioctl.ph';
$size = pack("L", 0);
ioctl(FH, FIONREAD(), $size) or die "Couldn't call ioctl: $!\n";
$size = unpack("L", $size);
If *h2ph* wasn't installed or doesn't work for you, you can *grep* the
include files by hand:
% grep FIONREAD /usr/include/*/*
/usr/include/asm/ioctls.h:#define FIONREAD 0x541B
Or write a small C program using the editor of champions:
% cat > fionread.c
#include <sys/ioctl.h>
main() {
printf("%#08x\n", FIONREAD);
}
^D
% cc -o fionread fionread.c
% ./fionread
0x4004667f
And then hard-code it, leaving porting as an exercise to your successor.
$FIONREAD = 0x4004667f; # XXX: opsys dependent
$size = pack("L", 0);
ioctl(FH, $FIONREAD, $size) or die "Couldn't call ioctl: $!\n";
$size = unpack("L", $size);
FIONREAD requires a filehandle connected to a stream, meaning sockets,
pipes, and tty devices work, but *not* files.
--
if (tmpstab == envstab) /* heavy wizardry going on here */
str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
/* he threw the brick up into the air */
------------------------------
Date: 18 Feb 1999 15:27:26 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 5.28: How do I dup() a filehandle in Perl?
Message-Id: <36cc93ce@csnews>
(This excerpt from perlfaq5 - Files and Formats
($Revision: 1.34 $, $Date: 1999/01/08 05:46:13 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq5.html
if your negligent system adminstrator has been remiss in his duties.)
How do I dup() a filehandle in Perl?
If you check the "open" entry in the perlfunc manpage, you'll see that
several of the ways to call open() should do the trick. For example:
open(LOG, ">>/tmp/logfile");
open(STDERR, ">&LOG");
Or even with a literal numeric descriptor:
$fd = $ENV{MHCONTEXTFD};
open(MHCONTEXT, "<&=$fd"); # like fdopen(3S)
Note that "<&STDIN" makes a copy, but "<&=STDIN" make an alias. That
means if you close an aliased handle, all aliases become inaccessible.
This is not true with a copied one.
Error checking, as always, has been left as an exercise for the reader.
--
Fine, let them give me the endless crap. --Andrew Hume
------------------------------
Date: 18 Feb 1999 16:27:31 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 5.29: How do I close a file descriptor by number?
Message-Id: <36cca1e3@csnews>
(This excerpt from perlfaq5 - Files and Formats
($Revision: 1.34 $, $Date: 1999/01/08 05:46:13 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq5.html
if your negligent system adminstrator has been remiss in his duties.)
How do I close a file descriptor by number?
This should rarely be necessary, as the Perl close() function is to be
used for things that Perl opened itself, even if it was a dup of a
numeric descriptor, as with MHCONTEXT above. But if you really have to,
you may be able to do this:
require 'sys/syscall.ph';
$rc = syscall(&SYS_close, $fd + 0); # must force numeric
die "can't sysclose $fd: $!" unless $rc == -1;
--
An Inteligent terminal is not a smart-ass terminal; it is one you can educate.
--Rob Pike
------------------------------
Date: 18 Feb 1999 14:16:08 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: How do you format decimal numbers in Perl?
Message-Id: <36cc8318@csnews>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, Craig <craig@skybound.demon.nl> writes:
:I'm a Windows programmer and don't know much about Perl.
:I'ld like to know how you format a number so that you always have a
:fixed number of decimal places.
:Example:
:
: 4.5 -> 4.50
:
:P.S. I use Windows (and have no "man" stuff).
NO NO NO!
That's a horrible misunderstanding. All perl systems come with complete
documentation. What real programmers call manpages often appear in
other formats on consumer-oriented systems, but they are still there.
If you have Perl, you have the documentation. If you do not have the
documentation, then you do not have Perl. Sometimes this is
installed as HTML. Sometimes it's as pod, and you just have
to run it through pod2text to view it. But it's there. Just because
you're on a crippled systems doesn't mean that you have to be crippled,
too. We've tried to help you.
In this case, you have negligent about overlooking the perlfaq4
documentation. Or just about any example programs. I include
here the relevant sections of the manpages.
Hope this helps.
=head1 NAME
perlfaq4 - Data Manipulation ($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
=head1 DESCRIPTION
The section of the FAQ answers question related to the manipulation
of data as numbers, dates, strings, arrays, hashes, and miscellaneous
data issues.
=head1 Data: Numbers
=head2 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 L<perlvar/"$#"> if you use
print. C<$#> has a different default value in Perl5 than it did in
Perl4. Changing C<$#> yourself is deprecated.
This affects B<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,
C<printf("%.2f", 19.95)>) to get the required precision.
See L<perlop/"Floating-point Arithmetic">.
...
=head2 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.
--
_doprnt(pat, args, &fakebuf); /* what a kludge */
--Larry Wall, from util.c in the v5.0 perl distribution
------------------------------
Date: Thu, 18 Feb 1999 15:39:28 -0600
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: howto read element from string..
Message-Id: <ga1ia7.oae.ln@magna.metronet.com>
Chris Oudeman (c.m.oudeman@st.hanze.nl) wrote:
: Its a simple prob but can't seem to find a solution.
: How to read an element from a string
foreach my $char ( split //, $string ) {
print "the char was '$char'\n";
}
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 18 Feb 1999 13:57:16 -0500
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: Matching for a null string
Message-Id: <x3ysoc3pmk4.fsf@tigre.matrox.com>
"David W. Hundley" <dave.hundley@eds.exsc01.exch.com> writes:
> Anyone know how
> to either match for a null string or determine that a string is
> definitely not null?
If by a "null" string you mean an "empty" string (ie a string with
whitespaces only), you can match it like so"
if ($string =~ /^\s*$/) { .. };
If by "null" you mean an undefined string, then you can test it like
so:
if (defined $string) { ... };
Ala
------------------------------
Date: 18 Feb 1999 21:58:02 -0000
From: Jonathan Stowe <gellyfish@btinternet.com>
Subject: Re: Perl install on Solaris 2.6
Message-Id: <7ai2da$lh$1@gellyfish.btinternet.com>
On Wed, 17 Feb 1999 19:10:59 GMT John P. Crisci wrote:
>
> I am installing Perl on Solaris 2.6. We have the SUN C compiler on this
> system - not GNU C. In the Generic nots section, it states Do not use
> /usr/ucb/cc.
>
> This is where the C compiler is. Was there a reason why this statement
> was added intothe hints file?
>
It was added because as far as I know that compiler is not ANSI compliant
a requirement to install recent Perls.
The only Sun supplied compiler that I have found to work correctly is the
'c89' compiler that comes with the SunWSPro package.
If you dont have that compiler then you are probably better off getting
hold of gcc - a prebuilt binary of which is available from a variety of
sources.
/J\
--
Jonathan Stowe <jns@btinternet.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: 18 Feb 1999 17:54:09 -0500
From: Uri Guttman <uri@ibnets.com>
Subject: Re: Perl install on Solaris 2.6
Message-Id: <39g183uxv2.fsf@ibnets.com>
>>>>> "JS" == Jonathan Stowe <gellyfish@btinternet.com> writes:
JS> On Wed, 17 Feb 1999 19:10:59 GMT John P. Crisci wrote:
>>
>> I am installing Perl on Solaris 2.6. We have the SUN C compiler on this
>> system - not GNU C. In the Generic nots section, it states Do not use
>> /usr/ucb/cc.
>>
>> This is where the C compiler is. Was there a reason why this statement
>> was added intothe hints file?
>>
that is not a full c compiler. it is a front end for sun's pro compiler
that uses bsd semantics (different libraries, paths, etc.). if that is
all you have, then you can't compile anything. you need either gcc (or
egcs) or buy it from sun or other third party.
if it does compile something, then you have a solaris compiler which
should work with perl. just don't put /usr/ucb in your path before the
other compiler.
JS> It was added because as far as I know that compiler is not ANSI
JS> compliant a requirement to install recent Perls.
it doesn't even compile anything anyhow so its ansi-ness is moot.
uri
--
Uri Guttman Hacking Perl for Ironbridge Networks
uri@sysarch.com uri@ironbridgenetworks.com
------------------------------
Date: Thu, 18 Feb 1999 22:11:16 GMT
From: tstone( Trevor Stone )@( )nyx.net
Subject: Perl Installation Help Resource?
Message-Id: <919375876.47890@iris.nyx.net>
I know that the range of errors cannot hope to be covered in a help document,
but some common ones might. Specifically, the makefile seems to imply that
miniperl often produces some problems. Is there some resource which helps
these sorts of problems, or am I completely on my own?
Specifically, when I run make, the following fun-filled errors show up:
cc -L/usr/local/lib -o miniperl miniperlmain.o libperl.a
libperl.a(pp.o): In function `Perl_pp_pow':
pp.o(.text+0x183e): undefined reference to `pow'
libperl.a(pp.o): In function `Perl_pp_atan2':
pp.o(.text+0x4036): undefined reference to `atan2'
libperl.a(pp.o): In function `Perl_pp_sin':
pp.o(.text+0x40e0): undefined reference to `sin'
libperl.a(pp.o): In function `Perl_pp_cos':
pp.o(.text+0x41ac): undefined reference to `cos'
libperl.a(pp.o): In function `Perl_pp_exp':
pp.o(.text+0x4484): undefined reference to `exp'
libperl.a(pp.o): In function `Perl_pp_log':
pp.o(.text+0x4591): undefined reference to `log'
libperl.a(pp.o): In function `Perl_pp_sqrt':
pp.o(.text+0x46a9): undefined reference to `sqrt'
libperl.a(pp.o): In function `Perl_pp_pack':
pp.o(.text+0x97a0): undefined reference to `floor'
pp.o(.text+0x9916): undefined reference to `floor'
make: *** [miniperl] Error 1
I'm compiling perl5.005_02 on Linux 2.0.12 (yeah, I know it's old). I've used
my mediocre C-hacking skills to determine that all syntax in pp.c compares with
that in math.h. I tried a few different Configure options, but always this
error set.
Any ideas on how I can deal with this? Is there a process I should follow in
dealing with such errors? Is there a better place to query?
Much obliged,
=-=-=-=-= Trevor Stone =-=-= a.k.a. Flwyd =-=-= tstone @ nyx . net =-=-=-=-=
Computer science, eclectic philosophy, games, wits, esotericism, weird hats.
http://robin.ml.org/~tstone/ Thou rank white-livered gudgeon!
Don't confuse me stopping for breath with asking for your opinion. -R. Green
------------------------------
Date: 18 Feb 1999 22:01:26 -0000
From: Jonathan Stowe <gellyfish@btinternet.com>
Subject: Re: perl not always in /usr/bin/perl
Message-Id: <7ai2jm$lm$1@gellyfish.btinternet.com>
On 16 Feb 1999 17:47:11 +0100 Christopher Creutzig wrote:
>
> I still think an even better solution would be to read the perl
> documentation and follow the example in 'perldoc perlrun':
>
> #!/bin/sh -- # -*- perl -*- -p
> eval 'exec /usr/bin/perl -wS $0 ${1+"$@"}'
> if $running_under_some_shell;
>
Except of course the original poster had said that he was distributing
applications in an environment where he couldnt guarantee that he had
/usr/bin/perl .
/J\
--
Jonathan Stowe <jns@btinternet.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: Thu, 18 Feb 1999 22:51:55 GMT
From: biglife99@my-dejanews.com
Subject: Perl socket question
Message-Id: <7ai5i7$6qp$1@nnrp1.dejanews.com>
Hello everybody,
I am writing a perl server which accepts tcp/ip client connections and
returns information based on the request. Everything works fine except one
problem, the client has to send a newline character for my server to read the
data on the socket. My question, is there any way for my server to read data
data from the socket without the client sending a newline character?
thanks in advance for any help...
Sumesh Gupta
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Thu, 18 Feb 1999 16:52:03 -0500
From: Rabin Vongpaisal <rabinvp@nortelnetworks.com>
Subject: Problems Executing Script from web.
Message-Id: <36CC8B83.D4068677@hotmail.com>
Hi,
I was wondering if any of you can quickly help me out. I have written
scripts for Unix and I am trying to get a script working under Win NT.
The perl interpreter and perl registry ars supposedly to be installed
correctly. I can run the script from a DOS prompt ok, but when I try to
execute it from the web, it gives me this error:
---
CGI Error
The specified CGI application misbehaved by not returning a complete set
of HTTP headers. The headers it did return are:
Can't open perl script "???????????????ll????????????????????l??":
Invalid argument
---
I'm not sure what this means, but when running it from a DOS prompt, I
don't get any compilation errors.
The URL I used to run it was something like this:
http://servername/cgi-bin/test.pl
It's running on a MS Internet Information Server on Windows NT. Even a
simple Hello World program with a print statement gives me that error
message.
Any suggestions would be greatly appreciated.
Thanks in advance,
Rabin
------------------------------
Date: Thu, 18 Feb 1999 15:12:40 -0800
From: "ghost" <haiti@stc.com>
Subject: speed
Message-Id: <7ai6oi$62v$1@giants.stc.com>
Hi all,
Would Perl be a feesible language to use if I wanted to interface with a
MSQL database built to handle large amounts of traffic ie. database quering
and such, on the web?
Thanks again
------------------------------
Date: Thu, 18 Feb 1999 15:45:51 -0600
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: String concatenation, differences
Message-Id: <fm1ia7.oae.ln@magna.metronet.com>
renenyffenegger@my-dejanews.com wrote:
: Does it make any difference (in terms of speed)
: between the following to string
: concatenations:
Use the Benchmark.pm module to find out for yourself.
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 18 Feb 1999 16:48:17 -0500
From: "Murtuza Chhil" <nrd1mmc@nrd.ups.com>
Subject: Urgent String Manip. Question ?
Message-Id: <7ai1jl$f0g3@biko.telecom.ups.com>
Hi,
I develp on a Win NT platform and I run into this strange problem....it
probably sems strange to me as I am relatively new to Perl.
I extract the environment string from the path key of the registry and it
contains a sting like
$strTemp = "D:\perl\bin;%SystemRoot%\system32"
Next I append the string ";D:\mypath" at the end of $strTemp
Next I write this appended value to the path key.
When I run cmd and type path I see
D:\perl\bin;%SystemRoot%\system32;D:\mypath
instead of
D:\perl\bin;C:\Winnt\system32;D:\mypath
[ the %SystemRoot% implicitly gets changed to c:\winnt ....Winnt behavior]
Q) Do I need to take special care of the % sign by putting quotes or "\"
or is this a Winnt problem rather than perl.
Any help is greatly appreciated...
Chhil
------------------------------
Date: Thu, 18 Feb 1999 15:44:02 -0600
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Variable for variable names
Message-Id: <2j1ia7.oae.ln@magna.metronet.com>
zin@clanplaid.net wrote:
: I'm a relative perl newbie, and I was wondering if there's any way to have a
: loop act upon a different variable on each iteration. I was hoping to have an
: array with variable names ($foo, $bar, $etc) and then have a for loop
: to call the different elements of the array within a statement, such as
: "$vararray[$i] = valuexyz;" where $vararray[$i] is actually $foo, so that
: valuexyz goes into $foo.
: Is there any way of doing this?
Sure.
------------------------
#!/usr/bin/perl -w
#use strict;
$foo = 'foo before'; print "foo: $foo\n";
$bar = 'bar before'; print "bar: $bar\n";
$etc = 'etc before'; print "etc: $etc\n";
print "\n";
foreach my $var ($foo, $bar, $etc) {
$var =~ s/before/after/; # $var is a alias, read 'perlsyn.pod'
}
print "foo: $foo\n";
print "bar: $bar\n";
print "etc: $etc\n";
------------------------
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
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 4934
**************************************