#!/usr/bin/env perl use strict; use warnings; my $file = '/no_such_file.txt'; my $infh; while (not open $infh, '<', $file) { print "Could not open $file: $!. Please provide another:\n"; chomp ($file = ); } my $first = <$infh>; print "First line of $file: $first"; close $infh; exit; #### $ ./fo.pl Could not open /no_such_file.txt: No such file or directory. Please provide another: foobar Could not open foobar: No such file or directory. Please provide another: /etc/hosts First line of /etc/hosts: 127.0.0.1 localhost.localdomain localhost