[26140] in Perl-Users-Digest
Perl-Users Digest, Issue: 8331 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 18 11:05:33 2005
Date: Thu, 18 Aug 2005 08:05:12 -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 Thu, 18 Aug 2005 Volume: 10 Number: 8331
Today's topics:
[Socket]What is the bast solution? <sonet.all@msa.hinet.net>
Re: [Socket]What is the bast solution? <vek@station02.ohout.pharmapartners.nl>
coding style and performance <ngoc@yahoo.com>
Re: coding style and performance <no@email.com>
Re: coding style and performance <ngoc@yahoo.com>
Re: coding style and performance <vek@station02.ohout.pharmapartners.nl>
Re: coding style and performance <ngoc@yahoo.com>
Re: coding style and performance <Peter@PSDT.com>
Re: detecting french character into a file? (Anno Siegel)
Re: How to add directory to include - Newbie (Anno Siegel)
Re: Interfaces and Type Safety, Dynamic Typing (OOP New <tassilo.von.parseval@rwth-aachen.de>
Re: Organizing data for readability and efficiency (Anno Siegel)
Re: Organizing data for readability and efficiency <Mark.Seger@hp.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 18 Aug 2005 20:19:41 +0800
From: "sonet" <sonet.all@msa.hinet.net>
Subject: [Socket]What is the bast solution?
Message-Id: <de1ug4$e9g$1@netnews.hinet.net>
noneblocking socket
io::poll
thread
prefork
If i will handle a chat server that have 1000+ connection in the same time.
What is the bast solution? Or apache (mod_perl2 preconnectionHander)...?
------------------------------
Date: 18 Aug 2005 13:48:52 GMT
From: Villy Kruse <vek@station02.ohout.pharmapartners.nl>
Subject: Re: [Socket]What is the bast solution?
Message-Id: <slrndg94e4.34c.vek@station02.ohout.pharmapartners.nl>
On Thu, 18 Aug 2005 20:19:41 +0800,
sonet <sonet.all@msa.hinet.net> wrote:
> noneblocking socket
> io::poll
> thread
> prefork
>
> If i will handle a chat server that have 1000+ connection in the same time.
> What is the bast solution? Or apache (mod_perl2 preconnectionHander)...?
>
It depends more on how many new connections comes in per unit of time.
Once a call is accepted a process per connection seems more effecient, but
accepting the call and creating a new process is more expensive.
Villy
------------------------------
Date: Thu, 18 Aug 2005 14:18:38 +0200
From: ngoc <ngoc@yahoo.com>
Subject: coding style and performance
Message-Id: <43047c9d$1@news.broadpark.no>
Hi
Look at some code I see
for my $element (@array) {
..do something..
}
I think is not performance attractive because of
the interpreter will create new variable (my $element) for every loop step.
I prefer
my $element;
for $element (@array) {
..do something..
}
The interpreter will NOT create new variable for every loop step. It
just updates current one.
What I think is right or wrong?
thanks
------------------------------
Date: Thu, 18 Aug 2005 13:23:11 +0100
From: Brian Wakem <no@email.com>
Subject: Re: coding style and performance
Message-Id: <3mjctfF16quc9U1@individual.net>
ngoc wrote:
> Hi
> Look at some code I see
> for my $element (@array) {
> ..do something..
> }
> I think is not performance attractive because of
> the interpreter will create new variable (my $element) for every loop
> step.
>
> I prefer
>
> my $element;
> for $element (@array) {
> ..do something..
> }
> The interpreter will NOT create new variable for every loop step. It
> just updates current one.
>
> What I think is right or wrong?
> thanks
What results did you get when you benchmarked them?
--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
------------------------------
Date: Thu, 18 Aug 2005 14:58:25 +0200
From: ngoc <ngoc@yahoo.com>
Subject: Re: coding style and performance
Message-Id: <430485f0$1@news.broadpark.no>
How to benchmark perl script?
Brian Wakem wrote:
> ngoc wrote:
>
>
>>Hi
>>Look at some code I see
>>for my $element (@array) {
>> ..do something..
>>}
>>I think is not performance attractive because of
>>the interpreter will create new variable (my $element) for every loop
>>step.
>>
>>I prefer
>>
>>my $element;
>>for $element (@array) {
>> ..do something..
>>}
>>The interpreter will NOT create new variable for every loop step. It
>>just updates current one.
>>
>>What I think is right or wrong?
>>thanks
>
>
>
> What results did you get when you benchmarked them?
>
>
------------------------------
Date: 18 Aug 2005 13:43:55 GMT
From: Villy Kruse <vek@station02.ohout.pharmapartners.nl>
Subject: Re: coding style and performance
Message-Id: <slrndg944r.34c.vek@station02.ohout.pharmapartners.nl>
On Thu, 18 Aug 2005 14:18:38 +0200,
ngoc <ngoc@yahoo.com> wrote:
>
> I prefer
>
> my $element;
> for $element (@array) {
> ..do something..
> }
> The interpreter will NOT create new variable for every loop step. It
> just updates current one.
Then again, the loop won't use the $element you create before entering
the loop, it will use a new loop variable. Try assing a value to $element
before entering the loop and check its value after you leave the loop.
VIlly
------------------------------
Date: Thu, 18 Aug 2005 16:13:46 +0200
From: ngoc <ngoc@yahoo.com>
Subject: Re: coding style and performance
Message-Id: <43049799$1@news.broadpark.no>
Villy Kruse wrote:
> On Thu, 18 Aug 2005 14:18:38 +0200,
> ngoc <ngoc@yahoo.com> wrote:
>
>
>
>>I prefer
>>
>>my $element;
>>for $element (@array) {
>> ..do something..
>>}
>>The interpreter will NOT create new variable for every loop step. It
>>just updates current one.
>
>
> Then again, the loop won't use the $element you create before entering
> the loop, it will use a new loop variable. Try assing a value to $element
> before entering the loop and check its value after you leave the loop.
>
> VIlly
I have tested what you suggest and It is as you say. But I still do not
understand the logic.
If the loop create a new variable with the same name as my own variable,
So there are two variables with the same name.
1. My own variable will not conflict with loop's variable, because my
variable is 'global' and loop variable is 'local' (scoping).
2. If a new variable is created without a 'my' in front of it, why 'use
strict;' do not react?
------------------------------
Date: Thu, 18 Aug 2005 14:20:37 GMT
From: Peter Scott <Peter@PSDT.com>
Subject: Re: coding style and performance
Message-Id: <pan.2005.08.18.14.20.24.419533@PSDT.com>
On Thu, 18 Aug 2005 14:58:25 +0200, ngoc wrote:
> How to benchmark perl script?
Go to http://www.google.com. Type in
how to benchmark perl script?
Read the first hit.
--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
------------------------------
Date: 18 Aug 2005 12:40:35 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: detecting french character into a file?
Message-Id: <de1vk3$jki$1@mamenchi.zrz.TU-Berlin.DE>
Christian Lavenne <clavenne@lucent.com> wrote in comp.lang.perl.misc:
If you have an unrelated question, it is better to start a new thread.
> Could someone provide me with a script detecting the french character into a
> file?
> I don't know into which CPAN module I must use to do it.
You don't need a module to do that. Untested:
my $french = "..."; # list of "French" characters
my $found;
while ( <$file> ) {
$found = /[$french]/ and last;
}
if ( $found ) {
# ...
}
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: 18 Aug 2005 11:04:24 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: How to add directory to include - Newbie
Message-Id: <de1pvo$fi9$1@mamenchi.zrz.TU-Berlin.DE>
NewBie <leongyc@tp.edu.sg> wrote in comp.lang.perl.misc:
> Hi,
>
> Appreciate any advice to overcome the following:
>
> Can't locate RRDs.pm in @INC (@INC contains: /opt/orca/lib) at ./orca line
> 33.
> BEGIN failed--compilation aborted at ./orca line 33.
Your setting of @INC with only one directory on it is unusual. Either
you have a misconfigured perl, or you have changed it in the script
you run. Since you show no code, we can only guess.
> Where RRDs.pm can be located in other directory.
Put that other directory on @INC.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Thu, 18 Aug 2005 13:33:31 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: Interfaces and Type Safety, Dynamic Typing (OOP Newbie in Perl)
Message-Id: <slrndg8sgb.1pp.tassilo.von.parseval@localhost.localdomain>
Also sprach Veli-Pekka Tätilä:
> Tassilo v. Parseval wrote:
><snipped heavy-handedly>
>> Also sprach Veli-Pekka Tätilä:
>>> Is there a good way to implement an interface in Perl or some perlish
>>> idiom
>> write a package where each method dies on invocation.
> Ah, I see-. This semed like a crude solution until you showed the way to
> make multiple methods die without repeating the bulk of the code
> redundantly.
>
> Dying is at least better than doing a pure NOP. That way you know right away
> that you did something wrong. It's called defensive programming, though
> sometimes Perl is not as defensive as I'd like. Again PHP is about as
> defensless (is that the opposite?) as can be by default.
So PHP is offensive. ;-)
>> That is true. Mistyped method invocations are caught at runtime. They
>> have to be due to Perl's super-polymorphic nature.
> Oh ok. That's still a bit nasty, considering you can force strict refs, vars
> and so on, and of course normal subs canot be polymorphic so there names can
> be checked at runtime just fine. But could you give me an example of a real
> world project or module where dynamic typing is used to achieve something
> that would be very difficult to do in a strongly and statically typed
> language?
I don't know the innards of every Perl program or library ever written
but such a case can be easily conceived. Perl's lack of compile-time
checks can have a significant impact on the interface of a library.
Consider a module that is used to write XML tags as generically as
possible. Every tag should have its own method where the parameters are
attributes (we don't care about well-formedness here, but it could be
added using an internal stack of tags used to record the state). So this
code
$writer->foo(key => 'value')
->bar(key1 => 'another_value', key2 => 42)
->baz(key1 => '<>')
->foo;
should output
<foo key="value"/>
<bar key1="another_value" key2="42"/>
<baz key1="<>"/>
<foo/>
Obviously, the assumption is that the number of different possible tags
is indefinite or at least big enough that writing one method for each of
them would be quite painful. With Perl all you need is one special
method to handle it all. This special method will then for efficiency
generate and compile the requested method on the fly:
package Writer;
use HTML::Entities;
sub new { return bless \my $d => shift }
sub AUTOLOAD {
(my $methname = $AUTOLOAD) =~ s/.*:://;
return if $methname eq 'DESTROY';
*$AUTOLOAD = sub {
my ($self, %args) = @_;
print "<$methname";
while (my ($key, $val) = each %args) {
print " $key=\"", encode_entities($val), "\"";
}
print "/>\n";
return $self; # allow method-chaining
};
# replace current AUTOLOAD instance with
# instance of the method just compiled
goto &$AUTOLOAD;
}
1;
This approach has at least three advantages: First of all, the interface
is very convenient (that of course could have also been achieved through
a different implementation).
Secondly, the implementation is very concise: With a finite amount of
typing you have created an infinite amount of methods.
And finally, this is also quite efficient CPU-wise. The first invocation
of a method will be slow as perl eventually has to dispatch it to
AUTOLOAD. However, AUTOLOAD creates a real method so each subsequent
invocation of the same method will no longer end up in AUTOLOAD but it
will instead call the previously generated method. Also, this saves
compile-time as there is really not much code that needs to be compiled.
If you have a static library with maybe 50 methods, all of them need to
be compiled, no matter if your program eventually only calls three or
four of them.
It may be worthwhile to do a CPAN search for the string 'sub AUTOLOAD'.
You can use <http://cpansearch.bulknews.net/> for that. Note that h2xs,
the tool to pregenerate package-stubs, normally puts an AUTOLOAD into
every module. So only consider those cases where AUTOLOAD does actually
do something interesting. There should still be many of such examples.
> I've noticed that many OOP features appear to have very littel real-world
> significance first but there are special cases in which they may be
> essential. Take reflection and friend classes as examples. I was
> implementing a simple XML RPC server in Java for testing purposes and
> noticed that the lib I was using was able to automatically register certain
> methods based on their type and name. Where as in PHP you would have to
> manually fill in a method dispatch map, of course the manual way was present
> in the Java implementation, too. And as for friends, I was testing some
> badly written awt code with J-Unit (is there a P-Unit or something for
> Perl?), and dearly wished I could just say hey gimme full access when I'm
> testing. In stead I had to manually hack the code changing private parts to
> public in places.
There is nothing intrinsically wrong with having enforced privacy. It's
just the way how many languages handle it. Of course, it might be useful
if their compilers and interpreters had a switch or pragma to turn it
off for testing purposes.
As for P-Unit, I am not entirely familiar with what J-Unit does. As far
as I know it's some sort of testing framework. Similar things exist for
Perl. See all the Test:: modules on the CPAN that are used for most CPAN
modules when you do 'make test'.
>> sub func (\@) {
>> "I must receive an array";
>> my $ary_ref = shift; # it's passed as reference
>> }
> Hey thanks for this simple piece of code. I was under the impression that
> you had to put a separate prototype at the top as in C, rather than
> specifying it only one time in the err sub signature (compare to Java).
Perl does have forward-declarations as well. The case above works if the
function definitions comes before the code that calls this function.
Otherwise the prototypes wont work. If you want to put the defintion at
the bottom, you can use a forward-declaration, just as in C:
sub func (\@);
...
func @ary;
sub func (\@) {
...
}
>> Run-time checks such as isa() or ref() are the
>> ones most commonly found to catch type violations.
> That makes sense and is probably also sufficient for most cases. after all
> it is unlikely you would assign the wrong type of reference if knowing the
> class hierarchy at some level.
You as the author of a piece of software maybe not, but it's more
difficult when you write a library used by other people. You always have
to be prepared for programmers using your library in a way it was not
intended. So a certain amount of checks need be there. It would of
course be nice if some of these checks could be carried out at
compile-time. But then, certain languages have to be better than Perl for
something. :-)
><module internals>
>> This very liberal approach may be frowned upon by the Ada language
>> designers, however each has its own virtues and shortcomings.
> Agreed. Getting slightly OT in this thread again: sometimes abstraction can
> be a hindrance, too. I like the fact that Perl has got regexp in the
> language. If I need to do them in Java, I write and test the Perl code first
> if possible.
Ah, I am sure Java wraps them in classes so that a simple pattern match
wont fit on one screen any longer. ;-)
Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
------------------------------
Date: 18 Aug 2005 11:21:36 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Organizing data for readability and efficiency
Message-Id: <de1r00$gei$1@mamenchi.zrz.TU-Berlin.DE>
Mark Seger <Mark.Seger@hp.com> wrote in comp.lang.perl.misc:
> I know this can be a very long, complex topic and am hoping by using a
> specific example can keep things more focused, but I also suspect not. 8-)
>
> Anyhow I've put together a script for generating a variety of plots
> using perl/Tk from a set of tables. Each table describes what a plot
> looks like, for example its title, Y-acess limits, the variables it can
^^^^^
axis?
> plot, etc.
>
> To get things going quickly, I stored each in a array named for what the
> data is and indexed by the plot number. So $title[0] is the title of
> the first plot, $ymax[2] is upper bound of the yaxis of the 3rd, etc.
> While easy to understand there are too many arrays to pass around to
> various routines and I have therefore made them all globals.
That is bad style. You are right in looking for an alternative.
> What I'm now looking for are alternatives data organizations that could
> allow me to store all the information in a single data structure that
> could be passed to different routines but still be easily readable as
> well as efficient.
[various alternatives: LoL, HoL]
Put the description of each plot into a hash with usefully named keys.
Then make a list of those:
my @plots;
push @plots, {
title => 'Plot 13',
ymax => 120,
# ...
};
push @plots, {
title => 'Plot 14',
ymax => 1.5,
# ...
};
That way you have everything you need for one plot in one hash and
you can iterate over all plots:
for my $descr ( @plots ) {
$window->label( $descr->{ title});
# ...
}
That looks like the most natural arrangement of your data.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Thu, 18 Aug 2005 09:11:13 -0400
From: Mark Seger <Mark.Seger@hp.com>
Subject: Re: Organizing data for readability and efficiency
Message-Id: <430488f2$1@usenet01.boi.hp.com>
> Put the description of each plot into a hash with usefully named keys.
> Then make a list of those:
>
>
> my @plots;
>
> push @plots, {
> title => 'Plot 13',
> ymax => 120,
> # ...
> };
>
> push @plots, {
> title => 'Plot 14',
> ymax => 1.5,
> # ...
> };
good to know I was on the right track with my thinking. now to add a
slightly differnt wrinkle, in some cases I'll have mulitple not single
elements such as multiple lines on the same plot and therefore a legend
with multiple plot titles and even mulitple Y-axis entries. how would I
mix in arrays with what you described above? can I have something like
ymax => @max
legend => @names
where max is an array of the maximums for each plot line?
And finally, how is the efficiency of all this? Something like a plot
title or legend is only going to be accessed once when I draw the plot,
but the ymax represents something I have to check against for every data
point. Are there any inefficiencies worth worrying about in accessing
that data within a hash? In general when I need to access something
100Ks of times I do worry about hashes vs arrays.
-mark
------------------------------
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 V10 Issue 8331
***************************************