hide random home http://www.fmi.uni-passau.de/archive/doc/unix/perl/faq/2.17.html (Einblicke ins Internet, 10/1995)

How can I make a file handle local to a subroutine?

How can I make a file handle local to a subroutine?


    You must use the type-globbing *VAR notation.  Here is some code to
    cat an include file, calling itself recursively on nested local
    include files (i.e. those with #include "file", not #include <file>):

        sub cat_include {
            local($name) = @_;
            local(*FILE);
            local($_);

            warn "<INCLUDING $name>\n";
            if (!open (FILE, $name)) {
                warn "can't open $name: $!\n";
                return;
            }
            while (<FILE>) {
                if (/^#\s*include "([^"]*)"/) {
                    &cat_include($1);
                } else {
                    print;
                }
            }
            close FILE;
        }