[8736] in Perl-Users-Digest
Perl-Users Digest, Issue: 2352 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 17 17:07:28 1998
Date: Fri, 17 Apr 98 14:00:33 -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 Fri, 17 Apr 1998 Volume: 8 Number: 2352
Today's topics:
Re: 'strict' and filehandles (Alan Schwartz)
Re: Array slices using variables for range <philv@gribbles.com.au>
Re: Carriage return/line feed in text files (Chris King)
Re: Carriage return/line feed in text files <grinch@whoville.com>
Re: Carriage return/line feed in text files <rootbeer@teleport.com>
Re: Carriage return/line feed in text files <lr@hpl.hp.com>
Re: CLPM CONTENT POLICE (was info on cookies) (I R A Aggie)
Re: commenting a whole block? (Chris Nandor)
Re: Easy way to count lines in a file <sgordon@athena.lbl.gov>
Re: editing /etc/group file jtaylor@snet.net
Re: emacs info files for Perl 5.004 (Jan Dubois)
Re: Excel97 wont SaveAs() (Jan Dubois)
Fast getpwuid() call? (EXCHANGE:CAR:5J42)
Re: file upload <rootbeer@teleport.com>
Re: Hash of Arrays (Joe McMahon)
HELP!!! fork, exec, etc. <NOSPAMkEynOn@panix.comNOSPAM>
Re: Help: run VB Msgbox thou Perl (Win32::OLE) (Jan Dubois)
Re: How to get only 2 decimal notation? <philv@gribbles.com.au>
Re: How to get only 2 decimal notation? <ppp-support@canelle.telecom.uqam.ca>
Re: How to get only 2 decimal notation? (Mike Heins)
Re: How to insert a \n each... <philv@gribbles.com.au>
Re: how to remove blanks? <lr@hpl.hp.com>
Re: i want to learn Perl. HELP! (Chris King)
Re: Image instead of button in Perl Script <batkins@age.net>
Newbie question about return codes <ronald.wouters@tornado.be>
Re: order of declaration - CLARIFICATION <prl2@lehigh.edu>
order of declaration - my, local, anonymous sub <prl2@lehigh.edu>
Re: rand($.) (was wc-l in Perl) (Jamie McCarthy)
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 17 Apr 1998 16:54:41 GMT
From: alansz@araw.mede.uic.edu (Alan Schwartz)
Subject: Re: 'strict' and filehandles
Message-Id: <6h81gh$1no6$1@piglet.cc.uic.edu>
Uwe Hein <Uwe.Hein@cheops.informatik.uni-osnabrueck.de> writes:
>With 'use strict' I am not able to call
>
>'open ( FOO , $filename ) ;' anymore
>
>(Bareword error).
>
>Can anyone help?
Either:
open(FOO, $main::filename);
(if filename is to be a global package variable in the main package)
or:
my $filename = ....
open(FOO,$filename);
(And, of course, you should be writing:
die "Unable to open $filename: $!\n" unless open(FOO,$filename);
or some variant thereof. :)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Alan Schwartz <alansz@uic.edu>
Asst. Prof. of Clinical Decision Making | University of Illinois at Chicago
Adj. Asst. Prof. of Psychology | Department of Medical Education
"Life is what happens to you while you're busy making other plans"
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
------------------------------
Date: 17 Apr 1998 03:48:47 GMT
From: "Phil Vuong" <philv@gribbles.com.au>
Subject: Re: Array slices using variables for range
Message-Id: <01bd69b3$ffb8b740$57b837cb@melcs10.gribbles.com.au>
Robert Fridman <fridman@cpsc.ucalgary.ca> wrote in article
<35353BCB.1D68@cpsc.ucalgary.ca>...
> @a = qw(a b c d e f);
> $length = @a;
> $three = 3;
> print $length,"\n";
$length=6 --> 0..6 = 7values, which more than what your array has.
so if you want go get rid of the warning message, try $length--;
> print join " ", @a, "\n";
> print join " ", @a[0..3], "\n";
> print join " ", @a[0..$three], "\n";
> print join " ", @a[0..$length], "\n"; <---- line 9
------------------------------
Date: Fri, 17 Apr 1998 18:55:57 GMT
From: cking@cyberg8t.com (Chris King)
Subject: Re: Carriage return/line feed in text files
Message-Id: <3539a56e.90525006@news.cyberg8t.com>
On Thu, 16 Apr 1998 14:30:00 -0600, Dan Baker <dtbaker_@flash.net>
wrote:
>I would guess you are forgetting to add the \n at the end of each line
>you are printing... unless you TELL perl to add a newline, it won't...
>By the same token, when reading IN text, you almost always have to use
>the chomp($string) function to remove the newline from the end.
You do mean chop($sting) and not chomp($sting), right?
Sheila King
------------------------------
Date: Fri, 17 Apr 1998 15:11:45 -0400
From: "Grinch" <grinch@whoville.com>
Subject: Re: Carriage return/line feed in text files
Message-Id: <6h88mt$b0n@fridge.shore.net>
Chris King wrote in message <3539a56e.90525006@news.cyberg8t.com>...
>On Thu, 16 Apr 1998 14:30:00 -0600, Dan Baker <dtbaker_@flash.net>
>wrote:
>
>You do mean chop($sting) and not chomp($sting), right?
No, he meant chomp($string).
chop() removes the last character, regardless of what that character is.
chomp() removes all trailing carriage returns and/or linefeeds.
-grinch
--
-----
"Yes, I'm paranoid, but that doesn't mean
no one's out to get me." - me
Sherm Pendley
grinch@whoville.com
http://www.whoville.com
------------------------------
Date: Fri, 17 Apr 1998 19:49:14 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Chris King <cking@cyberg8t.com>
Subject: Re: Carriage return/line feed in text files
Message-Id: <Pine.GSO.3.96.980417124839.8799P-100000@user2.teleport.com>
On Fri, 17 Apr 1998, Chris King wrote:
> You do mean chop($sting) and not chomp($sting), right?
Generally, chomp is preferred, if it will do the job. Hope this helps!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Fri, 17 Apr 1998 12:37:02 -0700
From: "Larry Rosler" <lr@hpl.hp.com>
Subject: Re: Carriage return/line feed in text files
Message-Id: <6h8b11$5uu@hplntx.hpl.hp.com>
Chris King wrote in message <3539a56e.90525006@news.cyberg8t.com>...
>On Thu, 16 Apr 1998 14:30:00 -0600, Dan Baker <dtbaker_@flash.net>
>wrote:
>
>
>>I would guess you are forgetting to add the \n at the end of each line
>>you are printing... unless you TELL perl to add a newline, it won't...
>>By the same token, when reading IN text, you almost always have to use
>>the chomp($string) function to remove the newline from the end.
>
>You do mean chop($sting) and not chomp($sting), right?
>
>Sheila King
No, though chop will usually do what you want. See the Blue Camel (page
149). chomp "removes only any line ending corresponding to the current
value of $/, and not just any last character." (By default, the value of $/
is "\n".
--
Larry Rosler
Hewlett-Packard Laboratories
lr@hpl.hp.com
------------------------------
Date: Fri, 17 Apr 1998 16:07:51 -0500
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: CLPM CONTENT POLICE (was info on cookies)
Message-Id: <fl_aggie-1704981607520001@aggie.coaps.fsu.edu>
In article <6h85u8$7qv@fridge.shore.net>, "Grinch" <grinch@whoville.com> wrote:
+ I R A Aggie wrote in message ...
+
+ >In article <Pine.GSO.3.95.980417103643.19418C-100000@cp.pathfinder.com>,
+ >"Andrew F. Lee" <andrewf@cp.pathfinder.com> wrote:
+ >
+ >+ By useful I mean, in particular, more than "RTFM" or "That's not Perl".
+ I
+ >+ know that.
+ >
+ >And those answers are not useful _how_?
+
+ They typically don't tell the user what RTFM stands for, where TFM can be
+ found, or offer any guidance as to which newsgroup _would_ be more
+ appropriate.
Ah, you mean like the contents of:
Message-ID: <6gdugl$k3e$1@news1.teleport.com>
+ Archive-name: perl-faq/finding-perl-faq
+ Posting-Frequency: weekly
+ Last-modified: 7 April 1998
+
+ For most people, this URL should be all you need in order to find Perl's
+ Frequently Asked Questions (and answers).
+
+ http://cpan.perl.org/doc/FAQs/
+
+ Please look over (but never overlook!) the FAQ and related docs before
+ posting anything to the comp.lang.perl.* family of newsgroups.
+
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+
+ Beginning with Perl version 5.004, the Perl distribution itself includes
+ the Perl FAQ.
It was posted clpm and clp.announce.
James
--
Consulting Minister for Consultants, DNRC
The Bill of Rights is paid in Responsibilities - Jean McGuire
To cure your perl CGI problems, please look at:
<url:http://www.perl.com/CPAN-local/doc/FAQs/cgi/idiots-guide.html>
------------------------------
Date: Fri, 17 Apr 1998 15:18:54 -0400
From: pudge@pobox.com (Chris Nandor)
Subject: Re: commenting a whole block?
Message-Id: <pudge-1704981518550001@ppp-12.ts-1.kin.idt.net>
In article <35377F32.B62B2DE2@holly.colostate.edu>, John Blanco
<johnnie@holly.colostate.edu> wrote:
# Ji Huang wrote:
#
# > , but in perl i have to put # sign at the beginning of each line of
# > the
# > block.
# >
# > Is there any easier way for me to do it?
#
# Like this...
#
# print "I am the ";
# print "original code!\n";
#
# make it:
#
# if(0) {
# print "I am the ";
# print "original code!\n";
# }
Personally, I like:
=pod
comment();
out();
all_this_code();
=cut
--
Chris Nandor mailto:pudge@pobox.com http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10 1FF7 7F13 8180 B6B6'])
#== New Book: MacPerl: Power and Ease ==#
#== Publishing Date: Early 1998. http://www.ptf.com/macperl/ ==#
------------------------------
Date: Fri, 17 Apr 1998 13:33:01 -0700
From: Shawn Gordon <sgordon@athena.lbl.gov>
To: Harold Reed <"hareed"@bellsouth.net>
Subject: Re: Easy way to count lines in a file
Message-Id: <3537BC7D.373E69D9@athena.lbl.gov>
Hi Harold
> Is there an easy way to count the number of lines a file has?
[clip]
>
> while (<ACCESS>) {
> counter++;
> }
> print "You have $counter lines in this file\n";
>
How about:
$lines = scalar(@_ = <ACCESS>);
shawn
------------------------------
Date: Tue, 14 Apr 1998 09:39:44 -0600
From: jtaylor@snet.net
Subject: Re: editing /etc/group file
Message-Id: <6gvsfg$3t2$1@nnrp1.dejanews.com>
In article <6gtuln$65j$5@info.uah.edu>,
Greg Bacon <gbacon@cs.uah.edu> wrote:
>
> In article <6gtj26$dta$1@nnrp1.dejanews.com>,
> jtaylor@snet.net writes:
> :I am trying to write a little script that will update the group file with a
> :new user. I thought about reading in the whole line and apending to it,
then
> :writing it out. I also thought about just prepending the new user's name to
> :the begining of the list.
>
> my $user = "luser";
> my @groups = qw( lp user other );
>
> for (@groups) {
> my($group,$passwd,$gid,$members) = getgrnam $_;
>
> unless (defined $group) {
> warn "Group `$_' does not exist!\n";
> next;
> }
>
> if ($members eq "") {
> $members = $user;
> }
> else {
> $members .= " $user";
> }
> $members =~ tr/ /,/;
>
> print join(":", $group, $passwd, $gid, ""), $members, "\n";
> }
>
> Of course, you'll probably want to run through /etc/group line by line
> searching for each group you want to change.
This does help and thank you, only now I can't figure out how to write it
out to replace the old group line. I thought I would try:
$gfile = "/etc/group";
open(GFILE, "+<$gfile");
while (<GFILE>) {
s/$old_line/$new_line/ or die "Can't do substitue: $!\n";
# where $old_line = getgrnam($_)[0..3];
# $old_line =~ tr/ /,/;
}
My assumption is that it opens the file starts reading line by line, and then
relaces those lines that match with the new line. Unfortunately, it doesn't
do anything except error with:
$! = Error 536971436 occurred.
Anyway, I'm at a loss. I hope some one can understand this ill written
message. Thanks to anyone who can help.
>
> Hope this helps,
> Greg
> --
> open(G,"|gzip -dc");$_=<<EOF;s/[0-9a-f]+/print G pack("h*",$&)/eg
> f1b88000b620f22320303fa2d2e21584ccbcf29c84d2258084
> d2ac158c84c4ece4d22d1000118a8d5491000000
> EOF
>
Joshua Taylor
jtaylor@snet.net
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
------------------------------
Date: Fri, 17 Apr 1998 22:05:02 +0200
From: jan.dubois@ibm.net (Jan Dubois)
Subject: Re: emacs info files for Perl 5.004
Message-Id: <353fb2bb.11920270@news2.ibm.net>
[mailed & posted]
indhiraa@hotmail.com wrote:
>Where can I find emacs-info files for Perl 5.004 documentation.
>
>Or how can I generate one.
The most up to date info files (for Perl 5.004_04 I think) can be found
on CPAN under author Krishna_Shamu_Sethuraman (filename is
perl-54-info.tar.gz). It even includes PODs from many modules including
LWP. Regrettably it is missing the FAQs. You'll also find a pod2info
translator in this directory.
-Jan
------------------------------
Date: Fri, 17 Apr 1998 22:05:03 +0200
From: jan.dubois@ibm.net (Jan Dubois)
Subject: Re: Excel97 wont SaveAs()
Message-Id: <3541b3fb.12240711@news2.ibm.net>
[mailed & posted]
Rob Knight <robk-not-@reading.sgi.com> wrote:
>I am having some grief using Win32::OLE to open an HTML file and then
>save it into Excel95/5 format with Excel97. Whilst Excel can be told to
>open the HTML and then save it away to another filename, I just can't
>pursuade it to use the xlExcel5 format...
>
>use Win32::OLE ;
>
>$tfile = 'c:\start.htm' ;
>$nfile = 'c:\finish.xls' ;
>
>$xl = new Win32::OLE 'Excel.Application' ;
>$xl->{Visible} = 1 ;
>
>$xl->Workbooks->Open($tfile) ;
>
>$xl->ActiveWorkbook->SaveAs({
> Filename => $nfile,
> Fileformat => 'xlExcel5',
> });
>
>$xl->Quit() ;
xlExcel5 is a symbol constant. It has the value 39. Depending on the
version of your Win32::OLE module you can also use the Win32::OLE::Const
module:
use Win32::OLE::Const 'Microsoft Excel';
#...
$xl->ActiveWorkbook->SaveAs({
Filename => $nfile,
Fileformat => $xlExcel5,
});
Please note that the next release of Win32::OLE will change the const
module to produce inlinable subs instead of scalar variables. You'll
then be able to just write xlExcel5 (without the leading $).
-Jan
------------------------------
Date: Fri, 17 Apr 1998 15:03:32 -0400
From: "Totten, Grant (EXCHANGE:CAR:5J42)" <grantt@americasm01.nt.com>
Subject: Fast getpwuid() call?
Message-Id: <3537A783.10BD5D8@americasm01.nt.com>
Hi all,
We've got a few low-level routines that need to determine the user's
effective userid. In our environment (we use Yellow Pages) the
getpwuid() call can take a long-ish time. I was wondering if there is
another call or some other technique that we can use to determine the
current userid.
Here is what we use right now:
$login = (getpwuid($<))[0];
Any suggestions would be greatly appreciated. Please respond via e-mail
to:
grantt@nortel.ca
Thanks in advance,
Grant Totten
Nortel Public Data Networks
------------------------------
Date: Fri, 17 Apr 1998 19:41:24 GMT
From: Tom Phoenix <rootbeer@teleport.com>
To: Jayadev Gopinath <jayadev.gopinath@fmr.com>
Subject: Re: file upload
Message-Id: <Pine.GSO.3.96.980417124017.8799N-100000@user2.teleport.com>
On Fri, 17 Apr 1998, Jayadev Gopinath wrote:
> What are the modules that I need to have to write a file upload utility
> on Win NT and where do I get them ??
Besides the ones which come with Perl, you'll find some useful modules on
CPAN, in the modules directory. Hope this helps!
http://cpan.perl.org/
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Fri, 17 Apr 1998 15:31:00 -0400
From: joe.mcmahon@gsfc.nasa.gov (Joe McMahon)
Subject: Re: Hash of Arrays
Message-Id: <joe.mcmahon-1704981531000001@prtims.stx.com>
In article <353502EF.2CC600FF@email.csun.edu>, Ovanes Manucharyan
<olm@email.csun.edu> wrote:
>How can I create a hash of Arrays.
>For example, will this work?
>
>@HASH{$index}=@array;
>
>Do I need to create this data type? I tried to use this in my code and
>it only stores the first element of the @array, am I doing something
>wrong??
>
Yes, you are doing something wrong.
There are a couple problems here. No, make that several problems here.
First, when accessing an element in a hash, you say "$hash{$key} = $value;".
All hash elements (like array elements) have to be scalars, so you
indicate this with the leading "$".
Since what you are assigning to the hash is an array, and assignment to a
hash element forces scalar context, what you're going to get is the number
of elements in the array you're assigning, as if you'd said
scalar(@array).
Since you can't store an array in a hash element, you have to store
something that references the array in the hash element instead. "\@array"
would do this, because the resulting reference to the array is a scalar
and can be stored.
However, if you reuse \@array several times, you will end up with a bunch
of references that point to exactly the same place, so if you try to mess
with the array that $hash{"foo"} points to, you'll muck up the one that
$hash{"bar"} points to, because they're the same array.
There are two ways around this: one is to use a BLOCK, create a "my"
variable inside it, and asign this "my" variable to the hash entry. Once
you exit the block the array disappears from the symbol table, but hangs
around because there's still a reference to it from your hash.
The other, easier way is to use an anonymous array constructor, like this:
$hash{$index} = [@array];
@array is evaluated in list context; this list is used to initialize the
anonymous array, and the reference created by the anonymous array
constructor is stored in $hash{$index}. You can then rebuild @array and
execute this statement again to get a new anonymous array containing the
new contents of @array.
To get the content of these anonymous arrays back, use
$hash{$index}->[$arrayindex] for individual items
@{$hash{$index} for the whole array.
I'm sorry to have to saddle you with references, anonymous constructors, and
the proper syntax for addressing hashes, all in the same message, but
there it is.
I hope this hasn't scared you away from trying to do this; it's really not
that difficult.
ObRTFM: perlfaq4 (Data::Arrays section), perldata (constructors), perlref
(references)
--- Joe M.
p.s. - the proper syntax for talking about a hash as a whole is %hash, not
@hash.
------------------------------
Date: 17 Apr 1998 20:39:42 GMT
From: k y n n <NOSPAMkEynOn@panix.comNOSPAM>
Subject: HELP!!! fork, exec, etc.
Message-Id: <6h8eme$6l1@news1.panix.com>
I'm having trouble making sense of fork & exec, and could use some
help.
I want to start and communicate with a program (called RasMol) from
within my perl script, as well as get user input via STDIN. Something
like
open(RASMOL, "|rasmol");
.
.
.
print RASMOL "load pdb $tmpFl\n";
lets me talk to RasMol OK, but, unfortunately, RasMol takes over the X
window running the script, thereby blocking user input via STDIN. So,
what I think I need to do is to start a separate X window with RasMol
running on it. On the other hand, with everything I've tried along
these lines, I lose the ability to communicate with RasMol from within
the script.
I could do something like:
if(fork) {
exec("xwsh -e rasmol");
}
but I don't know how to send commands to RasMol. Is there a good way
for the parent process to learn the device name of the window where
the child's RasMol process is running? Or alternatively to get a
filehandle associated with the child's RasMol process to write to?
(I'm trying to learn as much Perl as possible, so, if I may look at a
gift horse in the mouth here, rather than "&hiTechQuickSlickPerl"
solutions I'd prefer "low-tech-verbose-but-instructive-perl" ones.)
Thanks for your help,
K.
--
To reply directly by e-mail, remove the upper-case letters from the
return address in the header.
------------------------------
Date: Fri, 17 Apr 1998 22:05:01 +0200
From: jan.dubois@ibm.net (Jan Dubois)
Subject: Re: Help: run VB Msgbox thou Perl (Win32::OLE)
Message-Id: <353bb19c.11633598@news2.ibm.net>
[mailed & posted]
Tommy <tkho@technologist.com> wrote:
>This may be a question for OLE or VB. But my question is how to run VB's
>MsgBox command thou Perl. Is there a source I can look up for the
>commands for PerlW32's OLE (other than the help of MSOffice's VB
>builder)?
>
>My script works with InputBox that a input windows pop up when the
>script is run. However, it does nothing when I change it to "MsgBox".
>What I want is to have a window to pop up with a message and the OK and
>Cancel buttons.
Your script works with InputBox because that is a method of the Excel
application object. You are not calling the InputBox function; you are
calling the InputBox method. There is no corresponding MsgBox method,
just the MSgBox Visual Basic function. I'm not aware of a way to call
Visual Basic functions from Perl (but I also haven't tried very hard).
-Jan
------------------------------
Date: 17 Apr 1998 03:27:01 GMT
From: "Phil Vuong" <philv@gribbles.com.au>
Subject: Re: How to get only 2 decimal notation?
Message-Id: <01bd69b0$f5a2f520$57b837cb@melcs10.gribbles.com.au>
Try this :
printf "%5.2f\n",$value;
cheers!
PV
Apisilp Trunganont <b39ast@nontri.ku.ac.th> wrote in article
<6gkis7$keg$1@news.ku.ac.th>...
> $value = (27/126)*100;
> print $value;
> The output is 2.142857142857142705. But I want only 2.14. How to do this?
------------------------------
Date: Fri, 17 Apr 1998 20:05:53 GMT
From: ppp-support <ppp-support@canelle.telecom.uqam.ca>
Subject: Re: How to get only 2 decimal notation?
Message-Id: <3537B5E9.36216644@canelle.telecom.uqam.ca>
tony wrote:
> > Huh? Sprintf does round:
> >
> > perl -e 'print sprintf("%.2f\n", 1.249)'
> > 1.25
> > perl -e 'print sprintf("%.2f\n", 1.241)'
> > 1.24
sprintf looks nice but if i want a sub routine to take care of that
rounding stuf??
sub round
{
my $value = $_[0];
my $precision = $_[1];
my $value;
... # Do the rounding with the precision we want
return $value;
}
and then we could use smething like this:
$value = (27/126)*100;
$roundvalue = &round($value);
So we can print out $roundvalue or do something else with it, like
passing the rounded value to another routine etc..! Can it be more
useful like this?
Reni
------------------------------
Date: 17 Apr 98 20:52:04 GMT
From: mikeh@minivend.com (Mike Heins)
Subject: Re: How to get only 2 decimal notation?
Message-Id: <3537c0f4.0@news.one.net>
Larry Rosler <lr@hpl.hp.com> wrote:
> tony wrote in message ...
>>
>>
>>Any ideas of what to use if your number has leading zeros?
>>
>>perl -e 'print sprintf("%.2f\n", 00.000)'
>>0.00
>>perl -e 'print sprintf("%.2f\n", 01.000)'
>>10.00
>>perl -e 'print sprintf("%.2f\n", 01.999)'
>>1999.00
>>perl -e 'print sprintf("%.2f\n", 10.999)'
>>11.00
>>
> These are very strange results indeed. I tried the following:
> printf "%.2f\n", '01.777';
> printf "%.2f\n", 1.777;
> printf "%.2f\n", 01.777;
> printf "%.2f\n", 01.777e0;
> printf "%.2f\n", +01.777e0;
> printf "%.2f\n", 01777;
> The first two yield the expected result, 1.78, but the next three yield
> 1777.00, as you reported. The last yields 1023.00, as expected.
> The Blue Camel says (p. 38, footnote): To convert from string to number,
> Perl uses C's atof(3) function. So I tried (in C):
> printf("%.2f\n", atof("01.777"));
> and got the expected 1.78.
Why would you ever have this type of notation in a literal, which
is the only time this can happen? And when it is an unspecified
and undefined literal notation?
> Apparently the Perl parser is not converting the literal 01.777 to convert
> to a floating-point number. The Blue Camel says (p. 39, re 0xffff and
> 0377): "The automatic conversion of a string to a number does not recognize
> these prefixes..." Why should it do anything special with 01.777 other than
> hand it to atof?
> This seems like a perl bug to me. If so, I'm amazed it hasn't been noticed
> till now. "This is perl, version 5.004_03"
Not really strange -- Perl doesn't do octal decimals. Perl is treating
it as 01 . 777, which becomes 1 . 777. Try 01.977 and you get:
Illegal octal digit at - line 7, at end of line
Execution of - aborted due to compilation errors.
The only bug I can see is rolling the expectation of an octal number
past the concatenation operator. This may be tied up in some gnarly compiler
precedence issue, for all I know.
Since the literal is an octal number, and octal numbers don't include
decimals, Perl is doing the best it can. It certainly has no way to
know the literal is going to an atof() call, nor would I want it to
waste the processor time to figure that vague possibility out.
Perl 5 dropped more than one level of "automagical" conversion.
Note it is documented in a very vague way:
!!perldoc -f oct | sed 's/^/ /'
=item oct EXPR
=item oct
Interprets EXPR as an octal string and returns the corresponding
value. (If EXPR happens to start off with 0x, interprets it as
a hex string instead.) The following will handle decimal, octal, and
hex in the standard Perl or C notation:
$val = oct($val) if $val =~ /^0/;
If EXPR is omitted, uses $_. This function is commonly used when
a string such as "644" needs to be converted into a file mode, for
example. (Although perl will automatically convert strings into
numbers as needed, this automatic conversion assumes base 10.)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note also that it says "octal string", and not "octal number".
The bottom line is that a literal 01.777 is not a legal syntax,
and 01 . 777 is.
--
Mike Heins http://www.minivend.com/ ___
Internet Robotics |_ _|____
Just because something is 131 Willow Lane, Floor 2 | || _ \
obviously happening doesn't Oxford, OH 45056 | || |_) |
mean something obvious is <mikeh@minivend.com> |___| _ <
happening. --Larry Wall 513.523.7621 FAX 7501 |_| \_\
------------------------------
Date: 17 Apr 1998 03:34:56 GMT
From: "Phil Vuong" <philv@gribbles.com.au>
Subject: Re: How to insert a \n each...
Message-Id: <01bd69b2$108e5360$57b837cb@melcs10.gribbles.com.au>
> > LE CORRE (lecorre@magic.fr) wrote:
> > : I tried $line=~ s/.{10}/\n/g; but it doesn't work.
Try this :
$line =~ s/.{10}/$&\\n/g ;
PV
------------------------------
Date: Fri, 17 Apr 1998 12:28:25 -0700
From: "Larry Rosler" <lr@hpl.hp.com>
Subject: Re: how to remove blanks?
Message-Id: <6h8agp$5uo@hplntx.hpl.hp.com>
Zenin wrote in message <892836055.73298@thrush.omix.com>...
>Sami Sandqvist <samiss@cc.tut.fi> wrote:
> >snip<
>: Also, this is a FAQ. The easiest way is
>: $string =~ s/^\s*(.*?)\s*$/$1/;
>: but doing
>: $string =~ s/^\s+//;
>: $string =~ s/\s+$//;
>: is faster.
>
> Or somewhere in between, where we still only have one line of code,
> but it's nearly as fast as the two line version:
>
> $string =~ s/(?:^\s+|\s+$)//g;
^^^ ^
I don't think these parentheses accomplish anything.
("Don't capture the match that's not used anyway.")
>
> Benchmark: timing 100000 iterations of FAQ, EASIEST, ZENIN...
> EASIEST: 10 secs ( 9.33 usr -0.01 sys = 9.32 cpu)
> TWO_PART: 3 secs ( 3.05 usr 0.00 sys = 3.05 cpu)
> ZENIN: 4 secs ( 4.90 usr 0.00 sys = 4.90 cpu)
Without your test string, it's difficult to reproduce or comment on these
results. I have measured the following variation to be FASTEST, provided
you are sure there is at least one non-space character in the operand. (You
can test the value of the expression -- FALSE if there are no non-space
characters -- but that test dissipates the small speed advantage.)
$string =~ s/^\s*(.*\S)\s*$/$1/;
My benchmark results (usr time for 100000 iterations) if
$string = "\t\t\txxx\t\t\t"; are
EASIEST: 5.18
FASTEST: 4.41
TWO_PART: 6.00
ZENIN: 5.92
Obviously, YMMV.
--
Larry Rosler
Hewlett-Packard Laboratories
lr@hpl.hp.com
------------------------------
Date: Fri, 17 Apr 1998 18:53:23 GMT
From: cking@cyberg8t.com (Chris King)
Subject: Re: i want to learn Perl. HELP!
Message-Id: <3538a488.90295616@news.cyberg8t.com>
On Mon, 13 Apr 1998 23:37:13 -0700, rdm@cfcl.com (Rich Morin) wrote:
>Although I have great respect for both the Camel and Llama books, and we
>point folks to them several times in our own book ("MacPerl: Power and Ease"),
>I must respectfully disagree with you in this case.
>
>Neither the Camel nor even the Llama makes any real concessions to the needs
>of beginning programmers. In fact, both books appear to assume that the
>reader already knows at least one programming language. Many references and
>comparisons are made to C, a few less to awk, sed, and sh. Here is a quote
>from the beginning of the Llama book (2nd Ed., p. 35):
>
> "Perl's operators and expressions are generally a superset of those
> provided in most other ALGOL/Pascal-like programming languages, such
> as C or Java."
>
>Part of our goal, in writing "MacPerl: Power and Ease", was to allow Mac
>users, many of whom are not even familiar with command-line interfaces, to
>successfully approach and learn programming, using (Mac-)Perl as a platform.
I have to agree. I am just teaching myself Perl (for a couple of days
now). I have the Llama book and another tutorial book. I have
experience programming in other languages, but not C or awk or sed or
whatever is common on Unix. Reading Chap. 1 in the Llama book, there
are continual references to "this is like in <such and such language>
or <that other language> etc..." I would think this would be extremely
difficult for someone without any previous programming experience to
make heads or tails of.
Sheila King
>
>Please don't get me wrong: I have the O'Reilly books sitting right next to
>my desk; don't try to borrow them! But I wouldn't hand either the Camel
>or the Llama to a programming neophyte, given a better alternative.
>
>-r
------------------------------
Date: Fri, 17 Apr 1998 15:57:48 -0400
From: Brad Atkins <batkins@age.net>
Subject: Re: Image instead of button in Perl Script
Message-Id: <3537B43C.FFA5F6E4@age.net>
Actually it does.
The proccess
# If this is the result of a new message subform.
elsif($in{"MAILMAN_NEW"})
{
OutputMessageForm("NEW",0,0);
exit(0);
}
Calls a new action. If that action is called upon by a form button it works
ok and fetches the apporpriate data. But when switch to an image instead of
the default form button, it doesn't do anything except reload the page. It
is not due to HTML programming error.
Any ideas?
Brad Atkins
ps: never knew people who knew Perl are so stuck up.
Bob Trieger wrote:
> Brad Atkins wrote:
> >
> > I've been trying to get an image instead of a normal form button in a
> > web based email client. The problem is, that with certain functions
> > (everything except logging in) the Script does not fetch the datum.
>
> And somehow this has something, anything to do with perl?
>
> --
> Bob Trieger | Titanic: big boat, bigger
> sowmaster@juicepigs.com | iceberg, big deal
------------------------------
Date: Fri, 17 Apr 1998 21:57:00 +0200
From: Ronald Wouters <ronald.wouters@tornado.be>
Subject: Newbie question about return codes
Message-Id: <3537B40B.E39AE377@tornado.be>
This is a multi-part message in MIME format.
--------------E635B919F2429A6EB0525431
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi, I'm new to Perl and I would like to know the following.
When I do something like:
$rc = system("xxxxxx.exe");
$rc = $rc/256;
Then in some cases the value of $rc equals 255.
What does 255 mean ?
Thanks in advance.
Ronald Wouters
Email:ronald.wouters@NOSPAM.tornado.be
--------------E635B919F2429A6EB0525431
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Wouters, Ronald
Content-Disposition: attachment; filename="vcard.vcf"
begin: vcard
fn: Ronald Wouters
n: Wouters;Ronald
email;internet: ronald.wouters@tornado.be
x-mozilla-cpt: ;0
x-mozilla-html: FALSE
version: 2.1
end: vcard
--------------E635B919F2429A6EB0525431--
------------------------------
Date: Fri, 17 Apr 1998 15:13:41 -0400
From: "Phil R Lawrence" <prl2@lehigh.edu>
Subject: Re: order of declaration - CLARIFICATION
Message-Id: <6h89mr$14ig@fidoii.cc.Lehigh.EDU>
BTW, the area marked with the <!*!> was a cut and paste error:
> local $SIG{"INT"} = sub <!*!>
>
> no strict;
> $dbh->disconnect if ($dbh);
> exit(0);
> };
of course should have read:
local $SIG{"INT"} = sub {
no strict;
$dbh->disconnect if ($dbh);
exit(0);
};
Thanks,
Phil
------------------------------
Date: Fri, 17 Apr 1998 14:56:59 -0400
From: "Phil R Lawrence" <prl2@lehigh.edu>
Subject: order of declaration - my, local, anonymous sub
Message-Id: <6h88ni$1ikq@fidoii.cc.Lehigh.EDU>
I want to cleanly exit my program when hit with a CTRL-C. Following are two
snippets of code - the 1st works, the 2nd doesn't. Can someone help me find
out why?
First Version:
<...snip>
sub report {
my $sid = "$ENV{ORACLE_SID}";
my $dsn = "dbi:Oracle:$sid";
my $user = "";
my $pass = "";
my $dbh;
local $SIG{"INT"} = sub
no strict;
$dbh->disconnect if ($dbh);
exit(0);
};
<snip...>
Second Version:
<...snip>
sub report {
local $SIG{"INT"} = sub
no strict;
$dbh->disconnect if ($dbh);
exit(0);
};
my $sid = "$ENV{ORACLE_SID}";
my $dsn = "dbi:Oracle:$sid";
my $user = "";
my $pass = "";
my $dbh;
<snip...>
Specifically, the 2nd doesn't executes the disconnect method, and thus some
errors are written to the console as I dump out of the program. Why does
the order of declaration matter? I should think the 'no strict' would
eliminate that need.
Thanks!
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Phil R Lawrence phone: 610-758-3051
Programmer / Analyst e-mail: prl2@lehigh.edu
194 Lehigh University Computing Center
E.W. Fairchild - Martindale, Bldg. 8B
Bethlehem, PA 18018
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
------------------------------
Date: Fri, 17 Apr 1998 16:04:55 -0400
From: jamie@mccarthy.org (Jamie McCarthy)
Subject: Re: rand($.) (was wc-l in Perl)
Message-Id: <jamie-1704981604550001@clmxmi133074.voyager.net>
Tom Phoenix <rootbeer@teleport.com> wrote:
> > > open FILE, $name or die "Can't read '$name': $!";
> > > while (<FILE>) {
> > > $line = $_ if rand($.) <= 1;
^^ should be "<"
> > > }
> > > print "One randomly-selected line is $line";
> It _is_ biased, but towards the end of the file (see my explanation at
> http://cpan.perl.org/doc/FMTEYEWTK/random).
Your explanation is true, but it's also slightly biased because of the
compare. rand(1) returns a value between 0 and 1: "including 0, but
excluding 1" according to The Camel. So in a two-line file, the
second line is ever-so-slightly more likely to be picked than the
first, if you use "<= 1" instead of "< 1". Of course the effect is
negligible unless used on files with a whole lot of lines.
--
webmaster: http://www.holocaust-history.org/ Jamie McCarthy
fan of: http://www.nizkor.org/ jamie@mccarthy.org
http://jamie.mccarthy.org/
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.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 2352
**************************************