[26053] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8261 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 19 18:05:34 2005

Date: Tue, 19 Jul 2005 15:05:08 -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           Tue, 19 Jul 2005     Volume: 10 Number: 8261

Today's topics:
    Re: "Can't return a temporary from lvalue subroutine... <nobull@mail.com>
    Re: "Can't return a temporary from lvalue subroutine... <nobull@mail.com>
    Re: "Can't return a temporary from lvalue subroutine... (Anno Siegel)
    Re: "Can't return a temporary from lvalue subroutine... (Anno Siegel)
    Re: "Can't return a temporary from lvalue subroutine... <jkrugman345@yahbitoo.com>
    Re: "Can't return a temporary from lvalue subroutine... <jkrugman345@yahbitoo.com>
    Re: copy contructor (Anno Siegel)
    Re: copy contructor <1usa@llenroc.ude.invalid>
    Re: copy contructor (Anno Siegel)
    Re: How to call an internal CGI subroutine? <sherm@dot-app.org>
    Re: How to call an internal CGI subroutine? <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: How to call an internal CGI subroutine? <jwxxxxx@yahoo.com>
    Re: How to call an internal CGI subroutine? <noreply@gunnar.cc>
    Re: ithreads & memory (bler)
    Re: ithreads & memory <1usa@llenroc.ude.invalid>
        Numeric or character ? <john.cecere@sun.com>
    Re: Numeric or character ? <1usa@llenroc.ude.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 19 Jul 2005 19:09:36 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <dbjfl9$d42$1@slavica.ukpost.com>



Anno Siegel wrote:

> Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
> 
>>
>>use Tie::OneOff;
>>
>>sub var : lvalue {
>>     my $self = shift;
>>     Tie::OneOff->lvalue({
>>        FETCH => sub { $self->{_var_} },
>>        STORE => sub {
>>          my $newval = shift;
>>          # validate $newval
>>          $self->{_var_} = $newval;
>>        },
>>     });
>>}
> 
> 
> Ah, you're handing in the FETCH and STORE methods as run time parameters
> to tie().  They could even be closured.  That's crazy, and I like it.

Thankyou.  It's nice to be appreciated.  I have no idea if anyone else 
(except one person who left a CPAN review) has ever used my Tie::OneOff.


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

Date: Tue, 19 Jul 2005 19:17:05 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <dbjg31$d4q$1@slavica.ukpost.com>



Anno Siegel wrote:

> I suppose your motivation is that you like the idea of lvalue methods,
> but need more control over what gets stored by careless users in your
> sensitive objects.  A similar problem exists with objects that expose
> (parts of) their interior though overloading a dereference operator,
> say %{}.  Like lvalues, it gives you pretty syntax, but leaves your
> objects wide open.  I have used tied hashes to correct this (that is,
> returned a reference to a tied hash in response to %{ $obj}, and haven't
> encountered the particular difficulty you're seeing.  That may be
> an alternative approach.

Here be dragons.

There are bugs in Perl.  In particular if the reference returned by the 
%{} overload is the only reference to the tied hash some very nasty 
things happen very soon.  Even if it's not, nasty things seem to happen 
after a while.

I wrote whole load of in-house classes that used this API technique and 
then had to labourously go through all our programs to use a direct 
method-call API rather than the overload-tie API.


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

Date: 19 Jul 2005 19:22:42 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <dbjju2$g36$1@mamenchi.zrz.TU-Berlin.DE>

Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
> 
> 
> Anno Siegel wrote:
> 
> > Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
> > 
> >>
> >>use Tie::OneOff;
> >>
> >>sub var : lvalue {
> >>     my $self = shift;
> >>     Tie::OneOff->lvalue({
> >>        FETCH => sub { $self->{_var_} },
> >>        STORE => sub {
> >>          my $newval = shift;
> >>          # validate $newval
> >>          $self->{_var_} = $newval;
> >>        },
> >>     });
> >>}
> > 
> > 
> > Ah, you're handing in the FETCH and STORE methods as run time parameters
> > to tie().  They could even be closured.  That's crazy, and I like it.
> 
> Thankyou.  It's nice to be appreciated.  I have no idea if anyone else 
> (except one person who left a CPAN review) has ever used my Tie::OneOff.

Since only a small percentage of users write reviews, it is likely that
there were many more than that one.

The better the module, the less likely an author is to hear about it.

Otherwise, yes, it is hard to make a module findable for potential
users.  I'm not sure how CPAN search builds its indexes (or whatever
it does), nor even if the two(?) major search sites give the same results
in all cases.  So it's hard to plan for that.

