Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Grab input from the user and Open the file

by Lotus1 (Vicar)
on Nov 28, 2016 at 18:16 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176718]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found