icon Top 9 categories map      RocketAware > Perl >

How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles?

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.



Home

Search Perl pages


Subjects

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

    

How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles?

You may have some success with typeglobs, as we always had to use in days of old:

    local(*FH);

But while still supported, that isn't the best to go about getting local filehandles. Typeglobs have their drawbacks. You may well want to use the FileHandle module, which creates new filehandles for you (see the FileHandle manpage):

    use FileHandle;
    sub findme {
        my $fh = FileHandle->new();
        open($fh, "</etc/hosts") or die "no /etc/hosts: $!";
        while (<$fh>) {
            print if /\b127\.(0\.0\.)?1\b/;
        }
        # $fh automatically closes/disappears here
    }

Internally, Perl believes filehandles to be of class IO::Handle. You may use that module directly if you'd like (see Handle), or one of its more specific derived classes.

Once you have IO::File or FileHandle objects, you can pass them between subroutines or store them in hashes as you would any other scalar values:

    use FileHandle;

    # Storing filehandles in a hash and array
    foreach $filename (@names) {
        my $fh = new FileHandle($filename)              or die;
        $file{$filename} = $fh;
        push(@files, $fh);
    }

    # Using the filehandles in the array
    foreach $file (@files) {
        print $file "Testing\n";
    }

    # You have to do the { } ugliness when you're specifying the
    # filehandle by anything other than a simple scalar variable.
    print { $files[2] } "Testing\n";

    # Passing filehandles to subroutines
    sub debug {
        my $filehandle = shift;
        printf $filehandle "DEBUG: ", @_;
    }

    debug($fh, "Testing\n");


Source: Perl FAQ: Files and Formats
Copyright: Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
Next: How can I set up a footer format to be used with write()?

Previous: How can I manipulate fixed-record-length files?



(Corrections, notes, and links courtesy of RocketAware.com)


[Overview Topics]

Up to: File Access




Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlfaq5/How_can_I_make_a_filehandle_loca.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments