_learning perl_, ch6, 2.I.2002, nh ------------- | BASIC I/O | ------------- * input to = ----------------- scalar: everything up to newline (or whatever $/ is) = value $a = ; list: each thing up to newline (or whatever $/ is) makes seperate elements of list `-> if you want to do sth to each element as you go: while (defined($line = )) { #process $line here -- i assume also stick in list? } `-> this is common enough that you can just use implied $_: * while () { # means, * # while(defined($_ = )) * chomp; # like chomp($_); * # other expressions here with $_ * } * input from the Diamond Operator: <> ------------------------------- #!/usr/bin/perl # program called "kitty" while (<>){ # when invoked orange:~/perl>kitty file1 file2, print $_; # kitty reads all lthe lines of file1, then } # file2, printing them out -- just like 'cat' <> is technically reading lines into the special '@ARGV' array `-> so, if you want, you can set @ARGV within your program, and tell <> to work on that new list rather than command-line arguments: @ARGV = ("aaa","bbb","ccc"); while(<>) { # processes aaa, then bbb, then ccc print "this line is: $_"; } * output to STDOUT I/O -------------------- print -- has a true/false value, if it succeeds or not ===== sometimes takes perens -- like, print ((2+3), "hello"); > print 1+2+4; # Prints 7. > print(1+2) + 4; # Prints 3. > print (1+2)+4; # Also prints 3! man > print +(1+2)+4; # Prints 7. perl-> print ((1+2)+4); # Prints 7. func > > If you run Perl with the -w switch it can warn you about > this. For example, the third line above produces: > > print (...) interpreted as function at - line 1. > Useless use of integer addition in void context at - line 1. printf -- print FORMATTED ====== like C's printf operation man printf f > Perl's sprintf() permits the following > universally-known conversions: > > %% a percent sign > %c a character with the given number man > %s a string perl-> %d a signed integer, in decimal func > %u an unsigned integer, in decimal > %o an unsigned integer, in octal > %x an unsigned integer, in hexadecimal > %e a floating-point number, in scientific notation > %f a floating-point number, in fixed decimal notation > %g a floating-point number, in %e or %f notation numbers format it: printf("%4d %6g", $a $b); # 4-digit integer, 6-digit float