Without something to manipulate, the only thing you've done today is waste your time and sanity. So how do you get the stuff to manipulate? Through a magically wonderful thing called input-output. There are two major types of input and output: user input-output and file input-output.User input is when the user explicitly tells your script to do something. Output is when the script speaks directly to the user. This is all done through means called standard input and standard output. Input is received by reading the file handle. Even though standard input isn't a file, the two are handled the same way. You'll see what I mean when we get to file input-output. Standard input is read like so:
$usersaid = <STDIN>;
File handles are always written in uppercase so they can be easily identified. My wonderful editor Evany asked if they had anything to do with love handles. My first thought was to think, "Damn, is she hinting that I'm putting on weight?" But as fate would have it, file handles have nothing to do with love handles. File handles are the script's representation of an opened file. Standard input and output are always considered open, so you don't need to open or close them like the regular files I'll show you in a minute. The statement $stuff = <INPUT>; would read one line of input into the variable $stuff from the file handle INPUT. The above line would present the user with a line to enter input. Then whatever was entered would be saved in the variable $usersaid.The print command you've seen me use in previous examples is the standard method of outputting information. The print command is set by default to print out to the standard output, so you don't need to specify it like other file handles. To print the user's input back out onto the screen, you'd use:
print "$usersaid";
So with this in mind, you could put together a nice little guessing game:#!/usr/local/bin/perlprint "\nPlease enter a number: ";$guess = <STDIN>;while ($guess != 20) {print "\nSorry! You guessed wrong sucka! Guess again!";$guess = <STDIN>;}print "Damn! You got it!";
By using the while loop, we tested for the user input of $guess and kept looping until they got it right. And believe me, this isn't even the most irritating thing you can do! To make sure this little torture device is nice and easy for your victim to read, drop in some line breaks with \n, which is the character that makes new line in Unix.You can do file input and output with just a few extra steps. You first have to open a file, kinda like the way you have to open a can of good old Dr. Pepper. Like me, you always give each can a name. Then you go looking for it in the proper place in the fridge by calling out its name. So think of a witty yet descriptive file handle, know the path to the file, and use the open command. Here's how it's done:
open (NAMES, "/usr/people/ferm/names.txt");
Once the file is open, you can read in one line of the file at a time (the same way you would with user input above) by doing this ...$fileinput = <NAMES>;
... and storing it in a single scalar variable. Or you could read the whole thing into an array, with each slot as a separate line:@fileinput = <NAMES>;
Are you with me so far? File input has to be opened first. Then read it either one line at a time, or all at once. So once you've got the data, be sure you close the file handle in case you want to write back to it some day. And this brings me to Rule #7.Rule #7: It explicitly states that you cannot read and write to the same file at the same time. Closing is one line that looks something like:
close (NAMES);
There are two different modes for writing to a file: writing and appending. Writing deletes the contents of the entire file and writes over the file. Or if the file doesn't already exist, it creates a new file in which to dump the info. Appending adds the new information to the bottom of an existing file.
It's like this: Writing's like having a dead guy named Jerry. If there's already a Jerry in the graveyard, then you dig up the old Jerry and dump in the new Jerry. Or you can dig a new hole if one doesn't already exist. Appending is like throwing both Jerrys into the same hole. Nothing's sacred here, pal.
Writing:
open (OUTFILE, ">/usr/people/ferm/perllog.txt");
Appending:open (OUTFILE, ">>/usr/people/ferm/perllog.txt");
Once you decide how you want to write to a file, you have to print out to the file handle. It's just like printing out to the standard output, except that you specify explicitly where you want to go by putting the file handle between the print and the actual output:print OUTFILE "Here's a line of output";
And don't forget to close that file!
next page»