[19683] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1878 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 5 11:10:45 2001

Date: Fri, 5 Oct 2001 08:10:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1002294610-v10-i1878@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 5 Oct 2001     Volume: 10 Number: 1878

Today's topics:
    Re: Coding pratices (Anno Siegel)
        Finding the difference between arrays... (JR)
    Re: Finding the difference between arrays... <fty@mediapulse.com>
        Goedel JAPH <lt@toetsch.at>
    Re: Multiplexing strings - one line only <JPFauvelle@Colt-Telecom.fr>
    Re: Multiplexing strings <joe+usenet@sunstarsys.com>
    Re: Multiplexing strings <JPFauvelle@Colt-Telecom.fr>
    Re: Multiplexing strings <dtweed@acm.org>
    Re: Multiplexing strings <JPFauvelle@Colt-Telecom.fr>
    Re: Need help to suppress STDOUT <tambaa@no.spam.yahoo.com>
    Re: Nested loops and scoping $_ <joe+usenet@sunstarsys.com>
        Rewinding __DATA__ ? <nomail@hursley.ibm.com>
    Re: Rewinding __DATA__ ? <joe+usenet@sunstarsys.com>
    Re: Rewinding __DATA__ ? <mjcarman@home.com>
    Re: Skipping following lines if the same <jasper@guideguide.com>
        warnings and $a, $b <djberge@qwest.com>
    Re: warnings and $a, $b <djberge@qwest.com>
    Re: Yet another fork question (Villy Kruse)
    Re: Yet another fork question (Anno Siegel)
    Re: Yet another fork question (Randal L. Schwartz)
    Re: Yet another fork question (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 5 Oct 2001 14:14:16 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Coding pratices
Message-Id: <9pkf7o$iks$1@mamenchi.zrz.TU-Berlin.DE>

According to Michael Carman  <mjcarman@home.com>:
> Dylan Riley wrote:

[one big source or many small modules?]

> As a general rule; if you have a piece of functionality that you may
> want to use in multiple scripts, it makes sense to factor it out and
> make it into a module. Anything unique to the current script normally
> goes in the main file.
>  
> > I'm faced with the dilema of either making the script bigger and
> > bigger or cutting it into a bunch of smaller interconnected files.
> 
> At some (arbitrary) point, it's probably a good idea to break it apart.
> It will make maintenance easier. When you reach that point, try to find
> logical subsets of the functionality and break things apart that way.

Breaking apart source code that has grown too big is hard.  *Because*
it has all been developed in one file, there tend to be cross-dependencies
that could have been avoided, and which now make it hard even to discern
the logical subsets, let alone separate them.

Putting together modules in a single file is usually far easier.  There-
for I recommend to break out subparts early and often, even if they don't
seem to deserve to be an independent module at the time.  That way you
get to keep clean interfaces between the parts, even if they finally end
up in one file.  It will help making the code maintainable.

Anno


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

Date: 5 Oct 2001 07:02:59 -0700
From: tommyumuc@aol.com (JR)
Subject: Finding the difference between arrays...
Message-Id: <319333f5.0110050602.5c0c210e@posting.google.com>

For the following array examples, how is it possible to determine the
difference?  I can't use hashes, because in the actual arrays there
would be many duplicate keys, which aren't allowed in hashes.  I can't
just split the fields and do something like "$arrayOne[$_] !=
$arrayTwo[$_]" because, as in the below example, that would return row
0, 2, and 3.  The true difference between the two arrays is:

01,001,000,0003 and 01,001,000,0002

because there are 3 (01,001,000,0003) in @arrayOne and 2 in @arrayTwo
and there is 1 (01,001,000,0002) in @arrayOne and 2 in @arrayTwo.

My guess is that I need to, in some way, pop, shift, or splice to
remove elements that are compared and found to be equal.  The problem
I'm having is how to loop through the arrays, and then delete elements
of those arrays after a match has been found (a runtime error occurs,
usually).


@arrayOne             @arrayTwo
---------             ---------
01,001,000,0002       01,001,000,0003
01,001,000,0003       01,001,000,0003
01,001,000,0003       01,001,000,0002
01,001,000,0003       01,001,000,0002
 ...                   ...

(DESIRED RESULT)
@fallOut
--------
01,001,000,0003
01,001,000,0002

I hope I've explained this problem reasonably well.  I would
appreciate any help anyone may have.


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

Date: Fri, 05 Oct 2001 14:34:18 GMT
From: "Jay Flaherty" <fty@mediapulse.com>
Subject: Re: Finding the difference between arrays...
Message-Id: <Kxjv7.808839$ai2.62031923@bin2.nnrp.aus1.giganews.com>

"JR" <tommyumuc@aol.com> wrote in message
news:319333f5.0110050602.5c0c210e@posting.google.com...
> For the following array examples, how is it possible to determine the
> difference?
http://www.perldoc.com/perl5.6.1/pod/perlfaq4.html#How-do-I-compute-the-diff
erence-of-two-arrays---How-do-I-compute-the-intersection-of-two-arrays-

Jay




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

Date: Fri, 5 Oct 2001 16:15:12 +0200
From: Leopold Toetsch <lt@toetsch.at>
Subject: Goedel JAPH
Message-Id: <slrn9rrg3g.355.lt@toetsch.at>

Hi JAPHs,

for your weekend fun, I have a new one:

#!/usr/bin/perl -lw
use strict;
use English;
## Please note the <spaces> in lines 6, 16 & 17
$_=q!
y[Ao-qBPdghl1] [1];
tr/C\nnH;#/;/;
y[b]{1};
s[y]{1}g;
tr/5/1/d;
y/;//ds;
y[8\}\(.,][1];
#-lt,2001(lt)
tr[12$](111);
tr(01){0}c;#1
s([001]{5}) .pack'b8',  "$&010".geoms;
y.YGFBM. ="'$.;
s/\B./lc$MATCH/eg;
s[P](p);
s/A/a/;
!; #'"
eval and eval eval;


Have fun,
leo
-- 
Oct 05 16:15:32 lux siggen[3834]: reading next random sig
Oct 05 16:15:32 lux siggen[3834]: writing ~/.sognature


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

Date: Fri, 05 Oct 2001 15:38:06 +0200
From: Jean-Philippe Fauvelle <JPFauvelle@Colt-Telecom.fr>
Subject: Re: Multiplexing strings - one line only
Message-Id: <eibrrt84b8jv17d4965sbac5s6chqae7q2@4ax.com>

Le 4 Oct 2001 15:46:21 -0700, mauroid@csi.forth.gr (Dimitri) +AOk-crit:

@STR= ($str1,$str2,$str3);

# number of strings
$N = @STR;

# chars per token
$M = 2;  

# string length
$S = length $STR[0]; 


$out = join '', 
	( join('',@STR) =+AH4- /(.{$M})/gos )[
		map { $_, $_+-$S/$M, $_+-2*$S/$M }  0 .. ($S/$M)-1
	];

one line only ! 

Jean-Philippe Fauvelle


>I have N=3 strings of equal length, and I want to multiplex them by taking
>M=2 characters from each string (the length of each string is divisible by
>M). Example :
>
>Input:
>
>$str1 = "aabbccddeeff";
>$str2 = "AABBCCDDEEFF";
>$str3 = "001122334455";
>
>Output :
>
>$out = "aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55";
>
>Besides the obvious for loop :
>
>$out = ""; $len = length($str1);
>
>for ($i = 0; $i < $len; $i +-= 2) {
> $out .= substr($str1, $i, 2) . substr($str2, $i, 2) . substr($str3, $i, 2);
>}
>
>Is there a more elegant (faster) way?
>
>Thanks!
>-Dimitri



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

Date: 05 Oct 2001 09:14:01 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Multiplexing strings
Message-Id: <m3r8sii606.fsf@mumonkan.sunstarsys.com>

Dave Tweed <dtweed@acm.org> writes:

> Joe Schaefer wrote:
> > Wins what?  I didn't realize there was a prize to be had for
> > avoiding subroutine calls in the solution.
> 
> The OP asked, "Is there a more elegant (faster) way?"

Notice that "faster" is in parens.  I certainly feel
swrite() is more elegant, and is well-suited to the 
problem. 

But since you wanna talk speed...

% cat try.pl
#!/usr/bin/perl -w
  $str1 = "aabbccddeeff";
  $str2 = "AABBCCDDEEFF";
  $str3 = "001122334455";
  $ARGV[0] ||= 0;
  for ($str1, $str2, $str3) {
    $_ .= $_ x $ARGV[0];
  }

  use 5.006;
  use Benchmark qw/timethese cmpthese/;
  use Carp;
  sub swrite {
    croak "usage: swrite PICTURE ARGS" unless @_;
    my $format = shift;
    $^A = "";
    formline($format,@_);
    return $^A;
  }

  cmpthese timethese -5, {

      orig => <<'CODE',
  $out = ""; $len = length($str1);
  for ($i = 0; $i < $len; $i += 2) {
    $out .= substr($str1, $i, 2) . 
            substr($str2, $i, 2) . 
            substr($str3, $i, 2);
  }
CODE

    swrite => '$out = swrite("^<^<^<~~",@a=($str1,$str2,$str3))',
    inline => <<'CODE',
  $^A=''; @a = ($str1, $str2, $str3);
  formline("^<^<^<~~",@a);
  $out = $^A;
CODE

  }, 'none';

__END__

% ./try.pl    
          Rate swrite   orig inline
swrite  6350/s     --   -26%   -40%
orig    8574/s    35%     --   -20%
inline 10657/s    68%    24%     --

% ./try.pl 10
         Rate   orig swrite inline
orig    855/s     --   -45%   -55%
swrite 1545/s    81%     --   -18%
inline 1885/s   120%    22%     --

% ./try.pl 100
         Rate   orig swrite inline
orig   92.1/s     --   -53%   -59%
swrite  197/s   114%     --   -12%
inline  225/s   144%    14%     --

These numbers are from a 5.6.1 debugging build, which probably 
penalizes "orig" more than a non-debugging build would.  But 
for sufficiently large strings, the inline/swrite code should 
win out.  I suspect this is also true if the number of "multiplexed" 
strings is increased, but I don't care to write a test for it.


-- 
Joe Schaefer         "Experience is one thing you can't get for nothing."
                                               -- Oscar Wilde



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

Date: Fri, 05 Oct 2001 15:41:33 +0200
From: Jean-Philippe Fauvelle <JPFauvelle@Colt-Telecom.fr>
Subject: Re: Multiplexing strings
Message-Id: <k3errtshfbhlstk94pcg8om81gdu6hjhse@4ax.com>

Le 4 Oct 2001 15:46:21 -0700, mauroid@csi.forth.gr (Dimitri) +AOk-crit:

@STR= ($str1,$str2,$str3);

# number of strings
$N = @STR;

# chars per token
$M = 2;  

# string length
$S = length $STR[0]; 


$out = join '', 
	( join('',@STR) =+AH4- /(.{$M})/gos )[
		map { $_, $_+-$S/$M, $_+-2*$S/$M }  0 .. ($S/$M)-1
	];

one line only ! 

Jean-Philippe Fauvelle

>I have N=3 strings of equal length, and I want to multiplex them by taking
>M=2 characters from each string (the length of each string is divisible by
>M). Example :
>
>Input:
>
>$str1 = "aabbccddeeff";
>$str2 = "AABBCCDDEEFF";
>$str3 = "001122334455";
>
>Output :
>
>$out = "aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55";
>
>Besides the obvious for loop :
>
>$out = ""; $len = length($str1);
>
>for ($i = 0; $i < $len; $i +-= 2) {
> $out .= substr($str1, $i, 2) . substr($str2, $i, 2) . substr($str3, $i, 2);
>}
>
>Is there a more elegant (faster) way?
>
>Thanks!
>-Dimitri



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

Date: Fri, 05 Oct 2001 14:34:30 GMT
From: Dave Tweed <dtweed@acm.org>
Subject: Re: Multiplexing strings
Message-Id: <3BBDC3A6.47A9C124@acm.org>

Boy, am I confused. I'm getting the same results regardless of the length
of the strings! Here's my script, modified to include your inline example
and to support making the strings longer:
=============================================================================
#!perl -w

use Carp;
use Benchmark qw/cmpthese timethese/;

sub swrite {
    croak "usage: swrite PICTURE ARGS" unless @_;
    my $format = shift;
    $^A = "";
    formline($format,@_);
    return $^A;
}

$str1 = "aabbccddeeff";
$str2 = "AABBCCDDEEFF";
$str3 = "001122334455";

$ARGV[0] ||= 0;

for ($str1, $str2, $str3) {
    $_ .= $_ x $ARGV[0];
}

$benchmarks = {
    'substr' => sub {
        $out = ""; $len = length($str1);

        for ($i = 0; $i < $len; $i += 2) {
            $out .= substr($str1, $i, 2) . substr($str2, $i, 2) . substr($str3,
$i, 2);
        }
    },
    'split' => sub {
        @a1 = split /(..)/, $str1;
        @a2 = split /(..)/, $str2;
        @a3 = split /(..)/, $str3;
        $out = "";
        while (@a1) {
            $out .= shift (@a1) . shift (@a2) . shift (@a3);
        }
    },
    'swrite' => sub {
        $out = swrite("^<^<^<~~", $str1, $str2, $str3);
    },
    'inline' => sub {
        $^A = "";
        formline("^<^<^<~~", $str1, $str2, $str3);
        $out = $^A;
    },
};

&{$benchmarks->{'inline'}};
print $out, "\n";

cmpthese (-5, $benchmarks);
=============================================================================

 ... and heres the output:

=============================================================================
~/temp/perl>perl mux.pl
aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55
Benchmark: running inline, split, substr, swrite, each for at least 5 CPU second
s...
    inline:  5 wallclock secs ( 5.29 usr +  0.00 sys =  5.29 CPU) @ 229747.54/s
(n=1214905)
     split:  5 wallclock secs ( 5.41 usr +  0.00 sys =  5.41 CPU) @ 218306.40/s
(n=1180601)
    substr:  4 wallclock secs ( 5.29 usr +  0.00 sys =  5.29 CPU) @ 342611.95/s
(n=1811732)
    swrite:  5 wallclock secs ( 5.18 usr +  0.00 sys =  5.18 CPU) @ 67194.74/s (
n=347800)
           Rate swrite  split inline substr
swrite  67195/s     --   -69%   -71%   -80%
split  218306/s   225%     --    -5%   -36%
inline 229748/s   242%     5%     --   -33%
substr 342612/s   410%    57%    49%     --
~/temp/perl>perl mux.pl 10
aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55aaAA00bb
BB11ccCC22ddDD33eeEE44ffFF55aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55aaAA00bbBB11ccCC
22ddDD33eeEE44ffFF55aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55aaAA00bbBB11ccCC22ddDD33
eeEE44ffFF55aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55aaAA00bbBB11ccCC22ddDD33eeEE44ff
FF55aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55
Benchmark: running inline, split, substr, swrite, each for at least 5 CPU second
s...
    inline:  5 wallclock secs ( 5.26 usr +  0.00 sys =  5.26 CPU) @ 229464.14/s
(n=1206293)
     split:  5 wallclock secs ( 5.24 usr +  0.00 sys =  5.24 CPU) @ 220767.23/s
(n=1156158)
    substr:  6 wallclock secs ( 5.31 usr +  0.00 sys =  5.31 CPU) @ 340368.57/s
(n=1806336)
    swrite:  5 wallclock secs ( 5.15 usr +  0.00 sys =  5.15 CPU) @ 67000.39/s (
n=344918)
           Rate swrite  split inline substr
swrite  67000/s     --   -70%   -71%   -80%
split  220767/s   230%     --    -4%   -35%
inline 229464/s   242%     4%     --   -33%
substr 340369/s   408%    54%    48%     --
~/temp/perl>perl -v

This is perl, v5.6.1 built for cygwin-multi

Copyright 1987-2001, Larry Wall
=============================================================================

What's going on here?

-- Dave Tweed


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

Date: Fri, 05 Oct 2001 16:43:28 +0200
From: Jean-Philippe Fauvelle <JPFauvelle@Colt-Telecom.fr>
Subject: Re: Multiplexing strings
Message-Id: <8ohrrt01618s4jf6hihee08ngt51b9e3gg@4ax.com>

Le 4 Oct 2001 15:46:21 -0700, mauroid@csi.forth.gr (Dimitri) +AOk-crit:

@STR= ($str1,$str2,$str3);

# number of strings
$N = @STR;

# chars per token
$M = 2;  

# string length
$S = length $STR[0]; 


$out = join '', 
	( join('',@STR) =+AH4- /(.{$M})/gos )[
		map { $_, $_+-$S/$M, $_+-2*$S/$M }  0 .. ($S/$M)-1
	];

one line only ! 

Jean-Philippe Fauvelle


>I have N=3 strings of equal length, and I want to multiplex them by taking
>M=2 characters from each string (the length of each string is divisible by
>M). Example :
>
>Input:
>
>$str1 = "aabbccddeeff";
>$str2 = "AABBCCDDEEFF";
>$str3 = "001122334455";
>
>Output :
>
>$out = "aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55";
>
>Besides the obvious for loop :
>
>$out = ""; $len = length($str1);
>
>for ($i = 0; $i < $len; $i +-= 2) {
> $out .= substr($str1, $i, 2) . substr($str2, $i, 2) . substr($str3, $i, 2);
>}
>
>Is there a more elegant (faster) way?
>
>Thanks!
>-Dimitri



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

Date: Fri, 5 Oct 2001 09:57:59 -0500
From: "T.H." <tambaa@no.spam.yahoo.com>
Subject: Re: Need help to suppress STDOUT
Message-Id: <9pkhdf$m7v$1@tilde.csc.ti.com>


"John J. Trammell" <trammell@haqq.hypersloth.invalid> wrote in message
news:slrn9rp42l.dsm.trammell@haqq.hypersloth.net...
> On Wed, 3 Oct 2001 13:23:49 -0500, T.H. <tambaa@no.spam.yahoo.com> wrote:
> > I am calling a an external program using system from within a perl
script. I
> > am trying to redirect (using >) the output to file but the program
output is
> > still being displayed on the screen. I tried closing STDOUT before using
> > system just to see whether that would make a difference but it doesn't.
> >
> > Anyone know how I can completely suppress the output to the screen?
> >
>
> Please show your code and tell what OS you're using.
>
> But something like system("/bin/foo 1>stdout.txt 2>stderr.txt") has
> always worked for me.

Well, I'll be d---. The above worked for me. Thanks.




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

Date: 05 Oct 2001 10:07:21 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Nested loops and scoping $_
Message-Id: <m3eloii3ja.fsf@mumonkan.sunstarsys.com>

Bernie Cosell <bernie@fantasyfarm.com> writes:

> nobull@mail.com wrote:
> 
> } my @z = map { my $q = $_; [ grep { $_ > $q } @x ] } @y;
> } 
> } Of course $q is a copy of $_ so modifying it does not modify the
> } element of @y.
> 
> Yup, and to make *THAT* work, you need to do something like 
> local *q = \$_ or some such... yuk!
> 
> This is, alas, one of the failings of using shallow binding over 
> real dynamic binding...
  ^^^^^^^^^^^^^^^^^^^^

