80 likes | 215 Views
Input from STDIN. STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if the command line (following the $ prompt symbol) is: t80maj1 $ print_input < input_file.txt
E N D
Input from STDIN • STDIN, standard input, comes from the keyboard. • STDIN can also be used with file re-direction from the command line. For instance, if the command line (following the $ prompt symbol) is: t80maj1 $ print_input < input_file.txt the program “print_input” will take each line from the file “input_file.txt” and process in a loop such as: while (<STDIN>) { print; } or @lines = <STDIN>; • A special note: the while (<STDIN>) construction assigns the output of STDIN to $_, exactly the same as while ($_ = <STDIN>). But, you can’t put anything else in the conditional clause (such as “chomp”, or a test condition, and still get <STDIN> assigned to $_.
Input from Files • There are 2 ways of using a file as input instead of using the keyboard. • The simplest way mimics the behavior of standard Unix commands like “ls” and “cat”. On the command line, the first word in the Perl program to run. All words following this are treated as the names of files to be opened and processed by the Perl program: t80maj1 $ print_files.pl file1.txt file2.txt This syntax can be used to open and process each of these files. • To get this to work, your Perl program needs to use the “diamond operator” <>: while (<>) { print; } • If there is no filename on the command line after the program’s name, the program processes keyboard input instead. Also if a hyphen is used instead of a file name.
File Handles • The best way to open a file whose name isn’t going to change each time you run the program is to open it from within the program. • The “open” command assigns a file name to a file handle, which is then used like STDIN is: open INFILE, “my_file.txt”; while (<INFILE>) { print; } • Files are closed automatically when the program terminates, but sometimes you need to specifically close them: close INFILE; # or whatever the file handle’s name is
Writing and Appending to Files • Files can also be opened for writing or appending. • Writing to a file means either creating it or opening a pre-existing file and destroying all of its contents. To write to a file, use a “>” before the file’s name in the “open” command: open OUTFILE, “>my_file.txt”; • To actually write to this file, use the file handle’s name with “print”: print OUTFILE “something interesting\n”; • Appending to a file means creating a new file if it doesn’t exist, or adding new contents to the end of a pre-existing file. Appending is done by using “>>” in front of the file’s name: open APPENDFILE, “>>my_file.txt”;
Command Line Arguments • You can pass arguments into a Perl program with the command line. On the command line, this looks just like the way we used the diamond operator: t80maj1 $ print_files.pl file1.txt file2.txt The difference is internal: instead of using “<>”, use @ARGV. All words on the command line after the programs’ name are put into @ARGV, which can then be used just like any other array: foreach my $arg (@ARGV) { print “$arg\n”; } • Note that there is no equivalent to C’s “argc” variable, which gives the number of arguments. To get this number, simply use @ARGV in a scalar context: print “The number of arguments is: “, scalar @ARGV, “\n”;
Formatted Printing • C uses the “printf” command to produce almost all of its output. C++ uses “cout” instead. • Occasionally it is necessary to format Perl output. For instance, this is the easiest way to round off numbers to a specified number of decimal places, or to put numbers into neat columns. • The syntax for “printf” uses a format code that starts with %, in quotes, followed by one or more things to print: printf “%g”, 54; This would simply print “54”. • The basic format codes: -- “%d” prints integers. Non-integral values are truncated (not rounded): printf “%d”, 99.999; gives “99”. -- “%g” prints in the “general format. Perl automatically chooses between integer, decimal, and exponential formats: printf “%g”, 5/2, 6/2, 6 * 10**23; # 2.5, 3, 6.0e+23 --”%f” prints in floating point format, with a decimal point --”%s” means string format.
More printf • The number of columns used to print, as well as justification, can be selected: printf “%6d”, 12; prints the number 12 right-justified with a total of 6 spaces (the leftmost 4 will be blanks). • With “%f”, the number of positions after the decimal point can be specified: printf “%6.3”, 66.6666666; gives 66.667. That is, a total of 6 spaces (including the decimal point), with 3 after the decimal point. • This is how numbers are rounded; note the “7” at the end. “%f” rounds the numbers instead of truncating them as “%d” does. • Left justification can be done using negative numbers in the format code: printf “%6d”, 12; # 4 blank spaces followed by 12 printf “%-6d, 12; # 12 followed by 4 blank spaces
Still More printf • You can include other text inside the quotes with the format code: printf “The cat has %d lives\n”, 9; prints “The cat has 9 lives” • To get an actual percent sign, use “%%” printf “Half means %5.3f%%”, 50; prints “Half means 50.00%” • To print to a string instead of to output, use “sprintf” with the format codes: my $str = sprintf “Half means %5.3f%%”, 50; puts “Half means 50.00%” into the variable $str.