next up previous contents index
Next: Excercise Up: I/O Previous: Example - Erase a   Contents   Index

File input

Save as dictionary.pl

    $filename = '/usr/share/lib/dict/words';
    $word = "cat";
    open INPUT, $filename;
    while (<INPUT>) {
        chomp;
        if ($_ eq $word) {

            print "$word is in the dictionary.\n";
            last; 
        }
    }

The perl function `open' allows access to '$filename' through the file handle `INPUT'. This time, `while' works one line at a time through the file handle `INPUT' which refers to the file `$filename'. The perl function last exits immediately from a loop. Here, the loop is the `while'.



Tom Hunt 2002-06-09