Could you explain this? AFAICT the reason that "local *q = \$_;"
would actually work is *because* of "shallow binding", not despite it.
(i.e. "local *q = *_;" will *not* work.)

-- 
Joe Schaefer                     "I'll sleep when I'm dead."
                                               -- Warren Zevon



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

Date: Fri, 05 Oct 2001 14:06:38 +0100
From: Derek Fountain <nomail@hursley.ibm.com>
Subject: Rewinding __DATA__ ?
Message-Id: <9pkb9r$1r28$1@sp15at20.hursley.ibm.com>

I have a bit of data at the end of my script, after a __DATA__ marker. I 
want to read this several times in a loop. After I've read it once, is it 
possible to "rewind" it so the next time I do <DATA> I get the first line 
of data again?

---
Derek


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

Date: 05 Oct 2001 09:48:28 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Rewinding __DATA__ ?
Message-Id: <m3itdui4er.fsf@mumonkan.sunstarsys.com>

Derek Fountain <nomail@hursley.ibm.com> writes:

> I have a bit of data at the end of my script, after a __DATA__ marker. I 
> want to read this several times in a loop. After I've read it once, is it 
> possible to "rewind" it so the next time I do <DATA> I get the first line 
> of data again?

  % perldoc -f tell
  % perldoc -f seek

