Input, Output, and Lists

We'll finish up today's lesson as we did yesterday: by talking a little more about input and output, this time with list and array context in mind. There are two topics to cover here that will help you work more with input and with files:

  • Using <STDIN> in a list context

  • Printing lists

Using <STDIN> in list context

Yesterday, you learned about <STDIN>, and how it's used to read data from the standard input. Up to now, we've been using it like this:

chomp($in = <STDIN>);

A close look at that line shows that you're using <STDIN> here in a scalar context—you're assigning it to the scalar variable $in. Like many other Perl operations, the input operator <> behaves differently in a list context than it does in a scalar one.

If you use <STDIN> in a scalar context, Perl reads a line of input up until the newline character. In a list context, however, <STDIN> reads all the input it can get, with each line stored as a separate element in the list. It only stops when it gets to an end-of-file.

That's a rather confusing explanation, given that standard input wouldn't seem to really have an end-of-file. But it does, actually. Typing a Ctrl+D (Ctrl+Z on Windows) tells Perl “this is the end of file” for standard input. If you use <STDIN> in a list context, then, Perl will wait until you've entered all your data and hit Ctrl+D or Ctrl+Z, put all that data into a list, and then continue with the script.

The use of input in a list context is much more useful when you're reading input from actual files, and you've got an explicit end-of-file. Generally you'll use <STDIN> to read individual lines of input from the keyboard in a scalar context. But it's important to realize the difference between input in a scalar context versus input from a list context; as with many other parts of Perl, there are differences in behavior between the two, and if you confuse them the bugs can get tricky.

Printing Lists

In the examples we've done in this chapter we've printed lists by using loops to examine and print each element of the list. If you just want to print the elements of a list without modifying them, however, there's an easier way: just print them using the print function. Given that print assumes its arguments are in list context, this makes it easy.

Well, sort of easy. Here's a simple list from 1 to 9:

@list = (1..9);

If you print this list with just the print function, you'll end up with this:

123456789

You won't even get a newline character at the end. By default, by printing a list, all the values of the list are concatenated together.

What if you want to print them with spaces in between them? You could use a while or foreach loop to do that. But there is an easier way. You can use variable interpolation for the list variable. You might remember yesterday we talked about variable interpolation inside strings, where given a string "this is string $count", the $count variable would be replaced with its actual value. Variable interpolation also happens with array and hash variables—the contents of the array are simply printed in order, separated by a space. For example, if you include the @list variable inside quotes, with a newline character:

print "@list
";

These lines will result in the list being printed with spaces in between the elements, with a newline at the end.Variable interpolation with list variables makes it easy to print out the contents of a list without resorting to loops. Note, however, that this does mean if you want to use the @ character inside a double-quoted string, you'll often need to backslash it to prevent Perl from searching for an array that doesn't exist (and then complaining that it doesn't). Perl warnings will let you know if you're making this mistake.

Note

Another way to control the printing of lists is to set special global Perl variables for the output field separator and output record separator. More about these special variables in today's “Going Deeper” section.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset