Learning Perl

Folks! Anyone please explain the behavior of this program step by step. Thanks.

#! /usr/bin/perl
$testfile = "./testfile2";
for ( $i = 1, $i <= 5, $i++) {
  open ($FILE, ">", $testfile);
  print ($FILE "Output 1 \n");
  close ($FILE);
}
print "The value of (4 * 2) / 2 is ";
print (4 * 2) / 2;
print ("\Output 2 \n");

This program opens file named "./testfile2" for writing.
five times it writes: "Output 1 \n" to this file. Note that it doesn't append to it because of this: ">" it wipes all data and then writes to it. if you want to append to it use: ">>".
The last three lines are trivial.

1 Like