Also, the (set of) situation(s) that Tie::OneOff would be of advantage in
is hard to describe in the first place.  What are good keywords for that,
even if one knew where to place them?

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: 19 Jul 2005 19:52:53 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <dbjlml$h1s$1@mamenchi.zrz.TU-Berlin.DE>

Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
> 
> 
> Anno Siegel wrote:
> 
> > I suppose your motivation is that you like the idea of lvalue methods,
> > but need more control over what gets stored by careless users in your
> > sensitive objects.  A similar problem exists with objects that expose
> > (parts of) their interior though overloading a dereference operator,
> > say %{}.  Like lvalues, it gives you pretty syntax, but leaves your
> > objects wide open.  I have used tied hashes to correct this (that is,
> > returned a reference to a tied hash in response to %{ $obj}, and haven't
> > encountered the particular difficulty you're seeing.  That may be
> > an alternative approach.
> 
> Here be dragons.
> 
> There are bugs in Perl.  In particular if the reference returned by the 
> %{} overload is the only reference to the tied hash some very nasty 
> things happen very soon.  Even if it's not, nasty things seem to happen 
> after a while.

Even without that, de-reference overloading is a major pain for its
possible heirs.  Overloading propagates up the ISA-chain just like
methods do, so if you are, say, a hash class, and you decide to inherit
from a class that overloads %{}, you'll start seeing "Not a XYZ
reference at ..." errors, or even deep recursions and segfaults.
You must "counter-overload" '%{}' => sub { shift } to be able to
access your own objects.

So you can't *be* a hash, and inherit from a class that overloads
hash dereferencing, and similar for arrays, scalars and whathaveyou.
Not all that surprising, really, but I only noticed this recently.

It's like real life and soap operas.  People inherit things, along
with the good stuff, whose consequences are less than favorable.

> I wrote whole load of in-house classes that used this API technique and 
> then had to labourously go through all our programs to use a direct 
> method-call API rather than the overload-tie API.

Oh yeah!

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: Tue, 19 Jul 2005 21:02:44 +0000 (UTC)
From: J Krugman <jkrugman345@yahbitoo.com>
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <dbjppk$398$1@reader2.panix.com>

In <dbjfl9$d42$1@slavica.ukpost.com> Brian McCauley <nobull@mail.com> writes:

>Anno Siegel wrote:

>> Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
>> 
>>>
>>>use Tie::OneOff;
>>>
>>>sub var : lvalue {
>>>     my $self = shift;
>>>     Tie::OneOff->lvalue({
>>>        FETCH => sub { $self->{_var_} },
>>>        STORE => sub {
>>>          my $newval = shift;
>>>          # validate $newval
>>>          $self->{_var_} = $newval;
>>>        },
>>>     });
>>>}
>> 
>> 
>> Ah, you're handing in the FETCH and STORE methods as run time parameters
>> to tie().  They could even be closured.  That's crazy, and I like it.

>Thankyou.  It's nice to be appreciated.  I have no idea if anyone else 
>(except one person who left a CPAN review) has ever used my Tie::OneOff.

I've seen references to it, though at first I thought it was only
meant as a joke around the phrase "tie one on" (i.e. "get drunk");
only when I saw you mention of it did I realize that the original
reference was serious.  The name may be too clever for its own
good.  :-)

jill

-- 
To  s&e^n]d  me  m~a}i]l  r%e*m?o\v[e  bit from my a|d)d:r{e:s]s.



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

Date: Tue, 19 Jul 2005 21:12:26 +0000 (UTC)
From: J Krugman <jkrugman345@yahbitoo.com>
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <dbjqbq$398$2@reader2.panix.com>



FWIW, I found a bug in bugs.perl.org that seems related to the
error message in the subject line:

http://rt.perl.org/rt3/Ticket/Display.html?id=30582

In particular, one of the follow ups shows a one-liner that elicits
the bug:

$ perl -MTie::Hash -we'tie %x,"Tie::StdHash"; sub foo:lvalue {$x{foo}} foo=1'
Can't return a temporary from lvalue subroutine at -e line 1.

As far as I can tell, there doesn't seem to be much momentum to
get this bug fixed (but I know little of the process, so I could
be way off-base on this).


jill

-- 
To  s&e^n]d  me  m~a}i]l  r%e*m?o\v[e  bit from my a|d)d:r{e:s]s.



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

Date: 19 Jul 2005 17:42:49 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: copy contructor
Message-Id: <dbje2p$cp9$1@mamenchi.zrz.TU-Berlin.DE>

Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote in comp.lang.perl.misc:
> Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> > Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMCCCXXXIX
> > September MCMXCIII in <URL:news:dbhc62$53b$1@mamenchi.zrz.TU-Berlin.DE>:
> > :)  Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> > :) > Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMCCCXXXIX
> > :) > September MCMXCIII in <URL:news:dbfv8r$88k$1@mamenchi.zrz.TU-Berlin.DE>:

[big snip]

> That's why carelessly written classes that access their objects all over
> the place are useless for inheritance.  A good class defines a handful
> of accessors and then never touches the object again.  Then most of the
> useful stuff *can* be inherited.
> 
> If the inheriting class has an object of the base class a component,
                                               ^^^^^^^^^^^^^^^^^^^^^^

That should have been "...base class *as* a component", sorry.

> (so the is-a relation is based on a has-a relation), even accessors
> can be made inheritable with a little trick the base class must provide.
> I'll post a rewrite of my example a little later.

Okay, here goes.

The code for the base class is mostly unchanged.  The only difference is
that it inserts a do-nothing method (one that just returns its object) into
all of its accessors (not in other methods).  So if the accessor was

    sub alpha { $_[ 0]->[ 0] }

it becomes

    sub alpha { $_[ 0]->parent->[ 0] }
    # ...
    sub parent { shift }

