#!/usr/bin/perl -w
use strict;
open (IN, "somefile") || warn "unable to open somefile";
open (IN, "somefile") || hey_do_something();
sub hey_do_something
{
print "some stuff in a sub\n";
print "more stuff in a sub\n";
}
In the above normally "warn" would be "die". In almost all cases an OPEN should succeed on a file_path (usual case is that you know a valid file_path). There are exceptions of course, but they are aren't "normal".
Anyway above "warn", "die", "hey_do_something" are just subroutines to execute if the OPEN fails.
Update: If you are working with some user input, I would check that the file exists before even getting to the "open" statement unless the error message from the open() is very clear: open() or die "can't open $file $!"; |