I am having trouble getting iteration to work over a tied array. The array in question is tied via some methods to provide the pids of all child proccesses of the current proccess. The Nanny package implements the array by parsing output from the ps command.So this isn't very portable, yet I have tried it and it works on solaris 2.6+ and redhat 7 (except iteration which just doesn't work). Example code and the package that implements the tie follows. Anybody know what I am doing wrong? Thanks---
#!/usr/bin/perl #Package Nanny provides methods to tie an array. #The methods fill a tied array with the pids of the #non zombie children of the current proccess in real time. #I can't seem to get iteration to work. #example code: Make a few eternally looping kids, get pids. tie my @kids, 'Nanny'; if($pid = fork) { push @pids,$pid; if($pid = fork) { push @pids,$pid; if($pid = fork) { push @pids,$pid; } else{while(1){}} } else{while(1){}} } else{ exit; } if ($pid){ print "supposed number:". @pids. " real time number:".scalar @kids + ."pids @kids"; } #this does not work # for(1..@kids){ # waitpid($kids[$_],0); # } #and this doesn't either # foreach(@kids){ # waitpid($_,0); # } # #and not this either #foreach(@kids){ # print $_; # } #Nanny package: package Nanny; use Tie::Array; use strict; our @ISA = ('Tie::StdArray'); sub TIEARRAY{ my ($self) = shift; my @real_pids; bless \@real_pids, $self; return \@real_pids; } sub FETCHSIZE { my ($self) = shift; my $masterpid = $$; my @real_pids; open(PS,"ps -e -o ppid -o pid -o tty -o comm | "); my $l; while(<PS>){ $l++; next if $l == 1; my @procs = split(' ',$_); next if (defined($procs[3]) && $procs[3] eq 'ps'); if ($procs[0] == $masterpid && $procs[2] =~ /\?/) { waitpid($procs[1],0); next; } if ($masterpid == $procs[0]){ push @real_pids,$procs[1]; } } close PS; return scalar @real_pids ; } sub FETCH { my ($self,$idx) = shift; unless(defined $idx){ $idx = 0; } my $masterpid = $$; my @real_pids; open(PS,"ps -e -o ppid -o pid -o tty -o comm | "); my $l; while(<PS>){ $l++; next if $l == 1; my @procs = split(' ',$_); next if (defined($procs[3]) && $procs[3] eq 'ps'); if ($procs[0] == $masterpid && $procs[2] =~ /\?/) { waitpid($procs[1],0); next; } if ($masterpid == $procs[0]){ push @real_pids,$procs[1]; } } close PS; return $real_pids[$idx]; }

In reply to Iteration problem on a tied array by tommyboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.