Obviously, this doesn't change the behavior of the class, except for
wasting some time (but we're doing OO here, Efficiency is down the hall).
So clients who don't know about the change continue to function.

The no-op is called like the class (de-emphasized by lower-casing).  It
could, of course, be called anything, but that is a good choice from the
clients point of view if the client chooses to use the facility.

It does that by defining a field that holds an object of class Parent
and calling the accessor to that field "parent", so it overrides that
method in the base class.

    sub new {
        # ...
        bless {
            parent => Parent->new( ...),
            # ...
        }, $class;
    }

    sub parent { $_[ 0]->{ parent} }

The effect is that the base class, when it comes to accessing an object,
interpolates the 'parent' method and sees an object of its own class which
it can handle.

This alone allows the client to inherit from Parent, that is, it can
put "Parent" on @ISA and all Parent methods will work using the parent
component.  Thus it must override only one method.  It can go on to
override specific other methods to modify the behavior.

So here is my original example re-written this way.  The class Client_orig
is how it must be done (well, one way) without the ->parent method.  It
still works with the modified Parent, but the modified Client is more
orderly.

Anno

    #!/usr/local/bin/perl
    use strict; use warnings; $| = 1;
    use Vi::QuickFix;

    my $c = Client->new( 5, 6, 7);
    print "$_ -> ", $c->$_, "\n" for qw( alpha beta gamma delta total);

    ##########################################################################

    package Parent;

    sub new {
        my $class = shift;
        my ( $alpha, $beta) = @_;
        bless [ $alpha, $beta], $class;
    }

    sub parent { shift }

    sub alpha { $_[ 0]->parent->[ 0] }
    sub beta  { $_[ 0]->parent->[ 1] }

    sub total { $_[ 0]->alpha + $_[ 0]->beta }

    ##########################################################################

    package Client;
    BEGIN { our @ISA = 'Parent' }

    sub new {
        my $class = shift;
        my ( $alpha, $gamma, $delta) @_;
        bless {
            parent => Parent->new( $alpha, undef),
            gamma  => $gamma,
            delta  => $delta,
        }, $class,
    }

    sub parent { $_[ 0]->{ parent} }
    sub gamma { $_[ 0]->{ gamma} }
    sub delta { $_[ 0]->{ delta} }

    sub beta { $_[ 0]->gamma + $_[ 0]->delta }

    ##########################################################################

    package Client_orig;
    BEGIN { our @ISA = 'Parent' }

    sub new {
        my $class = shift;
        my ( $alpha, $gamma, $delta) = @_;
        bless {
            alpha => $alpha,
            gamma => $gamma,
            delta => $delta,
        }, $class;
    }

    sub alpha { $_[ 0]->{ alpha} }
    sub gamma { $_[ 0]->{ gamma} }
    sub delta { $_[ 0]->{ delta} }

    sub beta { $_[ 0]->gamma + $_[ 0]->delta }

    __END__
-- 
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: Tue, 19 Jul 2005 17:50:34 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: copy contructor
Message-Id: <Xns96988CCF6F48Basu1cornelledu@127.0.0.1>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in
news:dbje2p$cp9$1@mamenchi.zrz.TU-Berlin.DE: 

> The code for the base class is mostly unchanged.  The only difference
> is that it inserts a do-nothing method (one that just returns its
> object) into all of its accessors (not in other methods).  So if the
> accessor was 
> 
>     sub alpha { $_[ 0]->[ 0] }
> 
> it becomes
> 
>     sub alpha { $_[ 0]->parent->[ 0] }
>     # ...
>     sub parent { shift }

[ rest of the discussion and code snipped ]

Anno:

I wanted to thank you very much for this series of posts, especially 
this last one. I found it very informative. 

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


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

Date: 19 Jul 2005 21:09:56 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: copy contructor
Message-Id: <dbjq74$jgn$1@mamenchi.zrz.TU-Berlin.DE>

A. Sinan Unur <1usa@llenroc.ude.invalid> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in
> news:dbje2p$cp9$1@mamenchi.zrz.TU-Berlin.DE: 
> 
> > The code for the base class is mostly unchanged.  The only difference
> > is that it inserts a do-nothing method (one that just returns its
> > object) into all of its accessors (not in other methods).  So if the
> > accessor was 
> > 
> >     sub alpha { $_[ 0]->[ 0] }
> > 
> > it becomes
> > 
> >     sub alpha { $_[ 0]->parent->[ 0] }
> >     # ...
> >     sub parent { shift }
> 
> [ rest of the discussion and code snipped ]
> 
> Anno:
> 
> I wanted to thank you very much for this series of posts, especially 
> this last one. I found it very informative. 

Thankyou.  It's nice to be appreciated. [As Brian (Nobull) just said in
another thread.]

In fact, I think I'll make a (not much) more extensive writeup of this
material and put it on the web (at least).  The final trick deserves
publicity, if I say so myself, but even without it, more awareness of
accessors and their significance would help OO programming in Perl.

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: Tue, 19 Jul 2005 13:18:11 -0400
From: Sherm Pendley <sherm@dot-app.org>
Subject: Re: How to call an internal CGI subroutine?
Message-Id: <m2r7duyass.fsf@Sherm-Pendleys-Computer.local>

James White <jwxxxxx@yahoo.com> writes:

> info (and somewhat of an example) that if you have just a vanilla <form>
> tag with no action, it will call itself.

You're muddling together HTML features, and those of the tool you're using to
produce the HTML. So let's clarify that.

In HTML, a form element with no action attribute normally won't do anything.
If it's in a JavaScript-enabled environment, an onSubmit handler could be
used to handle it within the client.

CGI.pm has functions to produce HTML output. These functions, by default, are
"sticky". So if you use these functions to produce a form element without
specifying the action URL, the default action printed will point back to the
same script.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


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

Date: Tue, 19 Jul 2005 11:41:00 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: How to call an internal CGI subroutine?
Message-Id: <t2uvq2xk0e.ln2@goaway.wombat.san-francisco.ca.us>

On 2005-07-19, Sherm Pendley <sherm@dot-app.org> wrote:
>
> CGI.pm has functions to produce HTML output. These functions, by default, are
> "sticky". So if you use these functions to produce a form element without
> specifying the action URL, the default action printed will point back to the
> same script.