-- 
Joe Schaefer       "The man who sets out to carry a cat by its tail learns
                something that will always be useful and which never will grow
                                      dim or doubtful."
                                               --Mark Twain



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

Date: Fri, 05 Oct 2001 08:51:55 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Rewinding __DATA__ ?
Message-Id: <3BBDBAFB.40E284D@home.com>

Derek Fountain wrote:
> 
> I have a bit of data at the end of my script, after a __DATA__ 
> marker. want to read this several times in a loop. After I've read
> it once, is it possible to "rewind" it so the next time I do <DATA>
> I get the first line of data again?

Yes. You can use the seek() function to set the position of a
filehandle. Typically you would say

    seek(FH, 0, 0);

to reset to beginning of the file. __DATA__ is special, though. If you
want to be able to reset it, you must use tell() before you start
reading so that you know where to reset to:

    my $data_start = tell(DATA);
    while (<DATA>) {
        # do something useful
    }

    seek(DATA, $data_start, 0);
    while (<DATA>) {
        # ...
    }

-mjc


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

Date: Fri, 05 Oct 2001 14:22:43 +0100
From: Jasper McCrea <jasper@guideguide.com>
Subject: Re: Skipping following lines if the same
Message-Id: <3BBDB423.CFC98433@guideguide.com>

