[31316] in Perl-Users-Digest
Perl-Users Digest, Issue: 2561 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 19 14:09:44 2009
Date: Wed, 19 Aug 2009 11:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 19 Aug 2009 Volume: 11 Number: 2561
Today's topics:
Re: end-of-line conventions <ben@morrow.me.uk>
Re: How to code an implicit $_ argument? <m@rtij.nl.invlalid>
Re: How to code an implicit $_ argument? <hjp-usenet2@hjp.at>
Re: How to code an implicit $_ argument? <willem@stack.nl>
Re: How to code an implicit $_ argument? (Tim McDaniel)
Re: How to code an implicit $_ argument? <ben@morrow.me.uk>
replace everything from the 6th character on with aster <jaredsubman@yahoo.com>
Re: replace everything from the 6th character on with a <jurgenex@hotmail.com>
Re: replace everything from the 6th character on with a <willem@stack.nl>
Re: replace everything from the 6th character on with a <someone@example.com>
Re: replace everything from the 6th character on with a sln@netherlands.com
Re: revisiting web development in Perl...where to start <hjp-usenet2@hjp.at>
Re: revisiting web development in Perl...where to start <cartercc@gmail.com>
Re: revisiting web development in Perl...where to start <eli.morris.heft@gmail.com>
Re: Rounding issue <hjp-usenet2@hjp.at>
Re: Rounding issue <hjp-usenet2@hjp.at>
Re: Rounding issue <hjp-usenet2@hjp.at>
Re: search for hex characters in a binary file and remo sln@netherlands.com
Re: undef($foo) versus $foo = undef()? <bugbear@trim_papermule.co.uk_trim>
Re: undef($foo) versus $foo = undef()? <hjp-usenet2@hjp.at>
windows one liner to output unix line feed <shambo_p@yahoo.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 19 Aug 2009 16:55:54 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: end-of-line conventions
Message-Id: <atjtl6-7f03.ln1@osiris.mauzo.dyndns.org>
Quoth Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>:
> In <3mgrl6-imp2.ln1@osiris.mauzo.dyndns.org>, on 08/18/2009
> at 09:48 PM, Ben Morrow <ben@morrow.me.uk> said:
>
> >OK, now I'm really confused, since I didn't think perl worked like that
> >anywhere. What do you get from
>
> > perl -le"print for unpack "C*", qq{\n}"
>
> syntax error at -e line 1, near "*,"
> Execution of -e aborted due to compilation errors.
>
> Presumably due to differences in shell processing between OS/2 and *ix[1].
More due to my inability to convert quoting styles on the fly. Sorry,
what I meant was of course
perl -le"print for unpack q{C*}, qq{\n}"
Ben
------------------------------
Date: Wed, 19 Aug 2009 09:32:16 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: How to code an implicit $_ argument?
Message-Id: <0dmsl6-7pb.ln1@news.rtij.nl>
On Wed, 19 Aug 2009 04:47:38 +0000, Tim McDaniel wrote:
> or something equivalent. Maybe I will go for
>
> defined($_) && (s/^\s+//, s/\s+$//) for @_ ? @_ : $_;
>
This will have unexpected results when $_ is defined, but you do give the
function arguments. Suddenly it works on $_ while you expected it to work
on @_.
Just drop the first test and it looks fine.
M4
------------------------------
Date: Wed, 19 Aug 2009 10:19:18 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: How to code an implicit $_ argument?
Message-Id: <slrnh8ndc6.4g2.hjp-usenet2@hrunkner.hjp.at>
On 2009-08-19 03:48, Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
> On 2009-08-19, Tim McDaniel <tmcd@panix.com> wrote:
>> sub trim(;\$) {
>> if (@_ == 0) {
>> $_[0] = \$_;
>> goto &trim;
>> }
>
> push @_, $_ unless @_;
That pushes a copy of $_ onto @_.
hp
------------------------------
Date: Wed, 19 Aug 2009 08:34:26 +0000 (UTC)
From: Willem <willem@stack.nl>
Subject: Re: How to code an implicit $_ argument?
Message-Id: <slrnh8ne8i.1au0.willem@turtle.stack.nl>
Tim McDaniel wrote:
) The only clean way I see to do it is this, but I don't know whether
) later Perl interpreters notice and optimize the tail recursion.
)
) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
)
) ...
) sub trim(;\$);
) sub trim(;\$) {
) if (@_ == 0) {
) return trim($_);
) }
) my $var = $_[0];
) if (defined $$var) {
) $$var =~ s/^\s+//;
) $$var =~ s/\s+$//;
) }
) return $$var;
) }
I don't see why you would need prototyping for this:
sub trim
{
return trim($_) unless @_;
for (@_) {
s/^\s+//;
s/\s+$//;
}
return @_;
}
As far as I know, members of @_ are aliases for the function parameters, so
this should work directly on the given parameters.
And, of course, the for (@_) loop makes $_ aliases of the @_ members.
I wouldn't worry about the single tail-recursive call. If it turns out you
need the extra speed, just hand-inline the code.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Wed, 19 Aug 2009 16:01:38 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: How to code an implicit $_ argument?
Message-Id: <h6h7l2$cs4$1@reader1.panix.com>
In article <0dmsl6-7pb.ln1@news.rtij.nl>,
Martijn Lievaart <m@rtij.nl.invlalid> wrote:
>On Wed, 19 Aug 2009 04:47:38 +0000, Tim McDaniel wrote:
>
>> or something equivalent. Maybe I will go for
>>
>> defined($_) && (s/^\s+//, s/\s+$//) for @_ ? @_ : $_;
>>
>
>This will have unexpected results when $_ is defined, but you do give
>the function arguments. Suddenly it works on $_ while you expected it
>to work on @_.
Why do you say that? "man perlsyn" says that a
Any simple statement may optionally be followed by a SINGLE
modifier, just before the terminating semicolon (or block ending).
The possible modifiers are:
if EXPR
unless EXPR
while EXPR
until EXPR
foreach LIST
(Pity about that "SINGLE":
s/^\s+//, s/\s+$// if defined($_) for @_ ? @_ : $_;
would look cleaner.)
So "&&" doesn't bind more loosely than "for". The "defined($_)"
refers to the new $_ defined by "for", and s/// works on that $_.
I've tested the statement with
$_ = ' narf ';
my $v = ' abc ';
my $w = "\tzyx \r ";
trim($v, $w);
$v and $w were trimmed and $_ was untouched, as specified.
>Just drop the first test and it looks fine.
You didn't notice what I wrote in a previous article, that I want it
to silently ignore arguments that are undef, and not emit
"Use of uninitialized value $_ in substitution (s///)" messages.
It occured to me later that I could do
$_ and s/^\s+//, s/\s+$// for @_ ? @_ : $_;
But that's getting too much like line-noise for my taste.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Wed, 19 Aug 2009 16:50:48 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to code an implicit $_ argument?
Message-Id: <ojjtl6-7f03.ln1@osiris.mauzo.dyndns.org>
Quoth tmcd@panix.com:
>
> In a recent message, someone mentioned that Perl 5.10 has the _
> prototype character: if there is no matching argument, use $_.
> But that doesn't work here, as the following code produces
> Can't use string (" abc ") as a SCALAR ref while "strict refs"
> in use at /home/tmcdaniel/local/test/084.pl line 7.
> and "sub trim(\_)" causes
> Malformed prototype for main::trim: \_ at
> /home/tmcdaniel/local/test/084.pl line 15.
>
> Also, where is _ documented? I don't see it in "man perlsub" for Perl
> 5.10 in Cygwin.
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> ...
> sub trim(_) {
> my $var = $_[0];
my $var = \$_[0];
> if (defined $$var) {
> $$var =~ s/^\s+//;
> $$var =~ s/\s+$//;
> }
> return $$var;
> }
Or just skip the ref step, and use
for ($_[0]) {
defined or last;
s/^\s+//;
s/\s+$//;
}
return $_[0];
Adjust to accept more than one argument at will...
Ben
------------------------------
Date: Wed, 19 Aug 2009 08:03:31 -0700 (PDT)
From: "jaredsubman@yahoo.com" <jaredsubman@yahoo.com>
Subject: replace everything from the 6th character on with asterisks
Message-Id: <6741a636-85bd-4e77-b99a-12be20b61e52@h31g2000yqd.googlegroups.com>
The code I have to do this so far is:
#!/usr/bin/env perl
use strict;
use warnings;
my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
print "$string\n";
# grab first 5 characters
my $match1=substr($string,0,5);
# grab from the 6th character to the end of the string
my $match2=substr($string,5);
# replace all of the 2nd set of matched chars with asterisks
$match2 =~ tr/[0-9][a-z][A-Z]/*/;
# join both parts
my $new_string = "$match1$match2";
print "$new_string\n";
But surely, there must be a better way to do this, preferably a one-
liner. Any ideas?
Thanks in advance.
------------------------------
Date: Wed, 19 Aug 2009 08:25:29 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: replace everything from the 6th character on with asterisks
Message-Id: <2v5o85l51lrg5nkd46toim8rl4hin860hs@4ax.com>
"jaredsubman@yahoo.com" <jaredsubman@yahoo.com> wrote:
>The code I have to do this so far is:
>
>#!/usr/bin/env perl
>
>use strict;
>use warnings;
>
>my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
>print "$string\n";
># grab first 5 characters
>my $match1=substr($string,0,5);
># grab from the 6th character to the end of the string
>my $match2=substr($string,5);
># replace all of the 2nd set of matched chars with asterisks
>$match2 =~ tr/[0-9][a-z][A-Z]/*/;
># join both parts
>my $new_string = "$match1$match2";
>print "$new_string\n";
>
>But surely, there must be a better way to do this, preferably a one-
>liner. Any ideas?
Indeed there is. You can either use substr() as an lvalue and assign to
the second half of the string
substr($string, 5) = '*' x (length($string)-5);
or you can use the 4-argument form of substr() directly:
substr($string, 5, length($string)-5, '*' x ((length($string))-5));
jue
------------------------------
Date: Wed, 19 Aug 2009 15:43:56 +0000 (UTC)
From: Willem <willem@stack.nl>
Subject: Re: replace everything from the 6th character on with asterisks
Message-Id: <slrnh8o7ds.1kog.willem@turtle.stack.nl>
jaredsubman@yahoo.com wrote:
) The code I have to do this so far is:
)
) #!/usr/bin/env perl
)
) use strict;
) use warnings;
)
) my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
) print "$string\n";
) # grab first 5 characters
) my $match1=substr($string,0,5);
) # grab from the 6th character to the end of the string
) my $match2=substr($string,5);
) # replace all of the 2nd set of matched chars with asterisks
) $match2 =~ tr/[0-9][a-z][A-Z]/*/;
) # join both parts
) my $new_string = "$match1$match2";
) print "$new_string\n";
)
) But surely, there must be a better way to do this, preferably a one-
) liner. Any ideas?
To keep most in line with your program:
substr($string, 5) =~ tr/[0-9][a-z][A-Z]/*/;
You see, you can use the result of subsrt as an lvalue.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Wed, 19 Aug 2009 10:48:45 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: replace everything from the 6th character on with asterisks
Message-Id: <%VWim.114800$9P.77486@newsfe08.iad>
jaredsubman@yahoo.com wrote:
> The code I have to do this so far is:
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
> print "$string\n";
> # grab first 5 characters
> my $match1=substr($string,0,5);
> # grab from the 6th character to the end of the string
> my $match2=substr($string,5);
> # replace all of the 2nd set of matched chars with asterisks
> $match2 =~ tr/[0-9][a-z][A-Z]/*/;
> # join both parts
> my $new_string = "$match1$match2";
> print "$new_string\n";
>
> But surely, there must be a better way to do this, preferably a one-
> liner. Any ideas?
$ perl -le'
my $string = q/ABCDEFG8IJK2MNOPbRST7VW5YZ/;
print $string;
substr( $string, 5 ) =~ tr//*/c;
print $string;
'
ABCDEFG8IJK2MNOPbRST7VW5YZ
ABCDE*********************
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
------------------------------
Date: Wed, 19 Aug 2009 11:07:47 -0700
From: sln@netherlands.com
Subject: Re: replace everything from the 6th character on with asterisks
Message-Id: <udfo85pf2dak5755dd2cifqnk6rnhah2ga@4ax.com>
On Wed, 19 Aug 2009 08:03:31 -0700 (PDT), "jaredsubman@yahoo.com" <jaredsubman@yahoo.com> wrote:
>The code I have to do this so far is:
>
>#!/usr/bin/env perl
>
>use strict;
>use warnings;
>
>my $string = 'ABCDEFG8IJK2MNOPbRST7VW5YZ';
>print "$string\n";
># grab first 5 characters
>my $match1=substr($string,0,5);
># grab from the 6th character to the end of the string
>my $match2=substr($string,5);
># replace all of the 2nd set of matched chars with asterisks
>$match2 =~ tr/[0-9][a-z][A-Z]/*/;
># join both parts
>my $new_string = "$match1$match2";
>print "$new_string\n";
>
>But surely, there must be a better way to do this, preferably a one-
>liner. Any ideas?
>Thanks in advance.
Yes, as Willem said, as an lvalue its an alias to a mechanism
that knows how to write to that location. So you can to tr/// on it
or anything you could on a normal variable, within the rules of substr.
substr($string,5) =~ tr/[0-9][a-z][A-Z]/*/;
same as
${\substr($string,5)} =~ tr/[0-9][a-z][A-Z]/*/;
or can get a reference and do it later
$ref = \substr($string,5);
$$ref =~ tr/[0-9][a-z][A-Z]/*/;
-sln
------------------------------
Date: Wed, 19 Aug 2009 10:56:54 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: revisiting web development in Perl...where to start?
Message-Id: <slrnh8nfim.4g2.hjp-usenet2@hrunkner.hjp.at>
On 2009-08-18 20:30, Jamie <nospam@geniegate.com> wrote:
> In <4b48baf8-36a7-49fd-b891-f7df5765cd09@k6g2000yqn.googlegroups.com>,
> ccc31807 <cartercc@gmail.com> mentions:
>>On Aug 17, 12:33=A0pm, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
>>> If you use mod_perl to execute your scripts, they are *not* standard CGI
>>> scripts. The CGI specification mandates that each invocation of a CGI
>>> script creates a new process which uses the environment, stdin and
>>> stdout to communicate with the server. mod_perl doesn't do this.
>>
>>It's interesting to note that a number of books advocating different
>>technologies often have a section near the front as to why their
>>technology is superior to the others. Most of these sections feature
>>Perl as one of the 'big' rivals, and most slam Perl by accusing it of
>>using CGI and therefore being slow and creating a lot of unnecessary
>>overhead.
>
> I usually see it slammed because it's older and therefore, inferior.
>
> If you're using mod_perl, I highly recommend checking into FastCGI, for
> 90% of the things a web app is going to do, FastCGI handles it better
> than mod_perl (IMO), because it lives outside the web server as a separate
> daemon process, you can keep apache lean & trim plus, it's much easier
> to work with. (I found it to be more elegant anyway)
I second that recommendation.
>>You are absolutely right about the original meaning of CGI, but these
>>days many people use CGI to refer to a style of coding, using Perl,
>>Python, Ruby, and maybe some other languages.
Can you describe this "style of coding"?
>>I'm guilty of this myself, since I describe what I do as Perl/CGI,
>>even though the last time I actually wrote a real CGI script was about
>>2001.
>
> I always thought of CGI as a kind of protocol, didn't have to be a new
> process, just the way the web server communicated with another process
> using stdio and environment variables. (never realized it /had/ to be a
> new process)
The CGI protocol (from the viewpoint of the CGI script) is basically:
* At startup, information about the header of the request plus some
other information is in the environment.
* The body of the request (if any) can be read from stdin.
* The response (including headers) must be written to stdout.
The end of the response is signalled by closing stdout.
At least on Unix-like systems (and this where CGI was invented) this
means that a process running a CGI script cannot serve more than one
request: There is no way it could get a new environment, and it cannot
reopen stdout. So the server must start a new process for the next
request.
FastCGI got around this by
* transmitting all the information from the web server to the fastcgi
script through the socket.
* Designing the protocol in such a way that both sides can signal "I am
finished with this request" without closing the communication
channel.
So the FastCGI script can continue running, and the web server can just
send one request after the other to it.
Java application servers use similar techniques.
hp
------------------------------
Date: Wed, 19 Aug 2009 07:46:39 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: revisiting web development in Perl...where to start?
Message-Id: <bcb02654-5430-4311-8888-94c39648e580@a26g2000yqn.googlegroups.com>
On Aug 19, 4:56=A0am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> >>You are absolutely right about the original meaning of CGI, but these
> >>days many people use CGI to refer to a style of coding, using Perl,
> >>Python, Ruby, and maybe some other languages.
>
> Can you describe this "style of coding"?
Yes. I can't say this is a global categorization, so please don't take
this too literally, but maybe as an impression and not as mutually
exclusive methods or styles.
HTML by itself is simply a markup language, it's not a programming
language. There are two ways to use HTML with programming: you can
either write the HTML and embed the programming elements in the HTML,
like PHP or JSP, or you can write the application and have it emit
HTML as output.
It's been my impression over the years that when people say 'CGI' they
don't mean using the CGI protocol in a literal sense, but writing
scripts that output HTML.
In my case, I write a lot of scripts that are heavy on the
computational side and light on the output side. For example, I just
finished a script that input two data files, one of about 37,000
records, chopped them to pieces and rearranged the data, and output
about 85 PDF files, 1 CSV file, and several HTML files. I don't know
what you would call this style of programming, but with respect to the
generation of HTML files I would call it CGI using Perl, not because
it uses the CGI protocol in any way, but because it's the way I once
wrote CGI scripts (and continue to write web applications, whether in
Perl, Python, or something else.)
I know that this isn't an accurate use of the term, but it conforms
with how the term seems to be used by the world at large.
CC
------------------------------
Date: Wed, 19 Aug 2009 10:55:02 -0700 (PDT)
From: Eli Morris-Heft <eli.morris.heft@gmail.com>
Subject: Re: revisiting web development in Perl...where to start?
Message-Id: <b099abf8-43d8-467e-a762-32ea572c1b62@26g2000yqk.googlegroups.com>
On Aug 18, 5:15=A0pm, Nathan Keel <na...@gm.ml> wrote:
> Jamie wrote:
> > I suspect most of the php coders out there have PHP as their first
> > "language" and aren't programmers so much as web designers, they
> > figure out how to do a couple things with it and walla, they jump into
> > writing full-fledged php applications that aren't planned out.
>
> That's exactly what I've seen. =A0Of course, none of us are claiming ther=
e
> aren't skilled coders in PHP, but far more unskilled people code in PHP
> than in Perl.
Ack. A bit of a self-fulfilling prophecy there. While it's not
guaranteed that most of the PHP coders out there have PHP as their
first language, I'll be a lot of them do - which is why they code in
PHP. From my professional experience, the closest most web designers
get to a programming language is either HTML (and many don't learn
that too well) or Flash (which isn't a programming language, though
Actionscript is, and 3.0 is actually a fairly respectable one - did I
write that?).
Both PHP and Perl suffer from being languages that are easy to start
learning, but difficult to build out in. They tend to breed
overconfidence from novice programmers, as sometimes exhibited on this
very newsgroup and its sister comp.lang.php. The fault, though, isn't
in the languages. It's in the programmer who fails to design, fails to
recognize limits, fails to ask before leaping. (And, really, which one
of us hasn't been that programmer at one time or another?)
From my experience, there's a lot of unskilled people out there coding
in Perl too - it's just that their scripts stay in their home
directories on their servers rather than being served for the whole
world to judge.
EMH, JAP{HP,erl}H
------------------------------
Date: Wed, 19 Aug 2009 11:02:28 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Rounding issue
Message-Id: <slrnh8nft4.4g2.hjp-usenet2@hrunkner.hjp.at>
On 2009-08-18 16:27, Jens Thoms Toerring <jt@toerring.de> wrote:
> Marten Lehmann <lehmannmapson@cnm.de> wrote:
>> Ben proposed using the bignums pragma, but I'm afraid that this breaks
>> something else in the code.
>
> That you will need if the numbers you use ints and end up with
> are too large to be stored in a simple int.
Be careful here. Perl doesn't have an "int" type. It has scalars, which
are kind of a mixture between string, signed int, unsigned int and
double, with conversions trown in as needed. You can assume that you can
exactly represent any integral value in the range +/- 2**53.
hp
------------------------------
Date: Wed, 19 Aug 2009 11:25:25 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Rounding issue
Message-Id: <slrnh8nh86.4g2.hjp-usenet2@hrunkner.hjp.at>
On 2009-08-18 19:30, Willem <willem@stack.nl> wrote:
> sln@netherlands.com wrote:
> ) I don't understand your big concern. Anytime you see numbers, decimal
> ) points, or anything printed in a view from a computer is a rounded
> ) representation of what the internal variable contains.
>
> No it isn't. It can also be an exact representation. For example,
> if the numbers are integers or some kind of decimal format.
The latter is misleading. The decimal format is neither necessary nor
sufficient for an exact representation.
Decimal fractions can be represented exactly in base 10
Binary fractions can be represented exactly in base 2.
So, *if* you need to represent values like 115/100 (which happens to be
common in finance, not even the UK uses shilling and pence any more),
they can be represented exactly in decimal, but not in binary.
But if you need other fractions, decimal won't help you. For example, if
you an American and have decided to store some length measurements in
feet. And now you need to store a length of 1 inch. Well that's 1/12 of
a feet, and there is no way to represent this exactly in a decimal
floating (or fixed) point number. Either you have to round it (and then
it doesn't matter much whether you round to decimal or binary) or you
have to rewrite your program to store everything in inches.
> ) Its just a view, its a one way snapshot of the contents of the variable.
> ) That doesen't affect the internal calculations. Indeed for percentage
> ) calculations, there is absolutely nothing wrong with floating point
> ) at all.
>
> Yes there is. Percentage calculations are fixed point,
How is a percentage calculation "fixed point"? Financial calculations
are usually fixed point, but a percentage calculation per se isn't.
For example, 19 % of 7.55 is 1.4345, but if that is an amount of money,
it will be rounded to either 1.43 or 1.44.
> and to get exact results, you need to be doing them in an exact
> representation such as integers or rationals.
Integers won't help in general. For example, if 8.99 is 119 %, how much
are 100 %? With rationals, you can represent the result exactly
(899/119), but with an integer, fixed, or floating point representation
you can't (unless you happen to use base 119 which isn't likely).
Of course in finance you don't care about the exact result of the
computation, but about the result rounded to a specific number of digits
(usually 2, sometimes 3 or even 5) and for that a fixed point (or
integer) representation is nice.
hp
------------------------------
Date: Wed, 19 Aug 2009 12:05:12 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Rounding issue
Message-Id: <slrnh8njio.4g2.hjp-usenet2@hrunkner.hjp.at>
On 2009-08-18 22:57, Jürgen Exner <jurgenex@hotmail.com> wrote:
> pacman@kosh.dhis.org (Alan Curry) wrote:
>>In article <j12m855c4rvjkbqtk39ls6psdgh8mu1l5o@4ax.com>,
>>Jürgen Exner <jurgenex@hotmail.com> wrote:
>>>No, Perl does not provide BCD natively (Binary Coded Decimals; someone
>>>else already pointed that out). The reason is simple: they not only take
>>>up more space (probably a minor issue on modern hardware) but more
>>>important most hardware does not support BCD arithmetic, therefore all
>>>calculations would have to be handcoded in software, making them very
>>>slow.
>>
>>Does anyone else find this argument ("We have to use the CPU's floating-point
>>because anything else is too slow") a bit odd?
>
> Please note I said "very slow", I didn't say "too slow". The first is a
> comparison against hardware supported arithmetic and can be measured.
However, we are talking about perl here, not machine code. How much of
the time spent executing a perl "multiply" operator is spent
multiplying, and how much is spent manipulating the stack, checking
whether the operands are NVs or UVs or PVs, etc.?
Even if the actual multiplication is 10 or 100 times slower, how much
slower does the "multiply" operator become? And how much slower does the
whole program (which presumably does other things besides multiplying)
become?
[rearranged]
> Well, you can always do a benchmark test.
Yes, you would need to do that. It's very hard to guess how much the
effect would be.
>>Did we not once have CPUs with no floating point at all?[1] It was all
>>"in software", and at about 1MHz too. Still, somehow, things got
>>done.
>
> Of course, the question is more if you are patient enough to wait that
> long considering today's applications. Somehow I have a feeling that
> viewing a video or running a graphic intensive game on a 386 wouldn't
> be much fun ;-) .
Few codecs or 3d engines are written in Perl, AFAIK.
>>Will there ever be a time when our need for speed is satiated, and we can
>>start thinking about making it easier to get correct results, instead of
>>bad results quickly? Can a well-rounded, general-purpose language like perl
>>ever decide that computers are fast enough now, that accuracy-preserving
>>"bignum" should be the default mode, and FPU speed freaks should be the ones
>>importing a special module to meet their needs?
>
> I'm not sure if that's really the issue.
>
> There are only very few areas where a number has to be accurate to the
> last digit, finance being the most obvious.
[lots of good examples snipped]
I fully agree.
hp
------------------------------
Date: Wed, 19 Aug 2009 10:03:19 -0700
From: sln@netherlands.com
Subject: Re: search for hex characters in a binary file and remove them
Message-Id: <gsbo8556fhl09o0f6goe8im06ig2idjh30@4ax.com>
On Tue, 18 Aug 2009 08:34:49 -0700 (PDT), venkateshwar D <venkateshwar1981@gmail.com> wrote:
>Thanks a lot. This does not seem to be working. it is doing a binary
>file copy.
>
>I want to search for that pattern in the binary file (000002020100)
>(it is hex character file) and remove this pattern + the next 18 bytes
>in the file.
>thanks
>venkat
How did you make out, any luck?
Try this sample and see if it is similar to what you have.
-sln
------------------------
use strict;
use warnings;
open my $ftest, '>', 'dummy.txt' or die "can't create dummy.txt: $!";
for (1 .. 2_000)
{
print $ftest "$_ 0000000000000000000 111111111111111111111\n";
}
print $ftest "sequence line: 0000000000000000000 <000002020100555555555555555555>111\n";
for (2_001 .. 4_000)
{
print $ftest "$_ 0000000000000000000 111111111111111111111\n";
}
close $ftest;
open my $fin, '<:raw', 'dummy.txt' or die "can't open input file: $!";
open my $fout, '>:raw', 'dummy_o.txt' or die "can't open output file: $!";
my ($chunksize, $found) = (4096,0);
{
local $/ = \$chunksize;
my ($keep, $buf, $data) = (50,'','');
while (defined ($data = <$fin>))
{
$buf .= $data;
$found = 1 if (not $found and $buf =~ s/000002020100.{18}//s);
print $fout substr( $buf, 0, -$keep, "");
}
print $fout $buf;
}
if (!$found) {
print "Did not match sequence: '000002020100.{18}'\n";
}
close $fout;
close $fin;
__END__
------------------------------
Date: Wed, 19 Aug 2009 09:15:54 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: undef($foo) versus $foo = undef()?
Message-Id: <ef-dnfSaM5snKRbXnZ2dnUVZ8rNi4p2d@brightview.co.uk>
Tim McDaniel wrote:
> In article <87ws508oq7.fsf@quad.sysarch.com>,
> Uri Guttman <uri@stemsystems.com> wrote:
>>>>>>> "TM" == Tim McDaniel <tmcd@panix.com> writes:
>> TM> Is there any reason to prefer one of
>> TM> $foo = undef;
>>
>> TM> versus
>>
>> TM> undef $foo;
>>
>> i prefer NEITHER. explicitly undefing a scalar usually signals a poor
>> design. usually you let variables exit scope and upon reentry they
>> get cleared by the my declaration.
>
> Yes. I do try to limit the scope of variables by using blocks, often
> not connected to if or for, just
>
> {
> my %unique_xes;
> ...
> }
>
> or whatever.
>
> But there have been a few cases where variable scopes don't nest
> nicely, so I explicitly kill the variable as soon as I can.
Yes - I find typically, even with block-limited scope, you need SOME variables
to transfer data from block-to-block; variables that don't need
function-wide scope, but need-wider-than-block scope.
BugBear
------------------------------
Date: Wed, 19 Aug 2009 10:31:28 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: undef($foo) versus $foo = undef()?
Message-Id: <slrnh8ne31.4g2.hjp-usenet2@hrunkner.hjp.at>
On 2009-08-19 06:28, Tim McDaniel <tmcd@panix.com> wrote:
> In article <87pras749b.fsf@quad.sysarch.com>,
> Uri Guttman <uri@StemSystems.com> wrote:
>>>>>>> "TM" == Tim McDaniel <tmcd@panix.com> writes:
>> TM> Is there any functional difference between
>> TM> undef @bar;
>> TM> or
>> TM> @bar = ();
>> TM> (and similarly for %baz)?
>>
>>yes. do one and the other and then test with defined. one is true and
>>the other is false. the fact that you can even call defined on an
>>aggregate is really a bug. p6 is eliminating that iirc.
>
> "man perlfunc" says
>
> Use of "defined" on aggregates (hashes and arrays) is deprecated.
> It used to report whether memory for that aggregate has ever been
> allocated. This behavior may disappear in future versions of
> Perl. You should instead use a simple test for size:
>
> if (@an_array) { print "has array elements\n" }
> if (%a_hash) { print "has hash members\n" }
Perhaps surprisingly, on at least some versions of perl,
if (keys %a_hash)
was substantially faster than
if (%a_hash)
If your script spends a lot of time checking whether a hash is empty,
you might want to benchmark both versions.
>>but that creates a list of the values first. my code iterates over
>>the values with less storage needed and more speed.
>
> What made me wonder was
[...]
> Note that the values are not copied, which means modifying them
> will modify the contents of the hash:
>
> for (values %hash) { s/foo/bar/g } # modifies %hash values
> for (@hash{keys %hash}) { s/foo/bar/g } # same
>
> particularly that last. It could be generating a list for
> values(%hash) and doing further magic to point to the real values
> (multiplying the storage needed),
That's what it does (at least until 5.8.8, maybe it changed in 5.10).
> or maybe there's an optimization in
> such a simple case to make values() work as efficiently as each()?
Nope. Which makes each a lot more efficient for large hashes.
> I've seen people post timing test results here, but I've not looked
> into the modules that conveniently implement them.
I don't have any numbers at hand, but I've had a few scripts where
replacing values with each yielded rather impressive speedups (not to
mention that in some cases it was needed to fit into the 3GB limit of
32-bit Linux).
hp
------------------------------
Date: Wed, 19 Aug 2009 10:28:31 -0700 (PDT)
From: boman <shambo_p@yahoo.com>
Subject: windows one liner to output unix line feed
Message-Id: <cd567a83-53f8-43a0-b94a-d7c47a1f77a9@l35g2000vba.googlegroups.com>
I have a simple one liner running on Windows that does a substitution.
However, with the -p option, the line endings are coming out \r\n, and
I need them to be just \n.
I searched for the -l option, but couldn't find how to specify unix
line breaks on output
Here's the code:
perl -pi.bak -e "s|foo|bar|g" myfile.txt
thanks
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2561
***************************************