#!/usr/bin/perl use strict; use warnings; my $file = '/path/file.dat'; my $exists = ( -e $file ); my $normal = ( -f $file ); $exists and (not $normal) and die "$file is unsuitable for this program"; my $iocmd = $normal ? "<$file" : ">$file"; # until now we could check the existence of the file # and how to open it and we did not need BEGIN open my $fh, $iocmd or die "$!, for $file"; my @content = $normal ? <$fh> # only now is the file read in if exists :( "I am\n", "the default\n", "contents\n" ); $normal or print $fh @content; # or printed to otherwise close $fh or die "$!, for $file"; # now both the file and @content contain either tbe # program default or previously stored content depending on # whether the file existed.