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

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
COMP234 Perl Printing Special Quotes File Handling.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
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.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Linux+ Guide to Linux Certification, Second Edition
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Guide To UNIX Using Linux Third Edition
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
BIF703 stdin, stdout, stderr Redirection. stdin, stdout, stderr Recall the Unix philosophy “do one thing well”. Unix has over one thousand commands (utilities)
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Standard Input and Output. Overview Data communication with a C program and the outside world is performed through files Files are a non-volatile way.
Linux+ Guide to Linux Certification, Third Edition
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Introduction to Unix – CS 21
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Linux+ Guide to Linux Certification, Second Edition
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
Linux Administration Working with the BASH Shell.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
Lecture 3: Getting Started & Input / Output (I/O)
C Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Formatting Output.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 9 Shell Programming – Command substitution
Variables, Expressions, and IO
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
The Linux Operating System
Formatting Output.
stdin, stdout, stderr Redirection
CSC215 Lecture Input and Output.
CSE 303 Concepts and Tools for Software Development
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
INPUT & OUTPUT scanf & printf.
Number and String Operations
Context.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Handling.
Perl I/O Learning Objectives:
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Topics Designing a Program Input, Processing, and Output
More advanced BASH usage
Programming in C Input / Output.
Conversion Check your class notes and given examples at class.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
CST8177 Scripting 2: What?.
Topics Introduction to File Input and Output
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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 $_.

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

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, an array that Perl creates for you. All words on the command line after the programs’ name are automatically 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”;

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 (0 .. 10) { print “$i ”; if ($i % 3 == 0) { warn “multiple of 3: $i\n”; } }

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.

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.3f”, 66.6666666; gives 66.667. 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

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.