in reply to Using a Sub to pull info. from a Text File

The most common way is to open the file and go through it line by line, either storing the bits of data you need in hashes and arrays, etc, or just printing as you go... i.e.
open(DATA, '/tmp/file') or die $!; $header = 1; while(<DATA>) { $header = 0 if(/^$/); if($header) { $header .= $_; } else { $body .= $_; } } close DATA;
Now, in doing that (example works well with email message or raw web data, header is terminated by blank line, rest is body info) younow have the header as one string, and the body as another, and can pass these off to subroutines... but that is just one way to do it.
BTW: while(<DATA>) goes through the file, line by line, putting each line into $_ for you to use, if you did not know that already.
                - Ant

Replies are listed 'Best First'.
Re: Re: Using a Sub to pull info. from a Text File
by sierrathedog04 (Hermit) on Apr 11, 2001 at 01:04 UTC
    Someone said on one of the other nodes today that DATA has a special meaning in Perl. In order to avoid name collisions ought not to call one's own filehandles DATA.

    I do recall Larry Wall saying that the possibility of name collisions in Perl exists only with respect to filehandles. He said that one should always name one's filehandles using all uppercase letters. And apparently, in addition one should not name a filehandle DATA.

      That was here and it was wrong. The only reason to not use a filehandle of DATA is in case you will want to upgrade your script to have an __END__ section (or your module to have a __DATA__ section).

      So I agree that it is a good idea to not use DATA as a file handle, but I disagree that doing so will cause problems with the script (just potential maintenance problems).

              - tye (but my friends call me "Tye")
      I've never had a problem using DATA as a filehandle.
                      - Ant