Autovivification - File and Directory Handles

File and Directory Handles

Perl 5.6.1 and newer support autovivification of file and directory handles. Calling open on an undefined variable will set it to a filehandle. According to perl561delta, "his largely eliminates the need for typeglobs when opening filehandles that must be passed around, as in the following example:

for my $file ( qw(this.conf that.conf) ) { my $fin = open_or_throw('<', $file); process_conf( $fin ); # no close needed } use Carp; sub open_or_throw { my ($mode, $filename) = @_; open my $h, $mode, $filename or croak "Could not open '$filename': $!"; return $h; }

Read more about this topic:  Autovivification

Famous quotes containing the words file and/or directory:

    A common and natural result of an undue respect for law is, that you may see a file of soldiers, colonel, captain, corporal, privates, powder-monkeys, and all, marching in admirable order over hill and dale to the wars, against their wills, ay, against their common sense and consciences, which makes it very steep marching indeed, and produces a palpitation of the heart.
    Henry David Thoreau (1817–1862)

    Although then a printer by trade, he listed himself in this early directory as an antiquarian. When he was asked the reason for this he replied that he always thought every town should have at least one antiquarian, and since none appeared for the post, he volunteered.
    —For the State of Iowa, U.S. public relief program (1935-1943)