#!/usr/bin/perl -w ####################################################### # class2_02_undef # # demo how using a value not yet set --- and so containing # the special Perl value undef --- isn't necessarily an # error # # modified by Sharon Tuttle from "Learning Perl", # by Schartz and Phoenix # # last modified: 4-10-03 ####################################################### $ct = 1; # computing (supposedly) the sum and product of the integers 1-10 while ($ct <= 10) { $sum += $ct; # no error --- initially undef, but treated as zero $prod *= $ct; # OK, not so good --- initially undef, and treated # as 0, but that means it stays 0 (multiplying by 0 # and all...!) (note the warning you get, too...) $ct++; } print("\$ct is: $ct\n"); print("\$sum is: $sum\n"); print("\$prod is: $prod\n"); # end of class2_02_undef