Skip to Content
Learning Perl, Fourth Edition
book

Learning Perl, Fourth Edition

by Randal L. Schwartz, Tom Phoenix, brian d foy
July 2005
Beginner
312 pages
9h 23m
English
O'Reilly Media, Inc.
Content preview from Learning Perl, Fourth Edition

Chapter 5. Input and Output

We’ve seen how to do some input/output (I/O) to make some of the earlier exercises possible. Now we’ll learn more about those operations by covering the 80% of the I/O you’ll need for most programs. If you’re familiar with the workings of standard input , output, and error streams, you’re ahead of the game. If not, we’ll get you caught up by the end of this chapter. For now, think of “standard input” as being “the keyboard” and “standard output " as being “the display screen.”

Input from Standard Input

Reading from the standard input stream is easy. We’ve been doing it with the <STDIN> operator.[113] Evaluating this operator in a scalar context gives you the next line of input:

    $line = <STDIN>;                # read the next line
    chomp($line);                   # and chomp it

    chomp($line = <STDIN>);         # same thing, more idiomatically

Since the line-input operator will return undef when you reach end-of-file, this is handy for dropping out of loops:

    while (defined($line = <STDIN>)) {
      print "I saw $line";
    }

There’s a lot going on in that first line: we’re reading the input into a variable, checking that it’s defined, and if it is (meaning that we haven’t reached the end of the input), we’re running the body of the while loop. So, inside the body of the loop, we’ll see each line, one after another, in $line.[114] This is something you’ll want to do fairly often, so naturally Perl has a shortcut for it. The shortcut looks like this:

    while (<STDIN>) {
      print "I saw $_";
    }

Now, to make this shortcut, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Perl, 3rd Edition

Learning Perl, 3rd Edition

Tom Phoenix, Randal L. Schwartz
Learning Perl, 6th Edition

Learning Perl, 6th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming Perl, 3rd Edition

Programming Perl, 3rd Edition

Larry Wall, Tom Christiansen, Jon Orwant

Publisher Resources

ISBN: 0596101058Catalog PageErrata