[22127] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4349 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 6 06:05:37 2003

Date: Mon, 6 Jan 2003 03:05:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 6 Jan 2003     Volume: 10 Number: 4349

Today's topics:
    Re: Devel::DProf giving bad results? <goldbb2@earthlink.net>
    Re: Devel::DProf giving bad results? (tî'pô)
    Re: Devel::DProf giving bad results? <goldbb2@earthlink.net>
    Re: How can split ignore quoted characters? (Villy Kruse)
        how to force a reload on a page <rx323@xtra.co.nz>
    Re: how to force a reload on a page <jurgenex@hotmail.com>
    Re: how to force a reload on a page (Joe Smith)
        how to refer to the array? <ie_qjzhu@yahoo.ie>
    Re: how to refer to the array? <goldbb2@earthlink.net>
    Re: OO primer, please! <tassilo.parseval@post.rwth-aachen.de>
    Re: regex code evaluation (??{code}) <krahnj@acm.org>
    Re: regex code evaluation (??{code}) <goldbb2@earthlink.net>
    Re: regexp question (Joe Smith)
    Re: Should I worry about the optimizer. (tî'pô)
    Re: Should I worry about the optimizer. (tî'pô)
    Re: Should I worry about the optimizer. <goldbb2@earthlink.net>
    Re: Should I worry about the optimizer. (tî'pô)
    Re: very easy string Q. <dmcbride@naboo.to.org.no.spam.for.me>
    Re: very easy string Q. (Daniel S. Lewart)
    Re: What means sh: ^P: not found (Joe Smith)
    Re: Why doesn't this foreach SMTP loop work? (Joe Smith)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 06 Jan 2003 03:27:33 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Devel::DProf giving bad results?
Message-Id: <3E193DF5.C4AC1E33@earthlink.net>

Teh (tî'pô) wrote:
> 
> Hi, I've just downloaded Devel::DProf from cpan.org and I'm getting
> very suspicious results.
[snip irrelevant stuff]
> As for my system:
> % perl -v
> This is perl, version 5.005_02 built for sun4-solaris

Try running it under 5.8.0

> % uname -a
> SunOS sanjer 5.8 Generic_108528-11 sun4u sparc SUNW,Ultra-5_10
> 
> Here's my test code
> ----------------------------------
> #!/bin/env perl -w
> use strict;
> 
> sub pre_inc {
>         my $num = shift;
>         my $ret = 0;
>         ++$ret while $ret < $num;
>         return $ret;
> }
> 
> sub pst_inc {
>         my $num = shift;
>         my $ret = 0;
>         $ret++ while $ret < $num;
>         return $ret;
> }
> 
> my $targ = 10000;
> my $i = 0;
> $i += pre_inc($targ);
> $i += pst_inc($targ);
> print STDERR "Ooops.\n" if $i != ($targ *2);
> ------------------------------------------
> I run
> % /usr/local/bin/perl -d:DProf test.pl
> % dprofpp
> Total Elapsed Time = 0.159815 Seconds
>   User+System Time =  0.12981 Seconds
> Exclusive Times
> %Time ExclSec CumulS #Calls sec/call Csec/c  Name
>  38.5   0.050  0.050      2   0.0250 0.0250  main::BEGIN
>  30.8   0.040  0.040      1   0.0400 0.0400  main::pst_inc
>  0.00   0.000 -0.000      1   0.0000      -  strict::import
>  0.00   0.000 -0.000      1   0.0000      -  strict::bits

Clearly, the time taken in the pre_inc is being counted as time in
BEGIN.

I believe that the reason this occurs is because after BEGIN is run,
it's CV gets freed, and as a consequence, it's address will end up being
assigned to the next allocated CV.

Thus, the pre_inc sub has the same memory location as BEGIN had.  And
since Devel::DProf keeps track of what happens in which sub with a hash
which is keyed by the memory location of the sub being executed, all
execution that occurs in the later CV is credited to the earlier one.

> Where did pre_inc disapear to? Why isn't it showing up?

It got merged with BEGIN.  It (or at least, it's time) is showing up,
but not with the proper name.

> I expected my results to show that about half the time was spent in
> pre_inc and half in pst_inc.

Ignoring the name confusion, that's precisely what's happening.

Insert the line:

   sub dummy() {()} dummy;

Between 'use strict' and 'sub pre_inc {', and maybe the problem will go
away.

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


------------------------------

Date: Mon, 06 Jan 2003 10:35:11 +0200
From: "Teh (tî'pô)" <teh@mindless.com>
Subject: Re: Devel::DProf giving bad results?
Message-Id: <cbfi1vg31e8rol7pu2v87dp3942lehavq4@4ax.com>

Benjamin Goldberg bravely attempted to attach 85 electrodes of
knowledge
to the nipples of comp.lang.perl.misc by saying:
>Teh (tî'pô) wrote:
>> 
>> Hi, I've just downloaded Devel::DProf from cpan.org and I'm getting
>> very suspicious results.
>[snip irrelevant stuff]
>> As for my system:
>> % perl -v
>> This is perl, version 5.005_02 built for sun4-solaris
>
>Try running it under 5.8.0
Anno Siegel run it under 5.8.0 and got the same results (message ID
aun7eh$suj$1@mamenchi.zrz.TU-Berlin.DE).

<snip>

>Clearly, the time taken in the pre_inc is being counted as time in
>BEGIN.
>
>I believe that the reason this occurs is because after BEGIN is run,
>it's CV gets freed, and as a consequence, it's address will end up being
>assigned to the next allocated CV.
>
>Thus, the pre_inc sub has the same memory location as BEGIN had.  And
>since Devel::DProf keeps track of what happens in which sub with a hash
>which is keyed by the memory location of the sub being executed, all
>execution that occurs in the later CV is credited to the earlier one.

<snip>

>Insert the line:
>
>   sub dummy() {()} dummy;
>
>Between 'use strict' and 'sub pre_inc {', and maybe the problem will go
>away.

It did, thanks.

This is great, I feel I can trust DProf again!

What is a CV (Code Variable?), is this something that might happen
with other functions or just with BEGIN which is freed when
compilation is over?

Thanks again.


------------------------------

Date: Mon, 06 Jan 2003 04:16:36 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Devel::DProf giving bad results?
Message-Id: <3E194974.D0AB7A44@earthlink.net>

Teh (tî'pô) wrote:
[snip]
> This is great, I feel I can trust DProf again!
> 
> What is a CV (Code Variable?),

A CV is the C-level data structure which hodes a coderef (a perl
subroutine reference).

> is this something that might happen with other functions or just with
> BEGIN which is freed when compilation is over?

All BEGIN blocks are special -- as soon as any particular BEGIN block
finishes getting compiled, it's immediately run, then freed.  It's
because the BEGIN block is freed *before* perl starts parsing 'sub
pre_inc' that the confusion occurs.

So...  If you free a subroutine, and then compile another, then it's
possible for that other to get mistaken for the one that was just freed.

I'm not entirely certain how to intentionally provoke this into
happening, other than with the unfortunate case which you discovered.

As a guess, something like this might work:

   #!/bin/env perl
   sub foo {++$x for 1..10_000}
   sub bar {++$x for 1..10_000}
   sub baz {++$x for 1..10_000}
   BEGIN { foo(); bar(); baz(); undef &foo; undef &bar; undef &baz }
   sub narf {++$x for 1..10_000}
   sub point {++$x for 1..10_000}
   BEGIN { narf(); point(); undef &narf; undef &point; }
   sub egad {++$x for 1..10_000}
   egad();
   __END__
   [untested]

I'm not entirely certain what gets freed at what time, but I suspect
that some of these will "disappear" from Devel::DProf's output, due to
an earlier sub being freed, and the newly allocated sub ending up at the
same address as the old one.

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


------------------------------

Date: 06 Jan 2003 08:58:50 GMT
From: vek@station02.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: How can split ignore quoted characters?
Message-Id: <slrnb1ihaa.3cd.vek@station02.ohout.pharmapartners.nl>

On Mon, 6 Jan 2003 11:40:01 +1100,
    Andrew Hamm <ahamm@mail.com> wrote:


>Robert Nicholson wrote:
>> How do you specific the pattern for split to ignore the quoted
>> characters of the character you want to split on?
>>
>> ie. you want to split on ',' but you want to ignore \, in the text
>>
>> if I use
>>
>> split (/[^\\],/);
>>
>> that will chop of the last character of any matching field
>>
>> how can I match without chopping off that last character?
>
>You sound suspiciously like you are trying to handle CSV (Comma Separated
>Values) which allows quotes around fields, and escaping quotes etc. If so,
>the Text::CSV module wins hands-down.
>


Also there is an entire book on this and related subject: 
"Mastering Regular Expressions" (O'Reilly).  

Among other things it shows that there is more to CSV than one would
immeditely think of.  Therefore using a well tested CPAN module would
be a good idea.



Villy


------------------------------

Date: Mon, 06 Jan 2003 06:38:20 GMT
From: Rich <rx323@xtra.co.nz>
Subject: how to force a reload on a page
Message-Id: <1103_1041835100@TERIINI>

Can someone please explain how i can force my script to reload the html being displayed to the user.
I have tried to find the answer myself but cannot.
Thank you.




------------------------------

Date: Mon, 06 Jan 2003 07:40:22 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: how to force a reload on a page
Message-Id: <GpaS9.162$1c.139@nwrddc01.gnilink.net>

Rich wrote:
> Can someone please explain how i can force my script to reload the
> html being displayed to the user. I have tried to find the answer
> myself but cannot.

If you are talking about a Perl script used in a CGI environment (you
neglected to tell us) then this is not possible.
The browser decides when to requests a new page. Once the page has been send
there is no way for the server to influence the browser in any way. In other
words: if you want to reload a page then you will have to encode this in the
reply that the server sends.

How to do that you should ask in a NG that actually deals with web
programming, CGI, and HTML.

jue




------------------------------

Date: Mon, 06 Jan 2003 07:58:55 GMT
From: inwap@inwap.com (Joe Smith)
Subject: Re: how to force a reload on a page
Message-Id: <3HaS9.1740$io.78856@iad-read.news.verio.net>

In article <1103_1041835100@TERIINI>, Rich  <rx323@xtra.co.nz> wrote:
>Can someone please explain how i can force my script to reload the html
>being displayed to the user.

http://wp.netscape.com/eng/mozilla/4.8/relnotes/windows-4.8.html
  To force the update of individual elements on a page, you can use
  the following meta tags in the <HEAD></HEAD> section:
     <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
     <META HTTP-EQUIV="Refresh" CONTENT="60"> 

This is not a perl question.
	-Joe

-- 
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


------------------------------

Date: Mon, 6 Jan 2003 17:29:05 -0800
From: "qjzhu" <ie_qjzhu@yahoo.ie>
Subject: how to refer to the array?
Message-Id: <avbim8$1a55$1@mail.cn99.com>

dear all,

    if I have

        my %countyInfo = (
          '1' => ['CITRUS', ['32650', '32651', '32652']],
          '2' => ['ALACHUA', ['32653', '32654', '32655', '32656', '32657']],
         );

    how can I push a new value into both array ['32650', '32651', '32652']
and
    ['32653', '32654', '32655', '32656', '32657']

    and make %countyInfo into

        my %countyInfo = (
          '1' => ['CITRUS', ['32650', '32651', '32652', '11111']],
          '2' => ['ALACHUA', ['32653', '32654', '32655', '32656', '32657',
'11111']],
         );




------------------------------

Date: Mon, 06 Jan 2003 05:03:31 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: how to refer to the array?
Message-Id: <3E195473.B41A81C3@earthlink.net>

qjzhu wrote:
> 
> dear all,
> 
>     if I have
> 
>         my %countyInfo = (
>           '1' => ['CITRUS', ['32650', '32651', '32652']],
>           '2' => ['ALACHUA', ['32653', '32654', '32655', '32656',
>                               '32657']],
>          );
> 
>     how can I push a new value into both array ['32650', '32651',
> '32652'] and ['32653', '32654', '32655', '32656', '32657']
> 
>     and make %countyInfo into
> 
>         my %countyInfo = (
>           '1' => ['CITRUS', ['32650', '32651', '32652', '11111']],
>           '2' => ['ALACHUA', ['32653', '32654', '32655', '32656',
>                               '32657', '11111']],
>          );

There's the verbose way:
   while( my ($country_id, $country_info) = each %countryInfo ) {
      my ($country_name, $country_numbers) = @$country_info;
      push @$country_numbers, '11111';
   }
Or the consise way:
   push @{$$_[1]}, '11111' for values %countryInfo;
There're other ways, too, of course.
For example, this:
   push @{$$_[1]}, '11111' while +(undef,$_) = each %countryInfo;
is slightly less consise than the 'consise' way, but uses a little bit
less memory.

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


------------------------------

Date: 6 Jan 2003 09:18:54 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: OO primer, please!
Message-Id: <avbhlu$b2d$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Michele Dondi:

><premise>
> I have no experience in OO programming (in general and) in Perl. I
> know that there are both good reference manpages and tutorials,
> however I'd like to see practically how to work on a task I'm taking
> into account right now.
></premise>
> 
> So, the problem is this: I'd like to implement an interface to store
> tables (please don't point me to suitable modules: it's just for
> experimenting/learning) and eventually to print them out.
> 
> The interface should work like this:
> 
>   my $table1=table->new;
>   for my $x (1..5) {
>       for my $y (1..3) {
> 	  $table1->entry($x,$y)->put(fun($x,$y));
>       }
>   }
> 
> Obviously there should be a corresponding  method 'get' and a method
> '$table1->printt()' to print the table once it's filled[*], and
> possibly others to follow (but I'd like to keep it as simple as
> possibe initially).

A common skeleton is to have one constructor in which you bless
something into the class and the instance methods.

    package Table;
    
    sub new {
        my $class = shift; # this is the class name
        my $self = [ ]; 
        bless $self => $class;
    }
    
    # this is an instance method
    sub entry {
        my ($self, $x, $y) = @_;
        return $self->[$x]->[$y];
    }

Yet, your interface is a little odd. Your premise:

    $table1->entry($x, $y)->put( fun($x, $y) );

would require that entry() returned an object which has a put() method.
Perhaps an object of the class Table::Cell or so. To keep it simple,
have entry() return just the content of a cell while put() receives
three arguments: $x, $y and $value:

    sub put {
        my ($self, $x, $y, $value) = @_;
        $self->[$x]->[$y] = $value;
    }

put() is a good candidate for a chained method, something like:
    
    $table->put(0, 0, "0x0")
          ->put(0, 1, "0x1")
          ->put(1, 0, "1x0");

If you want this, let put() return the object:

    sub put {
        my ($self, $x, $y, $value) = @_;
        $self->[$x]->[$y] = $value;
        return $self;               # <-- makes it chainable
    }

> As an aside, I seem to recall that there's a way to make a custom
> defined function return an l-value: I think it would be great to be
> able to store entries with a command like
> 
>   $table1->entry($x,$y)=fun($x,$y);
> 
> and to get them just by referring to them. I doubt that this conflicts
> with the OO approach in general, but it doesn't seem to bad in cases
> like this.

Making entry() lvaluable looks natural. You have to change it only
slightly to achieve this, basically by adding the 'lvalue' attribute:

    sub entry : lvalue {
        my ($self, $x, $y) = @_;
        $self->[$x]->[$y];
    }

You have to drop the return() here though, otherwise you can't assign
to '$self->[$x]->[$y]'. Note that entry() still behaves like an ordinary
method when called in non-lvalue context:

    print $table->entry(0, 0);

> I know that it would be more fair to read the relevant docs and
> possibly ask here if I have any difficulty (I'll do so indeed in case
> I don't receive any answer); nevertheless I'd be very grateful if
> somebody could be so kind and provide a *skeleton* of code...

Even though the skeleton layed out here is already quite flexible
(despite its simplicity) you should read the supplied documents. The
most gentle introduction to Perl-OO is perlboot.pod. You'll find
references to more useful documentation under its "SEE ALSO" section.

> [*] To be definite, at this stage a portion of code like this should
> suffice:
> 
>   {
>       local ($,,$\)=("\t","\n");
>       print @{$_} for @table;
>   }
> 
> (this implies that $x and $y are swapped as indexes of 2-dim array
> @table used here - it should be the array used practically to store
> the values)

This can easily be turned into a method. Remember that $self is a
reference to the blessed array that contains the data:

    sub print {
        my $self = shift;
        local ($,, $\) = ("\t", "\n");
        print @$_ for @$self;
    }

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


------------------------------

Date: Mon, 06 Jan 2003 05:20:17 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: regex code evaluation (??{code})
Message-Id: <3E191187.F0ECA546@acm.org>

weberh wrote:
> 
> I'd like to know more about the
> "highly experimental" code evaluation
> --(??{code})-- feature in perl 5.6 regex'.
> Couldn't find many postings on this topic.
> 
> perl -ne 'while(/([A|B|C|D]{6}).+?(??{reverse \1})/){print "$&\n"}'
> foo
> 
> (find palindromedar :) ) returns
> 
> Unmatched ) before HERE mark in regex m/) << HERE cdd11101x0(RALACS/
> at -e line 1, <> line 1.
> 
> Did I overlook something ?

If you are searching for six letter palindromes then this should work:

perl -ne'for($i=0;$w=substr$_,$i++,6;){$w=~/^[ABCD]{6}$/ and $w eq reverse$w and print"$w\n"}' foo



John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Mon, 06 Jan 2003 01:01:44 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: regex code evaluation (??{code})
Message-Id: <3E191BC8.51AC216C@earthlink.net>

weberh wrote:
> 
> Hi !
> 
> I'd like to know more about the
> "highly experimental" code evaluation
> --(??{code})-- feature in perl 5.6 regex'.
> Couldn't find many postings on this topic.

That's because most (all?) web search engines ignore non-word
characters, so it's hard to do a search for "(??{"

> perl -ne 'while(/([A|B|C|D]{6}).+?(??{reverse \1})/){print "$&\n"}'
> foo

Try:

   perl -ne 'print "$&\n" while /(\w{6})(.*?(??{reverse $1}))/g'

(Note the /g on the end of the regex)


-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


------------------------------

Date: Mon, 06 Jan 2003 07:52:19 GMT
From: inwap@inwap.com (Joe Smith)
Subject: Re: regexp question
Message-Id: <TAaS9.1739$io.78856@iad-read.news.verio.net>

In article <3e17f873$0$18872$afc38c87@news.optusnet.com.au>,
C3 <someone@microsoft.com> wrote:
>match any string starting with g-images.amazon.com and having up to five
>slashes after that. I feel this will be safe enough, but how do I come up
>with the expression? Here's what I came up with
>
>regex ^http://g-images.amazon.com(/.*){0,5}

Problem: '.*' matches any number of slashes.

  perl -le '$_="com/a/b/c/d/e/f/g/h/i/"; m#com(/.*){1,2}#; print $1'
  /a/b/c/d/e/f/g/h/i/

  perl -le '$_="com/a/b/c/d/e/f/g/h/i/"; m#com(/.*?){0,5}#; print $1'
  /

The first one matches one slash followed by any number of characters,
whether they are slashes or not.  The second one finds the minimum
positive match.  '(/[^/]*)' is more appropriate than '(/.*)'.

Of course, the other solutions involving tr/// better match your task.
	-Joe
-- 
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


------------------------------

Date: Mon, 06 Jan 2003 10:01:36 +0200
From: "Teh (tî'pô)" <teh@mindless.com>
Subject: Re: Should I worry about the optimizer.
Message-Id: <2kbi1vcimdbsjqk6rl0f02rlluhavh0j3u@4ax.com>

Benjamin Goldberg bravely attempted to attach 27 electrodes of
knowledge
to the nipples of comp.lang.perl.misc by saying:
>Teh (tî'pô) wrote:
>> 
>> Hi, I'm benchmarking a few things and I'm not sure how much I should
>> be worried about the optimizer optimizing away stuff.
>[snip]
>> Should I worry about perl optimizing away my loops? It doesn't in this
>> case but I don't know if I should take precautions against the
>> possibility.
>
>A better question is, what are you trying to optomize, and why?
>
>In general, Benchmark isn't really the best tool for speeding up your
>perl program -- the profiler is.  You want to spend your effort finding
>out which parts of the program need to be sped up, before going and
>spending your effort on doing the actual speeding up.

I have done some profiling using Time::HiRes instead of Devel::DProf
as I'm having problems with DProf (see my message
7bst0vk2smm9gknr1ck1p4jf36qp7vdaj2@4ax.com ).

So I'm not just benchmarking random bits of code, I've found a place
in which a lot of the time is spent and I'm benchmarking different
ways of doing the same thing. When benchmarking I want to minimize the
noise and just measure the stuff I'm interested in.


------------------------------

Date: Mon, 06 Jan 2003 10:02:49 +0200
From: "Teh (tî'pô)" <teh@mindless.com>
Subject: Re: Should I worry about the optimizer.
Message-Id: <20ei1v8or2639g11l4kdb900v0mu5681ns@4ax.com>

Mark Jason Dominus bravely attempted to attach 60 electrodes of
knowledge
to the nipples of comp.lang.perl.misc by saying:
>In article <igkg1vk4jkbifsolck6decs4ju0u7008g4@4ax.com>,
>Teh (tî'pô) <teh@mindless.com> wrote:
>>I was hoping someone who knows perl internally could give me a factual
>>answer.
>
>There isn't going to be a single factual answer.  The whole point of
>an optimizer is that it makes changes to the code that are
>semantically invisible; because the changes are semantically
>invisible, they can change from version to version of Perl without any
>warning.  The answer to any specific question about optimization will
>depend very much on the particular version of Perl you are using.
>
>So the best answer to such questions is that you should use -Dx and
>see for yourself what is happening.
>
>For example:
>
>        perl5.7.3 -Dx -e 'sub f { foreach (@_) { } }'
>
<snip>
>The 'null' items are operations that were optimized away.  But the
>'iter' (which is iterating on the array) and the 'and' (which is
>testing for the end) are both still there.
>
>I hopt this is something like what you wanted.

I think it is, thanks!


------------------------------

Date: Mon, 06 Jan 2003 03:37:46 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Should I worry about the optimizer.
Message-Id: <3E19405A.6152DDA5@earthlink.net>

Teh (tî'pô) wrote:
> Benjamin Goldberg wrote:
[snip]
> >In general, Benchmark isn't really the best tool for speeding up your
> >perl program -- the profiler is.  You want to spend your effort
> >finding out which parts of the program need to be sped up, before
> >going and spending your effort on doing the actual speeding up.
> 
> I have done some profiling using Time::HiRes instead of Devel::DProf
> as I'm having problems with DProf (see my message
> 7bst0vk2smm9gknr1ck1p4jf36qp7vdaj2@4ax.com ).

I've just now replied to that message, and hopefully offered a solution.

Oh, and one thing I didn't say there -- If I recally correctly, the
newest development perl (version 5.9) has stuff in it which prevents
newly allocated CVs from getting the same addresses as old, freed, ones;
this should entirely fix the problem of subs seeming to disappear from
DProf's output.

> So I'm not just benchmarking random bits of code, I've found a place
> in which a lot of the time is spent and I'm benchmarking different
> ways of doing the same thing. When benchmarking I want to minimize the
> noise and just measure the stuff I'm interested in.

Ok.  Well, in comparing foeach versus while/shift, I came up with the
following:

   [Windows 95] C:\WINDOWS>perl -MBenchmark=cmpthese -wMstrict
   my @a = 1 .. 10_000;
   sub sh { while(@_) { my $x = shift; } }
   sub fe { for my $x (@_) {} }
   cmpthese( -5, { 'sh' => sub { sh(@a) }, 'fe' => sub { fe(@a) } });
   __END__
   Benchmark: running fe, sh, each for at least 5 CPU seconds...
   fe:  6 wallclock secs (5.00 CPU) @ 49.60/s (n=248)
   sh:  5 wallclock secs (5.44 CPU) @ 20.77/s (n=113)
        Rate   sh   fe
   sh 20.8/s   -- -58%
   fe 49.6/s 139%   --

So, clearly, foreach is faster.

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


------------------------------

Date: Mon, 06 Jan 2003 10:50:04 +0200
From: "Teh (tî'pô)" <teh@mindless.com>
Subject: Re: Should I worry about the optimizer.
Message-Id: <n1gi1vssjmhve8gatmm9anl179sfkricrj@4ax.com>

Benjamin Goldberg bravely attempted to attach 48 electrodes of
knowledge
to the nipples of comp.lang.perl.misc by saying:
>Teh (tî'pô) wrote:
>> Benjamin Goldberg wrote:
>[snip]
>> >In general, Benchmark isn't really the best tool for speeding up your
>> >perl program -- the profiler is.  You want to spend your effort
>> >finding out which parts of the program need to be sped up, before
>> >going and spending your effort on doing the actual speeding up.
>> 
>> I have done some profiling using Time::HiRes instead of Devel::DProf
>> as I'm having problems with DProf (see my message
>> 7bst0vk2smm9gknr1ck1p4jf36qp7vdaj2@4ax.com ).
>
>I've just now replied to that message, and hopefully offered a solution.
>
>Oh, and one thing I didn't say there -- If I recally correctly, the
>newest development perl (version 5.9) has stuff in it which prevents
>newly allocated CVs from getting the same addresses as old, freed, ones;
>this should entirely fix the problem of subs seeming to disappear from
>DProf's output.
>
>> So I'm not just benchmarking random bits of code, I've found a place
>> in which a lot of the time is spent and I'm benchmarking different
>> ways of doing the same thing. When benchmarking I want to minimize the
>> noise and just measure the stuff I'm interested in.
>
>Ok.  Well, in comparing foeach versus while/shift, I came up with the
>following:
<snip>
>So, clearly, foreach is faster.

Yeah, that's intuitive too. 
That actually wasn't what I was measuring it was just a simplified
example that had code I thought might be optimized out.

What I originally tested for was this, I have a class hierarchy that
processes some data, in one of the derived classes I have special
treatment for data that matches a specific pattern. I wanted  to see
if I should send the data to the base class method and then grep the
pattern or plant a hook in the base method and override it in the
derived class. Here's my test.

Note: I don't have any questions about this, there is no need to read
on for any reason other than curiosity.

#!/bin/env perl -w
use strict;
use Benchmark;

package A;
sub new { bless {}, shift }

# Just iterate over data (processing removed)
sub plain {
	my $self = shift;
	foreach my $i (@_) { }
}
# base class virtual method, empty in base class.
sub vir { return }

sub withvir {
	my $self = shift;
	foreach my $i (@_) {
		$self->vir($i);
	}
}

package B;
@B::ISA = qw(A);
# first do plain and then grep and treat special cases
sub plain_get {
	my $self = shift;
	$self->plain(@_);
	my @arr = grep(/pre_.*_post/, @_);
	foreach my $i ( @arr) { }
}

# Derived virtual function, check if data matches special
# case (in real life do something if true).
sub vir { 
	my ($self, $i) = @_;
	$i =~ /pre_.*_post/;
}

package main;
my @data = ();
foreach my $i (1..1000) {
	# in real life about one in 350 cases is special
	push(@data,($i % 350)? $i: "pre_${i}_post");
}

my $aa = new A;
my $bb = new B;
my $count = @ARGV? shift : 1000;
timethese( $count, {
		A_plain  => sub { $aa->plain(@data) },
		A_withvir => sub { $aa->withvir(@data) },
		B_plain_get => sub { $bb->plain_get(@data) },
		B_withvir => sub { $bb->withvir(@data) },
		}
		);
__END__

The results I get (on several platforms) is that withvir() is about 5
times slower than plain() making the hook method less efficient.




------------------------------

Date: Mon, 06 Jan 2003 05:22:35 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: very easy string Q.
Message-Id: <vo8S9.232783$Qr.6597703@news3.calgary.shaw.ca>

^darkage wrote:

> easy string problem.    when I execute this the wget cmd runs but the ftp
> directory is seperated from the ".zip" extension.
> 
> ie.   it exec's    wget ftp://ftp.nai.com/pub/datfiles/english/dat-4239
> instead of wget ftp://ftp.nai.com/pub/datfiles/english/dat-4239.zip    Im
> no sure of the right way to append the .zip onto the string.

Unless this is a learning excersise for school or something, I'd suggest 
going to CPAN and grabbing everything you need, and coming up with 
something like:

#! /usr/bin/perl -w

use strict;
use Config::IniFiles;

# should use GetOpts or something to allow configurability!
my $filename = shift || './update.ini';
my $ini = Config::IniFiles->new(-file => $filename)
   or die "Can't read $filename";

my $version = $ini->val('Incremental', 'DATVersion')
   or die "Can't get Incremental->DATVersion from $filename";

exec("/usr/local/bin/wget",
     "ftp://ftp.nai.com/pub/datfiles/english/dat-$version.zip");


In fact, I'd even suggest doing away with wget, and doing that one line in 
about 3 or 4 lines of perl (using Net::FTP or LWP::UserAgent or something 
similar).  The reason?  Portability!  You would be able to move your script 
from this machine (which has wget) to any other machine (that may not have 
wget), including, perhaps, even a Windows machine (cough, cough).  As long 
as they have the required modules already installed, it will all just work 
great on the new machines.

[NOTE: I've never used Config::IniFiles before, but am basing the above 
UNTESTED code on the perldoc on cpan.]



------------------------------

Date: Mon, 06 Jan 2003 05:20:01 GMT
From: lewart@uiuc.edu (Daniel S. Lewart)
Subject: Re: very easy string Q.
Message-Id: <5m8S9.11512$Vf3.119955@vixen.cso.uiuc.edu>

darkage <darkage@freeshellzzzz.org> writes:

> Thank you both methods work good, except for one thing.  the url turns out to
> be, ftp://ftp.nai.com/pub/datfiles/english/dat-$datversion\dat-4240?.zip  I
> need to take out the ?.

It is probably a carriage return.

> It might help to know the contents of the update.ini below, here it has the
> version of DAT file.   the url for update.ini never changes.  probably
> should grep the .zip as well from here.
> DATVersion=4240
> FileName=dat-4240.zip

Below is the revised script.

Cheers,
Daniel Lewart
lewart@uiuc.edu
-------------------------------------------------------------------------------
#!/usr/bin/perl -w

use strict;

my $update_ini = "update.ini";

my $FileName;
open(FILE, $update_ini) || die "can't open file $update_ini $!";
while (<FILE>) {
    last if ($FileName) = /^FileName=(dat-\d+\.zip)/;
}
close FILE;

exec "/usr/local/bin/wget", "ftp://ftp.nai.com/pub/datfiles/english/$FileName";
-------------------------------------------------------------------------------


------------------------------

Date: Mon, 06 Jan 2003 08:36:30 GMT
From: inwap@inwap.com (Joe Smith)
Subject: Re: What means sh: ^P: not found
Message-Id: <iebS9.1741$io.78856@iad-read.news.verio.net>

In article <3E00C661.8010705@consultant.com>,
Alexander Stremitzer  <stremitz@consultant.com> wrote:
>I am trying to copy a file from one location to another utilizing the 
>non aliased version of Solaris cp.

The system() function does not honor aliases set up by your shell's rc
file.  Forget about .cshrc, .profile, .bashrc or whatever.  Just use
plain "cp" (for speed) or File::Copy (for portability to other OS's).
	-Joe
-- 
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


------------------------------

Date: Mon, 06 Jan 2003 09:03:41 GMT
From: inwap@inwap.com (Joe Smith)
Subject: Re: Why doesn't this foreach SMTP loop work?
Message-Id: <NDbS9.1742$io.78856@iad-read.news.verio.net>

In article <atr0ds$17gq$1@hedeland.org>, Per Hedeland <per@hedeland.org> wrote:
>In article <atq9pt$jbc$1@knossos.btinternet.com> "Nick Djurovich"
><Nick_Djurovich@NOSPAM.aimtechnology.com> writes:
>> Alternatively, you might try creating
>>a single To: with a list of addresses delimited with a semi-colon
>>and change the code thus ;
>
>Multiple addresses in an email header are *comma*-separated (except when
>displayed by some weird MUA).

Multiple addresses in an email header are *space*-seperated (if you are
using an MUA (such as /usr/ucb/mail) that expects addresses that way).

The RFCs clearly define how MTAs handle addresses, but MUAs are
implementation dependent.
	-Joe

-- 
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


------------------------------

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.  

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 4349
***************************************


home help back first fref pref prev next nref lref last post