[31066] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2311 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 1 00:09:45 2009

Date: Tue, 31 Mar 2009 21:09:09 -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, 31 Mar 2009     Volume: 11 Number: 2311

Today's topics:
    Re: Being more restrictive when using blessed hash ref  sln@netherlands.com
        finding maximum <nospam@nospam.com>
    Re: finding maximum <jacob@nospam.org>
    Re: finding maximum <thefatphil_demunged@yahoo.co.uk>
    Re: finding maximum <defaultuserbr@yahoo.com>
    Re: finding maximum <1usa@llenroc.ude.invalid>
    Re: finding maximum sln@netherlands.com
    Re: finding maximum <spibou@gmail.com>
    Re: finding maximum <esosman@ieee-dot-org.invalid>
    Re: finding maximum <tadmc@seesig.invalid>
    Re: finding maximum sln@netherlands.com
    Re: finding maximum <tadmc@seesig.invalid>
    Re: finding maximum <rjh@see.sig.invalid>
    Re: finding maximum <andrey.vul@gmail.com>
    Re: Interpreting terminal codes in Perl <m@rtij.nl.invlalid>
    Re: Pass Match Values to Variable <jjcassidy@gmail.com>
    Re: Pass Match Values to Variable <tadmc@seesig.invalid>
    Re: Question about the example in perlthrtut sln@netherlands.com
    Re: Question about the example in perlthrtut <xhoster@gmail.com>
    Re: Question about the example in perlthrtut sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 01 Apr 2009 00:59:33 GMT
From: sln@netherlands.com
Subject: Re: Being more restrictive when using blessed hash ref for OOP
Message-Id: <kle5t4dq820bgjp8vqui4vnquojoco9uod@4ax.com>

On Tue, 31 Mar 2009 08:32:23 -0700 (PDT), jl_post@hotmail.com wrote:

>On Mar 30, 6:55 am, "A. Farber" <Alexander.Far...@gmail.com> wrote:
>>
>> I define and use several classes in my Perl application,
>> and I use blessed hash refs to store the data members.
>>
>> Often I wonder, if there is a possibility for being
>> more restrictive - for example I'd prefer to get
>> a warning, when I reference or vivificate a
>> $href->{PLAYER} instead of $href->{PLAYERS} by mistake
>>
>> Any suggestions please?
>
>
>   You might want to take a look at the "fields" module.  (Read about
>it with "perldoc fields".)
>
>   It's been years since I last used it (so my memory on it is a bit
>sketchy), but it'll allow you to do things like:
>
>#/usr/bin/perl
>package Game;
>use strict;
>use warnings;
>use fields qw(PLAYERS);
>sub new
>{
>    my Game $self = shift;
     ^^^^^^^^^^^^^^^^^^^^^
I'm not from oldschool. What syntax is this?

>    $self = fields::new($self)  unless (ref $self);
     ^^^.........
Can you explain this briefly. No use putting down stuff somebody
has to explore. Where is the bless?

>    return $self;
>}
>my $href = Game->new;
>$href->{PLAYERS} = [];  # acceptable
>$href->{PLAYER} = [];  # generates an error
                          ^^^^^^^^^^^^^^^^^
Well that would double/tripple execution speed wouldn't it?
Looks like overloaded '->' if this were C++ and '->' were an operator.

>__END__
>
>   When I try to run this script, I get:
>
>Attempt to access disallowed key 'PLAYER' in a restricted hash at -
>line 14.
>
>   I THOUGHT barewords would be checked at compile-time, but my own
>experimentation just now leads me to believe it's a run-time check.

How did you draw that conclusion? Why wouldn't barewords be compile-time?

>Either way, that's better than nothing.
>
>   I hope this helps.
>
>   -- Jean-Luc

-sln



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

Date: Tue, 31 Mar 2009 23:13:09 +0100
From: sandeep <nospam@nospam.com>
Subject: finding maximum
Message-Id: <gqu4gd$ft8$1@aioe.org>

Hello Friends ~

How would you find the maximum of
(a) two numbers
(b) a set of numbers
in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
for a discussion of the differences in expressive power of the two
languages.

Thx!

Sandeep



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