I don't think the behaviour of start_form() is controlled by CGI's sticky
feature; the default action being the original script is merely a feature
of start_form().  Stickiness, AFAICT from perldoc CGI, only has to do
with the form fields, not the <form> itself.  (IOW, using -nosticky should
have no effect on start_form()'s defaults.)

--keith

-- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
see X- headers for PGP signature information



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

Date: Tue, 19 Jul 2005 15:09:25 -0400
From: James White <jwxxxxx@yahoo.com>
Subject: Re: How to call an internal CGI subroutine?
Message-Id: <pan.2005.07.19.19.09.24.933855@yahoo.com>


> You're muddling together HTML features, and those of the tool you're
> using to produce the HTML. So let's clarify that.

Yep.  At this point I am muddling pretty bad.  Main problem is that I am
used to a standard monolithic language (Delphi, Kylix, Builder) where
everything you need is in one tidy capsule - not multiple
protocols/languages scattered between a server and client.  But I will get
there with lots of reading.

> CGI.pm has functions to produce HTML output. These functions, by
> default, are

I know about CGI.pm and it looks good.  But I want to do the simple
learning stuff 'raw' for the moment so I will have a better idea of what
is happening and is not hidden in an object.

JW


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

Date: Tue, 19 Jul 2005 22:38:16 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How to call an internal CGI subroutine?
Message-Id: <3k56ltFsp3mbU1@individual.net>

James White wrote:
> Sherm Pendley wrote:
>> James White writes:
>>> if you have just a vanilla <form> tag with no action, it will call itself.
>> 
>> In HTML, a form element with no action attribute normally won't do anything.
>> If it's in a JavaScript-enabled environment, an onSubmit handler could be
>> used to handle it within the client.
>> 
>> CGI.pm has functions to produce HTML output.
> 
> I know about CGI.pm and it looks good.  But I want to do the simple
> learning stuff 'raw' for the moment so I will have a better idea of what
> is happening and is not hidden in an object.

Learning the 'raw' stuff sounds good IMO. *However*, to do that you'll 
need to study the applicable specifications, not just guess. According 
to the HTML spec., the browser behavior for the case the action 
attribute is not properly set is undefined:
http://www.w3.org/TR/html4/interact/forms.html#adef-action

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Tue, 19 Jul 2005 19:16:59 +0000 (UTC)
From: "Micha³ Lesiak (bler)" <michalusenet@blaair.eu.org>
Subject: Re: ithreads & memory
Message-Id: <Xns9698D8820EDA9michalusenetblaaireu@193.110.122.97>

xhoster@gmail.com wrote in news:20050719002327.291$6y@newsreader.com:

>> It's all the same when I use "sleep 1" and "sleep 4", respectively.
> 
> I'm not sure that that precludes this possibility.

Okay, consider this:

use threads;                                                                                                                        
use Time::HiRes "usleep";                                                                                                           
                                                                                                                                    
sub th {                                                                                                                            
    usleep(10000);                                                                                                                  
    }                                                                                                                               
                                                                                                                                    
for(my $inc; $inc < 100; $inc++) {                                                                                                                   
    $th_n = threads->new(\&th);                                                                                                     
    $th_n->detach;                                                                                                                  
    usleep(100000);                                                                                                                 
    }         

while (1) {
    	sleep 1;
    	}

So now it creates 100 threads, the while loop is there to pretend that the 
script is doing some other things. After the first while execution, the 
last thread should be finished. On my system, as before, memory is still 
taken (about 43MB).
 
> Are the threads hanging around holding memory, or is the memory building
> up in the parent process?  (e.g. does:
> ps -efl | fgrep <progname>
> show an ever increasing number of processes, or just one or two processes
> with increasing memory use?

There are only two processes present.

-- 
M.


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

Date: Tue, 19 Jul 2005 19:37:01 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: ithreads & memory
Message-Id: <Xns96989EDA3A9F9asu1cornelledu@127.0.0.1>

"Micha³ Lesiak (bler)" <michalusenet@blaair.eu.org> wrote in
news:Xns9698D8820EDA9michalusenetblaaireu@193.110.122.97: 

> xhoster@gmail.com wrote in news:20050719002327.291$6y@newsreader.com:
> 
>>> It's all the same when I use "sleep 1" and "sleep 4", respectively.
>> 
>> I'm not sure that that precludes this possibility.
> 
> Okay, consider this:
> 
> use threads;                                                          
>                                                              use
> Time::HiRes "usleep";                                                 
>                                                          

Oh, what's going on? I see, you have a ton of embedded hard tabs or 
something. Please don't do that. It makes it hard on your readers.

Why are don't you have

use strict;
use warnings;

in your script?

> for(my $inc; $inc < 100; $inc++) {                                    
>     $th_n = threads->new(\&th);                                       
>     $th_n->detach;                                                    
>     usleep(100000);                                                   
> }

Is there a reason you are using the same $th_n for all the threads over 
and over again?

Consider the code below:

#!/usr/bin/perl

use strict;
use warnings;

use threads;
use Time::HiRes "usleep";

sub th { usleep 10000;  print "$_[0]: Done\n" }

my $th_n;
for (1 .. 100) {
   $th_n = threads->new(\&th, $_);
   $th_n->detach;
   usleep 100000;
}

sleep 1 while 1;

__END__

When I ran this code on my system (Windows XP SP2, Perl 5.8.7), the 
memory footprint of the Perl process grew to 46,696K and stayed there. 
This looks like a standard leaker to me (although I could not describe 
to you the exact steps in which resources are being leaked), but it is, 
in principle, similar to the following C code:

#include <stdlib.h>

void leak(void) {
   char *p;
   while( 1 ) {
      p = malloc(1);
   }
}

Now, with one minor change, as in the following:

#!/usr/bin/perl

use strict;
use warnings;

use threads;
use Time::HiRes "usleep";

sub th { usleep 10000;  print "$_[0]: Done\n" }

for (1 .. 100) {
   my $th_n = threads->new(\&th, $_);
   $th_n->detach;
   usleep 100000;
}

sleep 1 while 1;

__END__

The maximum memory footprint stays at 3564K throughout the life of the 
script.

The maxim: Always declare variables in the smallest applicable scope.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


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

Date: Tue, 19 Jul 2005 17:26:56 -0400
From: John Cecere <john.cecere@sun.com>
Subject: Numeric or character ?
Message-Id: <dbjr71$4bn$1@news1brm.Central.Sun.COM>

Since

if ("aaa" == 0) {
	print "true\n";
}

will print true, how does one go about testing a value as an integer. For example, the user enters data:

$a=<STDIN>;

The user might accidentally type in character data, but I need to perform a test to see if what they entered is a number between 0 
and 255, and return a bad status for anything else. Doing this:

if ($a < 0 || $a > 255)

won't catch character data.

What's the _simple_ way of doing this ?

Thanks,
John Cecere


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

Date: Tue, 19 Jul 2005 21:47:22 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Numeric or character ?
Message-Id: <Xns9698B4F254690asu1cornelledu@127.0.0.1>

John Cecere <john.cecere@sun.com> wrote in
news:dbjr71$4bn$1@news1brm.Central.Sun.COM: 

> Since
> 
> if ("aaa" == 0) {
>      print "true\n";
> }
> 
> will print true, how does one go about testing a value as an integer.
> For example, the user enters data: 
> 
> $a=<STDIN>;
> 
> The user might accidentally type in character data, 

You seem to be unaware that everything that is stored in $a 
at this point is *character* data. If the user enters 110, 
$a will contain the string consisting of the characters '1', 
'1', and '0', not the integer 110.

> need to perform a test to see if what they entered is a number 
> between 0 and 255, and return a bad status for anything else. 
> Doing this: 
> 
> if ($a < 0 || $a > 255)

Of course not, you have a string in $a and you are comparing it 
to a number, and therefore it is subject to all the conditions of 
number to string conversion in Perl.

BTW, the variables $a and $b are special in Perl 
(see perldoc -f sort). You should not use them willy-nilly.

> What's the _simple_ way of doing this ?

In general, one checks if input conforms to a certain format by 
using  an appropriate regex match. In this case, it is a very 
simple one:

#!/usr/bin/perl

use strict;
use warnings;

my $input;

$| = 1;

do {
   print "Please enter and integer between 0 and 255: ";
   $input = <STDIN>;
} until $input =~ /^(\d\d?\d?)$/ and 0 + $1 < 256;

print "You entered: $input\n";

__END__

For more general numeric formats, see

<URL:http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common/number.pm>

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


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

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


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