Perl IO - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Perl IO

Description:

Each element is one line, and includes the terminating newline (the last line ... print 1 1, 'cheap Billn'; # also ok. Lecture 15 / Slide 10. printf Formatted Output ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 12
Provided by: andrew184
Category:
Tags: bill | one | perl

less

Transcript and Presenter's Notes

Title: Perl IO


1
Software Tools
  • Perl I/O

2
Input from STDIN
  • Reading from STDIN is easy, and we have done it
    many times.
  • a ltSTDINgt
  • In a scalar context, this gives the next line of
    input, or undef if there are are no more lines
    (the user has hit CTRL-d).
  • In a list context, this gives all remaining lines
    (all the lines until the user hits CTRL-d).
  • _at_a ltSTDINgt
  • Each element is one line, and includes the
    terminating newline (the last line may or may not
    have a newline).

3
Input from STDIN
  • Typically, a program will read in one line at a
    time and process the line
  • while(defined(line ltSTDINgt))
  • process line
  • print "line"
  • In the above example, defined() returns true
    unless its argument is undef.
  • As long as a line has been read, ltSTDINgt returns
    a defined value, and the loop continues.
  • When ltSTDINgt has no more lines to read (it hits
    CTRL-d), it returns undef, terminating the loop.

4
Input from STDIN
  • Here is an example of program input and output.
  • White text is typed by the user, green text is
    printed by the program or shell.
  • cat line1
  • !/usr/local/bin/perl5 -w
  • while(defined(line ltSTDINgt))
  • process line
  • print "line"
  • line1
  • hi
  • hi
  • test
  • test
  • CTRL-d

5
Input from STDIN
  • We can also read and print the lines altogether.
  • Again, white text is typed by the user, green
    text is printed by the program or shell.
  • cat line2
  • !/usr/local/bin/perl5 -w
  • _at_lines ltSTDINgt
  • process lines
  • print "_at_lines"
  • line3
  • hi
  • test
  • CTRL-d hi
  • test

6
Input from STDIN
  • Perl has a shortcut for reading a value from
    ltSTDINgt into _.
  • Whenever a condition consists solely of ltSTDINgt,
    Perl automatically copies the line into _.
  • while(ltSTDINgt)
  • same as while(defined(_ ltSTDINgt))
  • chomp same as chomp "_"
  • print "_\n"
  • _ is the default for many operations, such as
    chomp and print.

7
Input from ltgt
  • Another way to read input is with ltgt.
  • ltgt returns a single line in a scalar context, or
    all the remaining lines in a list context.
  • However, unlike ltSTDINgt, ltgt gets its data from
    the file (or files) specified on the command
    line.
  • cat f1
  • 1
  • cat f2
  • 2
  • cat mycat
  • !/usr/local/bin/perl5 -w
  • while(ltgt)
  • print
  • mycat f1 f2
  • 1
  • 2

8
Input from ltgt
  • If you do not specify any filenames on the
    command line, ltgt reads from standard input
    automatically.
  • cat mycat
  • !/usr/local/bin/perl5 -w
  • while(ltgt)
  • print
  • mycat
  • hi
  • hi
  • test
  • test
  • CTRL-d
  • ltgt is the easiest way to read files in Perl (we
    will learn more advanced ways later).

9
Output to STDOUT
  • We have also often used print.
  • The print function takes a list of strings, and
    sends each to STDOUT, without adding any
    characters between them.
  • print "Hi Bill!\n"
  • print "Hi ", "Bill!", "\n"
  • print("Hi ", "Bill!", "\n")
  • Sometimes, you will need to add parentheses to
    print, especially when the first thing starts
    with a left parenthesis
  • print (11), "cheap Bill\n" problem!
  • print ((11), "cheap Bill\n") ok
  • print 11, "cheap Bill\n" also ok

10
printf Formatted Output
  • You may wish to have more control over your
    output than print provides.
  • The printf function takes a list of arguments,
    where the first argument is a format control
    string, which defines how to print the remaining
    arguments (like printf in C).
  • printf "s d f\n", s, n, real
  • Format types
  • strings s
  • integer numbers d
  • floating-point numbers f

11
printf Formatted Output
  • cat printf1
  • !/usr/local/bin/perl5 -w
  • s "1234567890"
  • n 12345
  • real 1234567.123
  • printf "15s 5d 10.2f\n", s, n, real
  • printf1
  • 1234567890 12345 1234567.12
  • This example prints s in a 15-character field,
    then space, then n as a decimal integer in a
    5-character field, then another space, then real
    as a floating-point value with 2 decimal places
    in a 10-character field, and finally a newline.
Write a Comment
User Comments (0)
About PowerShow.com