Date: Wed, 01 Apr 2009 00:14:16 +0200
From: jacob navia <jacob@nospam.org>
Subject: Re: finding maximum
Message-Id: <49d295bf$0$2762$ba4acef3@news.orange.fr>

sandeep wrote:
> Hello Friends ~
> 
> How would you find the maximum of
> (a) two numbers
> (b) a set of numbers
> in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
> for a discussion of the differences in expressive power of the two
> languages.
> 
> Thx!
> 
> Sandeep
> 

OK. To make it easier for you, can you tell us
the name of your teacher?

We will send him the answer directly.

thanks


-- 
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32


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

Date: Wed, 01 Apr 2009 01:18:57 +0300
From: Phil Carmody <thefatphil_demunged@yahoo.co.uk>
Subject: Re: finding maximum
Message-Id: <87bprhibe6.fsf@nonospaz.fatphil.org>

sandeep <nospam@nospam.com> writes:
> Hello Friends ~
>
> How would you find the maximum of
> (a) two numbers
> (b) a set of numbers
> in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
> for a discussion of the differences in expressive power of the two
> languages.

Can you let us have your teacher's email address, so that we
can get your homework answers to him directly, to save you the 
effort.

Phil
-- 
Marijuana is indeed a dangerous drug.  
It causes governments to wage war against their own people.
-- Dave Seaman (sci.math, 19 Mar 2009)


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

Date: 31 Mar 2009 22:45:55 GMT
From: "Default User" <defaultuserbr@yahoo.com>
Subject: Re: finding maximum
Message-Id: <73fkp3Fuujf2U1@mid.individual.net>

sandeep wrote:

> Hello Friends ~
> 
> How would you find the maximum of
> (a) two numbers
> (b) a set of numbers
> in idiomatic C? How would you do it in idiomatic Perl? Use this as a
> basis for a discussion of the differences in expressive power of the
> two languages.

A better idea would be for you to do the homework that you've been
assigned. The assignment was for a reason, that is to teach you some
programming concepts and techniques.



Brian

-- 
Day 56 of the "no grouchy usenet posts" project


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

Date: Wed, 01 Apr 2009 00:16:31 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: finding maximum
Message-Id: <Xns9BDFCE3E133A6asu1cornelledu@127.0.0.1>

sandeep <nospam@nospam.com> wrote in news:gqu4gd$ft8$1@aioe.org:

> How would you find the maximum of
> (a) two numbers
> (b) a set of numbers

 ...

> How would you do it in idiomatic Perl?

http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm

> Use this as a basis for a discussion of the differences in expressive 
> power of the two languages.

Hopeless. We don't do your homework for you. 

Sinan

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

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


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

Date: Wed, 01 Apr 2009 00:43:04 GMT
From: sln@netherlands.com
Subject: Re: finding maximum
Message-Id: <93e5t415u5ijdakvfnu3f812bmh7otdl29@4ax.com>

On Tue, 31 Mar 2009 23:13:09 +0100, sandeep <nospam@nospam.com> wrote:

>Hello Friends ~
>
>How would you find the maximum of
>(a) two numbers
>(b) a set of numbers
>in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
>for a discussion of the differences in expressive power of the two
>languages.
>
>Thx!
>
>Sandeep

What is "idiomatic" is that a washing machine gone bad?

-sln



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

Date: Tue, 31 Mar 2009 18:08:50 -0700 (PDT)
From: Spiros Bousbouras <spibou@gmail.com>
Subject: Re: finding maximum
Message-Id: <fd6272f5-75b0-4b07-b412-dd5657e8c74f@u8g2000yqn.googlegroups.com>

On 31 Mar, 23:18, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
> sandeep <nos...@nospam.com> writes:
> > Hello Friends ~
>
> > How would you find the maximum of
> > (a) two numbers
> > (b) a set of numbers
> > in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
> > for a discussion of the differences in expressive power of the two
> > languages.
>
> Can you let us have your teacher's email address, so that we
> can get your homework answers to him directly, to save you the
> effort.

Or perhaps it's flamebait disguised as homework.


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

Date: Tue, 31 Mar 2009 22:11:08 -0400
From: Eric Sosman <esosman@ieee-dot-org.invalid>
Subject: Re: finding maximum
Message-Id: <gquif8$m7k$1@news.motzarella.org>