Laird wrote:
> 
> Hi everyone,
> 
> I've got this array
> 
> aaa
> aaa
> bbbbb
> cccc
> cccc
> c
> ddd
> ddd
> fff
> fff
> fff
> ffff
> ffff
> 
> and would like to transform it into this
> 
> aaa
> bbbbb
> cccc
> c
> ddd
> fff
> ffff
> 
> skipping following lines if the same.
> But only following lines.
> 
> I tried this but it doesn't work.
> 
> #!/usr/bin/perl
>      open (IN,data);
>      @a=<IN>;
>      for (@a){
>      $i++;
>      chomp $a[$i];
>      print "$a[$i]\n" if /$a[$i]/ ne /$a[$i+1]/
>      }
> 
> Cordially
> Ken

you could use:

my $last = '';
@a = map { $last eq $_ ? () : ($last = $_) } @a;

unfortunately, if you have umpteen empty elements at the start, these
will be included (not much of a problem if reading from a file). And
it's only useful if you've slurped the whole file first.

Jasper
-- 
      split//,'019617511192'.
      '17011111610114101114'.
      '21011141011840799901'.
            '17101174';
            foreach(0..         # my
            $#_){$_[$_          # signature is too
            ++]^=$_[$_          # bignature
            --]^=$_[$_
]^=$_[++    $_]if!($_%
2)}$g.=$_  ,chr($g)=~
 /(\w)/&&($o.=$1and
   $g='')foreach@_;
      print"$o\n"


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

Date: Fri, 5 Oct 2001 09:46:54 -0500
From: "Mr. Sunblade" <djberge@qwest.com>
Subject: warnings and $a, $b
Message-Id: <uFjv7.33$oK3.64687@news.uswest.net>

Hi all,

I recently wrote and published an OO module that has it's own "sort" method.
I've recently been toying with the sort method to make it more flexible,
allowing coderefs to be passed in.

It would look something like this:

#/usr/local/bin/perl -w
use strict;
my $obj = Set::Array->new(qw(1 4 2));
$obj->sort(sub{$a <=> $b});

However, running this will yield the warnings:

"Name "main::a" used only once: possible typo at ./arrayTest.pl line 4.
Name "main::b" used only once: possible typo at ./arrayTest.pl line 4."

Now consider this script:
#!/usr/local/bin/perl -w
use strict;
my @list = qw(1 5 3 6 2);
my @sorted = sort{ $a <=> $b } @list;
print "Sorted: ", join(',',@sorted), "\n";

This yields no warnings.

I have a couple questions on this:

First, if $a and $b are recognized as special variables and don't generate
the usual warnings, why am I getting warnings in my version?

Second, if I'm getting warnings in my version because I redefined 'sort()',
why am I only getting the 'once' warning and not the 'uninitialized' warning
or the fatal 'explicit package name required' error message?

