while (<>) {
chomp; # This works like the loop above, but with less typing
print "It was $_ that I saw!\n";
}
(The Invocation Arguments)
@ARGV is already stuffed full of the list of invocation arguments
@ARGV = qw# larry moe curly #; # force these three files to be read
while (<>) {
chomp;
print "It was $_ that I saw in some stooge-like file!\n";
}
3>: Output to Standard Output
@array=qw/ fred barney betty /;
print @array; # fredbarneybetty
print "@array"; # fred barney betty
$result = print("hello world!\n"); # result get 1,indicating the success of the print
print (2+3)*4; # print 5
(print (2+3))*4; # Oops!
4>: Formatted Output with printf
printf "Hello, %s; your password expires in %d days!\n", $user, $days_to_die;
%d:means a decimal integer
%f:means a floating-point
%g:(General) automatically chooses floating-point,integer,or even exponential notation
%s:means a string
%%:to print a real percent sign
printf "%g %g %g\n", 5/2, 51/17, 51 ** 17;
printf "in %d days!\n", 17.85; # in 17 days (Note that this is truncated, not rounded)
printf "m\n", 42; # output like ````42 (the ` symbol stands for a space)
printf "-\n", 2e3 + 1.95; # 2001
printf "s\n", "wilma"; # (right-justified) looks like `````wilma
printf "%-15s\n", "flintstone"; # (left-justified) looks like flintstone`````
printf "f\n", 6 * 7 + 2/3; # looks like ```42.666667
printf ".3f\n", 6 * 7 + 2/3; # looks like ``````42.667
printf ".0f\n", 6 * 7 + 2/3; # looks like ``````````43
printf "Monthly interest rate: %.2f%%\n", 5.25/12; # the value looks like "0.44%"
5>: Arrays and printf
my @items = qw( wilma dino pebbles );
printf "The items are: \n" . ("s\n" x @items), @items;
(Note that here we have @items being used once in a scalar context, to get its length, and
once in a list context, to get its contents. Context is important.)
6>: Filehandle
(the recommendation from Larry is that you
use all uppercase letters in the name of your filehandle)
STDIN, STDOUT, STDERR, DATA, ARGV, and ARGVOUT