sandeep wrote:
> Hello Friends ~
> 
> How would you find the maximum of
> (a) two numbers

     Negate both, find the minimum, and negate that minimum.
Be careful of corner cases: On many systems negating the most
negative integer fails to produce a positive integer (which
could lead to an incorrect answer), and on some systems there
are numbers X,Y such that X<=Y and Y<=X are *both* false.
A subtle problem; I wish you luck with it.

> (b) a set of numbers

     Much harder, because in a set of N numbers there are
(N choose 2) = N * (N-1) / 2 pairs to consider.  Even for a
fairly modest set size like N = 1000000 it can easily happen
that (N choose 2) exceeds the maximum value of the system's
integer.

     So, I suggest a heuristic approach.  Choose a pair of the
numbers at random, and determine their maximum by applying the
solution to (a).  Then choose another pair at random and find
*their* maximum the same way.  Now (and here's the crux) you
have formed the leftmost two pieces of a tree in which all the
numbers appear:

	      .      .
	     .        .            .
	    /          \          .
	max(A,B)    max(C,D)     .
	 /   \       /   \      /
	A     B     C     D    E    ...

Using a stack (or possibly a priority queue) you can recursively
compute max(A,B,C,D) = max(max(A,B), max(C,D)), and then
max(A,B,C,D,E,F,G,H) = max(max(A,B,C,D), max(E,F,G,H))
= max(max(max(A,B), max(C,D)), max(max(E,F), max(G,H))) and
so on, populating the tree in a bottom-up left-to-right
fashion.  Eventually, the root of the tree will be equal to the
maximum of the entire set, with a probability close to unity.
There's a strong analogy to the Mergesort algorithm: If you
consider the tree as a representation of a merge sort, then
the desired maximum of N elements is the unique element that
is not among the first N-1 emitted.

> in idiomatic C?

     There is no such thing as "idiomatic C."  There are only
"High C" (as practiced during the High C Era), and "Trade C"
(the patois that has taken hold among those who'd rather get
things done than argue about the proprieties).  There are also
some debased C-ish dialects, notably "Rapper's C" or "CRap."

> How would you do it in idiomatic Perl?

     Ah!  A much easier question!  Answer: I would not do it
in any kind of Perl, idiotmatic or otherwise.

> Use this as a basis
> for a discussion of the differences in expressive power of the two
> languages.

     Both languages are Turing-complete, that is, capable of
expressing methods that solve all computable functions (subject
to the finiteness limits of the computers they execute on.)  Also,
both languages are Virgil-complete, that is, capable of expressing
figurative and poetic thought, as in

	C2shining(C)

and

	Perl + Perl <- Swine

 > Thx!

     D'rn.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid


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

Date: Tue, 31 Mar 2009 20:14:15 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: finding maximum
Message-Id: <slrngt5fv7.lqs.tadmc@tadmc30.sbcglobal.net>


["Followup-To:" header set to comp.lang.perl.misc.]


Spiros Bousbouras <spibou@gmail.com> wrote:
> On 31 Mar, 23:18, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
> wrote:
>> sandeep <nos...@nospam.com> writes:
>> > Hello Friends ~
>>
>> > How would you find the maximum of
>> > (a) two numbers
>> > (b) a set of numbers
>> > in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
>> > for a discussion of the differences in expressive power of the two
>> > languages.
>>
>> Can you let us have your teacher's email address, so that we
>> can get your homework answers to him directly, to save you the
>> effort.
>
> Or perhaps it's flamebait disguised as homework.


The poster's address should have been a big clue.

I have that one killfiled.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Wed, 01 Apr 2009 03:15:06 GMT
From: sln@netherlands.com
Subject: Re: finding maximum
Message-Id: <31n5t45jd8f54aubr8m9nrj34oqndvj588@4ax.com>

On Tue, 31 Mar 2009 20:14:15 -0500, Tad J McClellan <tadmc@seesig.invalid> wrote:

>
>["Followup-To:" header set to comp.lang.perl.misc.]
>
>
>Spiros Bousbouras <spibou@gmail.com> wrote:
>> On 31 Mar, 23:18, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
>> wrote:
>>> sandeep <nos...@nospam.com> writes:
>>> > Hello Friends ~
>>>
>>> > How would you find the maximum of
>>> > (a) two numbers
>>> > (b) a set of numbers
>>> > in idiomatic C? How would you do it in idiomatic Perl? Use this as a basis
>>> > for a discussion of the differences in expressive power of the two
>>> > languages.
>>>
>>> Can you let us have your teacher's email address, so that we
>>> can get your homework answers to him directly, to save you the
>>> effort.
>>
>> Or perhaps it's flamebait disguised as homework.
>
>
>The poster's address should have been a big clue.
>
>I have that one killfiled.

Congratulations!

-sln



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

Date: Tue, 31 Mar 2009 22:24:27 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: finding maximum
Message-Id: <slrngt5njb.ml3.tadmc@tadmc30.sbcglobal.net>


["Followup-To:" header set to comp.lang.perl.misc.]


Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
> sandeep wrote:

>> in idiomatic C?
>
>      There is no such thing as "idiomatic C."  There are only
> "High C" (as practiced during the High C Era), and "Trade C"
> (the patois that has taken hold among those who'd rather get
> things done than argue about the proprieties).  There are also
> some debased C-ish dialects, notably "Rapper's C" or "CRap."


Leaving out "Tenne C" is inexcusable.

    http://www.netfunny.com/rhf/jokes/88q3/0tenny.html


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Wed, 01 Apr 2009 03:33:45 +0000
From: Richard Heathfield <rjh@see.sig.invalid>
Subject: Re: finding maximum
Message-Id: <X-SdnSN3zp9BfU_UnZ2dnUVZ8oWWnZ2d@bt.com>

[I note that the OP has not yet had any responses that move him 
forward technically (nice try, though, Eric!). So the first part of 
this response is a token effort in that direction.]

sandeep said:

> Hello Friends ~
> 
> How would you find the maximum of
> (a) two numbers

Use the comparison operator.

> (b) a set of numbers

Store the first number in the set, making it the "current maximum". 
Then use the comparison operator in a loop, updating the current 
maximum whenever appropriate.

> in idiomatic C?

See above.

> How would you do it in idiomatic Perl?

Well, personally I wouldn't do it in Perl, but no doubt the fine 
folks in comp.lang.perl.misc /would/ do it in Perl, and that is 
their right and privilege.

> Use this as
> a basis for a discussion of the differences in expressive power of
> the two languages.

C is expressively powerful, whereas Perl is powerfully expressive.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


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

Date: Tue, 31 Mar 2009 21:01:36 -0700 (PDT)
From: Andrey Vul <andrey.vul@gmail.com>
Subject: Re: finding maximum
Message-Id: <8412e4f1-2296-4e2f-bfe0-229b7511112d@r18g2000vbi.googlegroups.com>

On Mar 31, 10:11=A0pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> sandeep wrote:
> > Hello Friends ~
>
> > How would you find the maximum of
> > (a) two numbers
>
> =A0 =A0 =A0Negate both, find the minimum, and negate that minimum.
> Be careful of corner cases: On many systems negating the most
> negative integer fails to produce a positive integer (which
> could lead to an incorrect answer), and on some systems there
> are numbers X,Y such that X<=3DY and Y<=3DX are *both* false.
> A subtle problem; I wish you luck with it.
>
> > (b) a set of numbers
>
> =A0 =A0 =A0Much harder, because in a set of N numbers there are
> (N choose 2) =3D N * (N-1) / 2 pairs to consider. =A0Even for a
> fairly modest set size like N =3D 1000000 it can easily happen
> that (N choose 2) exceeds the maximum value of the system's
> integer.
>
> =A0 =A0 =A0So, I suggest a heuristic approach. =A0Choose a pair of the
> numbers at random, and determine their maximum by applying the
> solution to (a). =A0Then choose another pair at random and find
> *their* maximum the same way. =A0Now (and here's the crux) you
> have formed the leftmost two pieces of a tree in which all the
> numbers appear:
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 . =A0 =A0 =A0.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0. =A0 =A0 =A0 =A0. =A0 =A0 =A0 =A0 =A0 =A0.
> =A0 =A0 =A0 =A0 =A0 =A0 / =A0 =A0 =A0 =A0 =A0\ =A0 =A0 =A0 =A0 =A0.
> =A0 =A0 =A0 =A0 max(A,B) =A0 =A0max(C,D) =A0 =A0 .
> =A0 =A0 =A0 =A0 =A0/ =A0 \ =A0 =A0 =A0 / =A0 \ =A0 =A0 =A0/
> =A0 =A0 =A0 =A0 A =A0 =A0 B =A0 =A0 C =A0 =A0 D =A0 =A0E =A0 =A0...
>
> Using a stack (or possibly a priority queue) you can recursively
> compute max(A,B,C,D) =3D max(max(A,B), max(C,D)), and then
> max(A,B,C,D,E,F,G,H) =3D max(max(A,B,C,D), max(E,F,G,H))
> =3D max(max(max(A,B), max(C,D)), max(max(E,F), max(G,H))) and
> so on, populating the tree in a bottom-up left-to-right
> fashion. =A0Eventually, the root of the tree will be equal to the
> maximum of the entire set, with a probability close to unity.
> There's a strong analogy to the Mergesort algorithm: If you
> consider the tree as a representation of a merge sort, then
> the desired maximum of N elements is the unique element that
> is not among the first N-1 emitted.
>
> > in idiomatic C?
>
> =A0 =A0 =A0There is no such thing as "idiomatic C." =A0There are only
> "High C" (as practiced during the High C Era), and "Trade C"
> (the patois that has taken hold among those who'd rather get
> things done than argue about the proprieties). =A0There are also
> some debased C-ish dialects, notably "Rapper's C" or "CRap."
>
> > How would you do it in idiomatic Perl?
>
> =A0 =A0 =A0Ah! =A0A much easier question! =A0Answer: I would not do it
> in any kind of Perl, idiotmatic or otherwise.
>
> > Use this as a basis
> > for a discussion of the differences in expressive power of the two
> > languages.
>
> =A0 =A0 =A0Both languages are Turing-complete, that is, capable of
> expressing methods that solve all computable functions (subject
> to the finiteness limits of the computers they execute on.) =A0Also,
> both languages are Virgil-complete, that is, capable of expressing
> figurative and poetic thought, as in
>
> =A0 =A0 =A0 =A0 C2shining(C)
>
> and
>
> =A0 =A0 =A0 =A0 Perl + Perl <- Swine
>
> =A0> Thx!
>
> =A0 =A0 =A0D'rn.


You have made my day. Thank you so much.


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

Date: Wed, 1 Apr 2009 00:48:01 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Interpreting terminal codes in Perl
Message-Id: <pan.2009.03.31.22.48.01@rtij.nl.invlalid>

On Tue, 31 Mar 2009 08:07:39 +0200, Martijn Lievaart wrote:

> On Mon, 30 Mar 2009 23:32:33 +0100, Ben Morrow wrote:
> 
>> I don't know of anything that will do this. However, it shouldn't be
>> too hard to extract a list of valid capability names from your
>> termcap(5) or terminfo(5) and then call Term::Cap->Tgets on each to
>> build up a hash from control string to name for the current terminal.
>> OTOH, you may find it easier to work directly from the terminfo master
>> file at http://www.catb.org/terminfo/termtypes.master.gz. It doesn't
>> look too hard to write a parser for.
> 
> Thanks!
> 

I am currently encoding the full xterm set, I need to cover all the 
cases, not just those described by terminfo/termcap (although that covers 
99.99% of the cases). In the end I hope to fully cover xterm*/ansi*/vt*, 
and will do the rest through reversing the termcap.

M4


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

Date: Tue, 31 Mar 2009 12:30:21 -0700 (PDT)
From: A Dude <jjcassidy@gmail.com>
Subject: Re: Pass Match Values to Variable
Message-Id: <4a86c821-ae10-4794-9d4c-81c0cf2c5172@d19g2000prh.googlegroups.com>

On Mar 31, 8:02=A0am, perl Newbie <Anshe...@gmail.com> wrote:
> Hi
>
> Please let me know what I am doing wrong .. I need to pass the regex
> match values to a new variable.
>
> This is the code .. but both =A0$capture_txt =A0& @nettxt is blank, value=
s
> I am expecting in @nettxt are
>
> SAMPLE (NET) & SINGLE (net).
>
> @txt=3D(
> "SAMPLE (NET)"
> ,"Sample txt 1"
> ,"sample txt 2"
> ,"SINGLE (net) junk text"
> ,"Single txt 1"
> ,"Single txt 2"
> );
>
> foreach $txt(@txt) {
>
> =A0 =A0 =A0 =A0 if ($txt =3D~ /\w+\s*\(net\)/i) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "True - $txt \n";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $capture_txt =3D /\w+\s*\(net\)/ ;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "$capture_txt \n";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 push @nettxt, /(\w+\s*\(net\))/g;
> =A0 =A0 =A0 =A0 }
>
> }
>
> print @nettxt;

Shorter and simpler:

my @nettxt =3D map { /(\w+\s*\(net\))/gi } @txt;


What I think you wanted to do with $capture_txt is:

my ( $capture_text ) =3D $txt =3D~ /(\w+\s*\(net\))/i;
if ( $capture_text ) {
   ...
}

You're not capturing anything with \(...\), you're just looking for
'(net)'. You need un-escaped '(' ( and ')') to capture anything.


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

Date: Tue, 31 Mar 2009 15:58:24 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Pass Match Values to Variable
Message-Id: <slrngt50vg.j54.tadmc@tadmc30.sbcglobal.net>

perl Newbie <Ansher.M@gmail.com> wrote:
> On Mar 31, 6:49?pm, Tad J McClellan <ta...@seesig.invalid> wrote:

[ snip a bunch ]

>> --
>> Tad McClellan
>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -


It is bad manners to quote .sigs.



>> - Show quoted text -


Don't quote that silliness either please.


> Thanks for your detailed explanation.
>
> I am new to Perl (learning for the last 15 days) and using "Learning
> Perl" book. 


I am very familiar with that book (check the Acknowledgements).


> But it doesn?t help me much in understanding the way you
> have explained the details here 


Yes it does.

It covers every single thing that I mentioned.

Read it again. Slowly.


>  Some of the other links I freq. use
> are
>
> http://www.physics.rutgers.edu/~kotliar/perltut.html


Errr, a lot has changed in 10 years...


> Could you please suggest any book or links which will help me to
> better understand the concepts?


The absolutely most authoritative source of information about Perl
is the documentation that comes with Perl.

It is better than "Learning Perl" or any other printed book.

It is better than any random web page.

In about five or six paragraphs it addressed every problem you were having.

Use the docs, Luke.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Wed, 01 Apr 2009 00:35:51 GMT
From: sln@netherlands.com
Subject: Re: Question about the example in perlthrtut
Message-Id: <3l85t49li2bhtoajda14ebuvumecfhe224@4ax.com>

On Tue, 31 Mar 2009 07:21:51 -0700 (PDT), "jl_post@hotmail.com" <jl_post@hotmail.com> wrote:

>
>Hi,
>
>   I recently read the "perldoc perlthrtut" document to learn how to
>do threading with Perl, and I understand most of what it's teaching.
>However, I have a question regarding the complete example given (the
>one about generating prime numbers using a pipeline model):
>
>
>#!/usr/bin/perl
># prime-pthread, courtesy of Tom Christiansen
>
>use strict;
>use warnings;
>
>use threads;
>use Thread::Queue;
>
>my $stream = Thread::Queue->new();
>for my $i ( 3 .. 1000 ) {
>    $stream->enqueue($i);
>}
>$stream->enqueue(undef);
                  ^^^^^
This last scalar in the queue will break out of the 
while function in the first thread. But the $kid->join()
below that block will wait for the 2nd thread to complete,
which is waiting for the 3rd, and so on..
So the first thread will not complete untill the last thread
does.

>
>threads->create(\&check_num, $stream, 2)->join();
                                           ^^^^^^
This first thread has a full queue and the join() blocks
until the thread ends.

>
>sub check_num {
>    my ($upstream, $cur_prime) = @_;
>    my $kid;
>    my $downstream = Thread::Queue->new();
                         ^^^^^^^^^
Starting a new queue. The queue is thread-safe.
Items will be added by the parent thread and taken off
by the direct child thread.

>    while (my $num = $upstream->dequeue()) {
                                 ^^^^^^^
This will take an item off the head of the queue looking
for undef value to break out of the while block.
If there is nothing in the queue it blocks if the thread is
still alive (it will be).

>        next unless ($num % $cur_prime);
         ^^^^
Since the first threads $cur_prime was 2 and based on ($num % $cur_prime),
it found a new prime of 3. A new thread is created with prime of 3.

>        if ($kid) {
>            $downstream->enqueue($num);
                          ^^^^^^^^^^^^^
The first thread, since its prime was 2, only adds odd numbers to the child
queue. So after 3, the next odd number it adds is 5.

>        } else {
>            print("Found prime $num\n");
>            $kid = threads->create(\&check_num, $downstream, $num);
                             ^^^^^^
First thread creates a new second child thread and passes the prime 3 and an empty queue
which blocks immediatly waiting for its parent thread to add some items.

>        }
>    }

End of the while, all the threads have been created and are waiting for cascading information
to flow into thier queue's from parent threads. Each thread is started on a new prime.
Its basically a bubble sort with queue size decreasing with every new thread.

>    if ($kid) {
>        $downstream->enqueue(undef);
                              ^^^^^
Finally, trigger all the threads to break out of the while loop, last to first.

>        $kid->join();
               ^^^^^^
Cleanup, wait for all the threads to finish, last to the first.
>    }
>}
>
>
>(For the record, I deleted the first "$kid->join();" line (the one
>that was right after the first "thread->create" call) and appended "-
>>join()" to the previous line to make the program compile.)
>
>If I understand correctly, each thread takes a queue of numbers,
>reports that the first is prime (otherwise it wouldn't be first in the
>queue), and then starts a new thread.  The old thread then gives the
>new thread all the numbers in its queue that are not multiples of the
>prime number it reported.
>
>I ran the script, and it works great.  However, it seems that a thread
>won't end until its child thread (that is, the thread it created/
>started) ends.  And since it seems like it creates another thread for
>every prime number, that would mean that around 167 different threads
>can be running (or at least exist) at once.  (I say 167 because there
>are 167 primes from 3 to 1000.)
>
>So am I correct in saying that there are over 100 threads running at
>once?

Yes, there were 115 runnon on my machine.

>  If so, isn't that a bit taxing on the operating system?  I
>mean, I've noticed that in some cases over 100 levels of recursion can
>error out (or at least give a warning message), so wouldn't
>recursively creating a thread over 100 times also be something we'd
>want to avoid?  Or are threads different in this respect?

It appeared relatively slow on my windows box. So I think you have a point there.
But, I don't know how threads are implemented in Perl too well. I will have to
read the limited information.

OTOH, the data transformation/cascading effect, this alogrithym does, may have merits
in its implementation.
I am not a guru on calculating primes so I don't know.

I haven't found the "threads" documentation (I just read the Threads in the help docs)
where you got this code, so I will have to look for that.

I did find that $kid = threads->create(\&check_num, $downstream, $num); failed towards the
end, seemingly due to too many threads per process. How this affects cascading I don't know.
There is one thing for sure, more needs to be discovered here. I wouldn't put this code into
production.

-sln



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

Date: Tue, 31 Mar 2009 20:38:11 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: Question about the example in perlthrtut
Message-Id: <49d2e491$0$6905$ed362ca5@nr5-q3a.newsreader.com>

jl_post@hotmail.com wrote:
> Hi,
> 
>    I recently read the "perldoc perlthrtut" document to learn how to
> do threading with Perl, and I understand most of what it's teaching.
> However, I have a question regarding the complete example given (the
> one about generating prime numbers using a pipeline model):
> 
 ...
> 
> If I understand correctly, each thread takes a queue of numbers,
> reports that the first is prime (otherwise it wouldn't be first in the
> queue), and then starts a new thread.  The old thread then gives the
> new thread all the numbers in its queue that are not multiples of the
> prime number it reported.
> 
> I ran the script, and it works great.  However, it seems that a thread
> won't end until its child thread (that is, the thread it created/
> started) ends.  And since it seems like it creates another thread for
> every prime number, that would mean that around 167 different threads
> can be running (or at least exist) at once.  (I say 167 because there
> are 167 primes from 3 to 1000.)
> 
> So am I correct in saying that there are over 100 threads running at
> once? 

Yes.  Well, not necessarily actively "running", but existing, anyway. 
Notice that this example is just a pedagogical example.  It is like 
using Fibonacci's sequence as an example of recursion.  If my actual 
goal was to obtain prime numbers, I certainly wouldn't use this threaded 
implementation to do it, just like I wouldn't use naive recursion to get 
Fibonacci numbers.

> If so, isn't that a bit taxing on the operating system? 

That depends on your OS and your hardware.  My rather sad netbook, 
running a rather sad hack of Linux, can handle it OK up to 1000.  Up to 
3000, it has trouble.  10,000 and it swaps to do death.  Of course on a 
single CPU machine, there is no point in using threading on a CPU 
limited problem.


> I
> mean, I've noticed that in some cases over 100 levels of recursion can
> error out (or at least give a warning message),

It gives a warning, which can be turned off.  I don't even know why it 
is on by default.  It says that it "probably indicates infinite 
recursion" but there all kinds of ways to make infinite loops that don't 
get their own special warnings.  The only other infinite loops that do 
get warnings are ones that happen in the c-level code, not the user 
level.  Anyway, I was recent using a program that recursed to a depth of 
over 40,000.  No problem at all, as long as that is what you want to do.

> so wouldn't
> recursively creating a thread over 100 times also be something we'd
> want to avoid? 

You would generally want to avoid that.  But this code is for playing, 
not for practical applications.

Xho





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

Date: Wed, 01 Apr 2009 04:05:52 GMT
From: sln@netherlands.com
Subject: Re: Question about the example in perlthrtut
Message-Id: <0vp5t4dm384offfnbbnp8rib3j7i9i66sq@4ax.com>

On Tue, 31 Mar 2009 20:38:11 -0700, Xho Jingleheimerschmidt <xhoster@gmail.com> wrote:

>jl_post@hotmail.com wrote:
>> Hi,
>> 
>>    I recently read the "perldoc perlthrtut" document to learn how to
>> do threading with Perl, and I understand most of what it's teaching.
>> However, I have a question regarding the complete example given (the
>> one about generating prime numbers using a pipeline model):
>> 
>...
>> 
>> If I understand correctly, each thread takes a queue of numbers,
>> reports that the first is prime (otherwise it wouldn't be first in the
>> queue), and then starts a new thread.  The old thread then gives the
>> new thread all the numbers in its queue that are not multiples of the
>> prime number it reported.
>> 
>> I ran the script, and it works great.  However, it seems that a thread
>> won't end until its child thread (that is, the thread it created/
>> started) ends.  And since it seems like it creates another thread for
>> every prime number, that would mean that around 167 different threads
>> can be running (or at least exist) at once.  (I say 167 because there
>> are 167 primes from 3 to 1000.)
>> 
>> So am I correct in saying that there are over 100 threads running at
>> once? 
>
>Yes.  Well, not necessarily actively "running", but existing, anyway. 
>Notice that this example is just a pedagogical example.  It is like 
>using Fibonacci's sequence as an example of recursion.  If my actual 
>goal was to obtain prime numbers, I certainly wouldn't use this threaded 
>implementation to do it, just like I wouldn't use naive recursion to get 
>Fibonacci numbers.
>
>> If so, isn't that a bit taxing on the operating system? 
>
>That depends on your OS and your hardware.  My rather sad netbook, 
>running a rather sad hack of Linux, can handle it OK up to 1000.  Up to 
>3000, it has trouble.  10,000 and it swaps to do death.  Of course on a 
>single CPU machine, there is no point in using threading on a CPU 
>limited problem.
>
>
>> I
>> mean, I've noticed that in some cases over 100 levels of recursion can
>> error out (or at least give a warning message),
>
>It gives a warning, which can be turned off.  I don't even know why it 
>is on by default.  It says that it "probably indicates infinite 
>recursion" but there all kinds of ways to make infinite loops that don't 
>get their own special warnings.  The only other infinite loops that do 
>get warnings are ones that happen in the c-level code, not the user 
>level.  Anyway, I was recent using a program that recursed to a depth of 
>over 40,000.  No problem at all, as long as that is what you want to do.
>
>> so wouldn't
>> recursively creating a thread over 100 times also be something we'd
>> want to avoid? 
>
>You would generally want to avoid that.  But this code is for playing, 
>not for practical applications.
>
>Xho
>
>
You haven't a clue about this algo or threading. Disgracefull explanations.

-sln



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

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


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