Raymond Wan wrote:

Hi Richard,


Richard wrote:
while going over below example from the book, I am not understanding why/how below program works.
Can someone explain this to me in better way?


Have you tried running it?  does it work like what you expect?


what is READER exactly reading from???
and what does print while <READER> do ? I thought all the print happens during factorial() and fibonacci()


It is reading from a pipe. Take a look at the description of the pipe function and what a pipe is (in case you do not know):

http://perldoc.perl.org/functions/pipe.html
http://en.wikipedia.org/wiki/Pipe_(Unix)

<> is reading in from the pipe.  Take a look at:

http://perldoc.perl.org/perlop.html#I%2fO-Operators

So, while there is something left to read, print it out. Go to the above link and look at the first shaded box which says "The following lines are equivalent". The line that is confusing you is the last line in that box...as it says, all of those are equivalent.

to answer your last question, then, the answer is "no", the printing isn't happening in just the two functions you named. Take a look at the above links to see if they give you a clue. And if you are wondering what the select line is doing, take a look at this and look up the select function:

http://perldoc.perl.org/perlfaq5.html#How-do-I-flush%2funbuffer-an-output-filehandle%3f--Why-must-I-do-this%3f

And BTW, you can cut out almost half of the code if you just focus on either fibonacci or factorial and not both.

Ray



ok so here is my attempt to understand this,

serverX  ~/.sc/net> ./pipe.pl 2
hi2
fibonacci(1) => 1
hi2
fibonacci(2) => 1
done with fibonacci here
hi
factorial(1) => 1
hi
factorial(2) => 2
done with factorial
truly done

serverX ~/.sc/net> ./pipe.pl 10
hi
factorial(1) => 1
hi
factorial(2) => 2
hi
factorial(3) => 6
hi
factorial(4) => 24
hi
factorial(5) => 120
hi
factorial(6) => 720
hi
factorial(7) => 5040
hi
factorial(8) => 40320
hi
factorial(9) => 362880
hi
factorial(10) => 3628800
done with factorial
hi2
fibonacci(1) => 1
hi2
fibonacci(2) => 1
hi2
fibonacci(3) => 2
hi2
fibonacci(4) => 3
hi2
fibonacci(5) => 5
hi2
fibonacci(6) => 8
hi2
fibonacci(7) => 13
hi2
fibonacci(8) => 21
hi2
fibonacci(9) => 34
hi2
fibonacci(10) => 55
done with fibonacci here
truly done

still confused by after running this program.

1)when I do  #print while <READER>; program only outputs

  --> truly done

2)here is my attempt for understanding of this program.

"The parent process keeps one filehandle and closes the other, while the child does the opposite. The parent and child process can now communicate acorss the pipe as they work in parallel"

because above statement from the book network programming with perl... my thought is a)child choose to write to filehandle WRITER and have all the output from factorial and fibonacci go there b)parent choose to close WRITER and read from READER because that's what it will be able to read since READER will have
     all the output from child's filehandle WRITER

am I interpreting this right way? or close?

my $arg = shift || '10';

pipe(READER,WRITER) or die "Can't open pipe: $!\n";
### pipe(READHANDLE, WRITEHANDLE) ---> first is the name of a filehandle to read from ### 2nd is filehandle to write to. IF successful, pipe() returns a true result code

if ( fork == '0' ) {
    close READER;
    select WRITER; $| = '1';
    factorial($arg);
print "done with factorial\n";
    exit 0;
}

if ( fork == '0' ) {
    close READER;
    select WRITER; $| = '1';
    my $result = fibonacci($arg);
print "done with fibonacci here\n";
    exit 0;
}

#parent process closes WRITER and reads from READER
close WRITER;
print while <READER>;
print "truly done\n";


sub factorial {
  my $target = shift;
  for ( my $result = 1, my $i = 1; $i <= $target; $i++ ) {
print "hi\n";
       print "factorial($i) => ", $result *= $i, "\n";
  }
}

sub fibonacci {
  my $target = shift;
  my ($a,$b) = (1,0);
  for ( my $i = 1; $i <= $target; $i++ ) {
     my $c = $a + $b;
     print "hi2\n";
     print "fibonacci($i) => $c\n";
     ($a,$b) = ($b,$c);
  }
}



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to