Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Grab input from the user and Open the file

by Lotus1 (Vicar)
on Nov 28, 2016 at 18:16 UTC ( #1176718=note: print w/replies, xml ) Need Help??


in reply to Grab input from the user and Open the file

In addition to the excellent replies above I would like to mention file tests. While it is fine to attempt to open the file and then print out any errors, you can also test the file before you attempt to open it. If the file doesn't exist you could prompt the user to resubmit in case of a typo.

use warnings; use strict; my $directory = '/home/psimo/it441/challenge/logs'; + print "Please enter the file name: "; my $theFile = <STDIN>; chomp ($theFile); my $path = "$directory/$theFile"; if( ! -f $path ) { print "$path isn't a file.\n"; } else { print "$path is a file.\n"; } if( -T $path ) { print "$path is a text file.\n"; } else { print "$path is not a text file.\n"; }

Replies are listed 'Best First'.
Re^2: Grab input from the user and Open the file
by GotToBTru (Prior) on Nov 29, 2016 at 13:50 UTC
    my ($theFile); until (-f $theFile) { print "Please enter the file name:"; chomp($theFile = <STDIN>); }
    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1176718]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-01-21 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?