Thanks in advance.

Mr. Sunblade.

PS - Yes, I know I can ignore this with -- no warnings 'once' -- I was just
hoping to avoid it altogether








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

Date: Fri, 5 Oct 2001 10:03:26 -0500
From: "Mr. Sunblade" <djberge@qwest.com>
Subject: Re: warnings and $a, $b
Message-Id: <ZUjv7.36$oK3.70436@news.uswest.net>

FYI - perl 5.6.1 on Solaris 8




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

Date: 05 Oct 2001 13:08:57 GMT
From: vek@pharmnl.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: Yet another fork question
Message-Id: <slrn9rrc76.jeu.vek@pharmnl.ohout.pharmapartners.nl>

On 5 Oct 2001 11:40:17 GMT,
    Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:


>
>MJD has recently shown code where each kid runs its own accept loop
>and the parent does nothing but wait(pid) and more forks.  I like
>that solution a lot (if applicable), and it has me wondering why
>practically all standard examples in the Perl literature place the
>accept call in the parent.
>


This might lead to the "thundering herd problem", which, depending
on your OS, you may or may not have.  When a connection comes in
all children waiting in the accept is woken up, just to be put back
to sleep when the connection was taken by one of the quicker children.

Apache avoids this by having some kind of semaphor so at most one
of the children gets to sleep on the accept, and the other is sleeping
waiting for the semaphor.  As soon as one connection is accepted the
semaphor is released for the next child process.



Villy


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

Date: 5 Oct 2001 13:31:39 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Yet another fork question
Message-Id: <9pkcnr$g1t$1@mamenchi.zrz.TU-Berlin.DE>

According to Villy Kruse <vek@pharmnl.ohout.pharmapartners.nl>:
> On 5 Oct 2001 11:40:17 GMT,
>     Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> 
> 
> >
> >MJD has recently shown code where each kid runs its own accept loop
> >and the parent does nothing but wait(pid) and more forks.  I like
> >that solution a lot (if applicable), and it has me wondering why
> >practically all standard examples in the Perl literature place the
> >accept call in the parent.
> >
> 
> 
> This might lead to the "thundering herd problem", which, depending
> on your OS, you may or may not have.  When a connection comes in
> all children waiting in the accept is woken up, just to be put back
> to sleep when the connection was taken by one of the quicker children.

That makes sense.  There had to be a disadvantage in letting many
processes accept connections on a single socket.
 
> Apache avoids this by having some kind of semaphor so at most one
> of the children gets to sleep on the accept, and the other is sleeping
> waiting for the semaphor.  As soon as one connection is accepted the
> semaphor is released for the next child process.

 ...so the idle children in effect queue up at the socket.  Makes
sense again, and a semaphore is perfectly suited for the purpose.

Anno


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

Date: 05 Oct 2001 07:31:52 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Yet another fork question
Message-Id: <m11ykitaxz.fsf@halfdome.holdit.com>

>>>>> "Anno" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:

Anno> That makes sense.  There had to be a disadvantage in letting many
Anno> processes accept connections on a single socket.

And it's illegal on at least one place I was testing it, when
I was building the code for:

  <http://www.stonehenge.com/merlyn/WebTechniques/col34.html>

I don't recall where now, but I think it was sunos4. Linux handled it
just fine.
 
>> Apache avoids this by having some kind of semaphor so at most one
>> of the children gets to sleep on the accept, and the other is sleeping
>> waiting for the semaphor.  As soon as one connection is accepted the
>> semaphor is released for the next child process.

Anno> ...so the idle children in effect queue up at the socket.  Makes
Anno> sense again, and a semaphore is perfectly suited for the purpose.

And of course I needed a filehandle to flock that all the kids shared.
What better one to flock than the socket itself!  Worked great.

print "Just another Perl hacker,"

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: 5 Oct 2001 14:54:50 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Yet another fork question
Message-Id: <9pkhjq$lf7$1@mamenchi.zrz.TU-Berlin.DE>

According to Randal L. Schwartz <merlyn@stonehenge.com>:
> >>>>> "Anno" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
> 
> Anno> That makes sense.  There had to be a disadvantage in letting many
> Anno> processes accept connections on a single socket.
> 
> And it's illegal on at least one place I was testing it, when
> I was building the code for:
> 
>   <http://www.stonehenge.com/merlyn/WebTechniques/col34.html>
> 
> I don't recall where now, but I think it was sunos4. Linux handled it
> just fine.
>  
> >> Apache avoids this by having some kind of semaphor so at most one
> >> of the children gets to sleep on the accept, and the other is sleeping
> >> waiting for the semaphor.  As soon as one connection is accepted the
> >> semaphor is released for the next child process.
> 
> Anno> ...so the idle children in effect queue up at the socket.  Makes
> Anno> sense again, and a semaphore is perfectly suited for the purpose.
> 
> And of course I needed a filehandle to flock that all the kids shared.
> What better one to flock than the socket itself!  Worked great.

Makes sense one more time.  After all, it *is* the socket we need
exclusive access to, just not (as usual) for writing but for accept-ing.
So just frame the accept() calls in the kids' loop with requests and
releases of an exclusive lock, and all is well (as far as concurrent
accept() goes).

This thread makes an inordinate amount of sense :)

Anno

PS: Yikes.  For the first time I noticed what my spelling checker
    has to say about "Randal":

    0: Randall
    1: Rand Al
    2: Rand-Al
    3: Sandal
    4: Vandal


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

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


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