Let's do another example. Here's a script that prompts you for some input, and then echoes that input to the screen, like this:
% echo.pl Echo? Hi Laura Hi Laura %
Listing 1.2 shows the contents of the echo.pl script.
1: #!/usr/local/bin/perl -w 2: # echo the input to the output 3: 4: print 'Echo? '; 5: $input = <STDIN>; 6: print $input; |
You don't have to understand all of this script right now; I'll explain all the details tomorrow on Day 2. But you should feel comfortable typing and running this script, and have a general idea of how it works. Here's a quick run-through of the code:
Lines 1 and 2 are both comments: The first for the shebang line, the second to explain what the script does.
Line 4 prompts you to type something. Note that unlike "Hello World! " there's no in this string. That's because you want the cursor to stay on the end of the line after you finish printing, so your prompt actually behaves like a prompt.
Line 5 reads a line of input from the keyboard and stores it in the variable called $input. You don't have to keep track of the characters that get typed, or when the end of the line occurs; Perl reads up until the user hits Return (or Enter) and considers that the line.
Finally, Line 6 prints the value of the variable $input back to the screen.