Coder's Guild Mailing List

Passing filehandles PERL

Posted by Gerald P. Gillis on 2000-03-31

Here is a bit of code that I have used to pass file handles to functions 
in PERL.  More often, depending on the file size, enivironment, etc.  I 
will simple load the file into a buffer in memory then operate on it from 
there.  The following code is snipped from a script that creates a mini 
FAQ page from a file containing all the available FAQs.

if (!(open FAQFILE, "<$fp")) {
        # error checking code
}

# call to function using file handles
my ($rtn) = MakeTmpFAQ($at, FAQFILE, TMPFILE);

close FAQFILE;
close TMPFILE;

sub MakeTmpFAQ {

    # get arguments - $F and $T are file handles for FAQFILE and TMPFILE
    my ($at, $F, $T) = @_;

    while ($buf = <$F>) {       # read from FAQFILE
        if ($buf =~ m/$at/i) {
                                # write to TMPFILE
                print $T "<html><head><title>MINI FAQ</title>\n";
                # code code code
        }
        return 1;       # success return code
    }
    return 0;           # failed return code
}

GPG


G.P.Gillis__________________Software Engineer/Programmer/Technician
                                           
e-mail: gpgillis@xxxx.xxx  www: http://www.izzy.net/~gpgillis

"we've got computers, we leave 'em on ..." jimmie's chicken shack

_________________________________________Teach Your Children Well__


To signoff send a mail to listserver@xxxx.xx.xx with 
  "signoff tcg" in the body of your message.