Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input from STDIN STDIN, standard input, comes from the keyboard.

Similar presentations


Presentation on theme: "Input from STDIN STDIN, standard input, comes from the keyboard."— Presentation transcript:

1 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: prompt $ print_input.pl < input_file.txt the program “print_input.pl” 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 $_.

2 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.

3 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 It is good practice to use ALL CAPS for file handle names: it makes them easy to see. You can also test to see whether the file you want actually exists using a statement like: open INFILE, “my_file.txt” or die “Couldn‘t open file\n”; The “die” statement is only executed if the program can’t open myfile.txt die print out the error message and kills the program.

4 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”;

5 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 “<>”, an array that Perl creates for you. All words on the command line after the programs’ name are automatically put which can then be used just like any other array: foreach my $arg { 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 in a scalar context: print “The number of arguments is: “, “\n”;

6 Output to STDOUT and STDERR
Output from print statements goes into a channel called STDOUT Output from error messages goes into a channel called STDERR By default, both STDERR and STDOUT print to the screen. However, STDOUT output: is buffered: it only prints when the print buffer is filled up. In contrast, STDERR output prints immediately. Thus, print statements mixed with error messages don’t necessarily print in the order they appear in the program. Try this code: foreach my $i ( ) { print “$i ”; if ($i % 3 == 0) { warn “multiple of 3: $i\n”; } }

7 More STDERR and STDOUT It is possible to separate STDERR and STDOUT, sending one or both of them to separate files, so you can read them easily. Recall the Linux file re-direction symbol “>”: my_program.pl > output_file.txt redirects STDOUT (but not STDERR) to the file output_file.txt Also: my_program.pl >> output_file.txt appends STDOUT (but not STDERR) to the file output_file.txt (instead of over-writing it, as “>” does) Both STDOUT and STDERR are re-directed with “&>” my_program.pl &> output_file.txt redirects both STDOUT and STDERR to the file output_file.txt STDOUT (but not STDERR) can also be re-directed to a file using the knowledge that STDOUT is assigned file descriptor my_program.pl 1> output_file.txt” has the same effect as my_program.pl > output_file.txt STDERR (but not STDOUT) can be re-directed with “2>”. STDERR has file descriptor 2. my_program.pl 2> output_file.txt” re-directs the warnings and error messages (STDERR) to the file output_file.txt. STDOUT (print statements) print to the screen.

8 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”, ; 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.

9 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.3f”, ; gives That is, a total of 6 spaces (including the decimal point), with 3 after the decimal point. printf (and its cousin sprintf) is how numbers are rounded; note the “7” at the end. “%f” rounds the numbers instead of truncating them as “%d” does. sprintf prints to a variable, not to output. $var = sprint “%5.3”, 10/6; print “$var\n”; 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

10 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.2f%%”, 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.2f%%”, 50; puts “Half means 50.00%” into the variable $str.


Download ppt "Input from STDIN STDIN, standard input, comes from the keyboard."

Similar presentations


Ads by Google