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?

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

use warnings;
use strict;

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);
    exit 0;
}

if ( fork == '0' ) {
    close READER;
    select WRITER; $| = '1';
    my $result = fibonacci($arg);
    exit 0;
}

#parent process closes WRITER and reads from READER
close WRITER;
print while <READER>;

sub factorial {
  my $target = shift;
  for ( my $result = 1, my $i = 1; $i <= $target; $i++ ) {
       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 "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