[prev] [index] [next]

Input/Output (cont)

Handles can be explicitly attached to files via the open command:

  open(DATA, "< data");     # read from a file called "data"
  open(RES,  "> results");  # write to a file called "results"
  open(XTRA, ">> stuff");   # append to a file called "stuff"

Handles can even be attched to pipelines to read/write to Unix commands:

  open(DATE, "/bin/date |"); # read from output of date command
  open(FEED, "| more");      # send output through the more command

Opening a handle may fail:

   open(DATA, "< data")  or  die "Can't open data file";

Handles are closed using  close(HandleName)  (or automatically closed on exit).