You were almost there. You just needed to open the file which the user provided. Since this can also fail I prefer a loop. eg:
#!/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 = <STDIN>); } my $first = <$infh>; print "First line of $file: $first"; close $infh; exit;
Running this we see something like this:
$ ./fo.pl Could not open /no_such_file.txt: No such file or directory. Please pr +ovide another: foobar Could not open foobar: No such file or directory. Please provide anoth +er: /etc/hosts First line of /etc/hosts: 127.0.0.1 localhost.localdomain local +host
In reply to Re: if user provide input file use that in open() function instead
by hippo
in thread if user provide input file use that in open() function istead
by Uzma Shaikh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |