Reputation: 930
I have come into a situation where I need to read 2 lines data (username and password) from a file called login.txt(in current dir). After reading the username (1st line) has to be stored in $user and the second line (password) has to be stored in $pass.
In case if this file is not found, I have to prompt the user to enter the username and password and store it in $user and $pass and create the file login.txt(in currect dir) and write these two lines into the file.
my $user;
my $pass;
my $login = "login.txt";
unless (-e $login) {
print "Entering first time execution mode....!\n";
sleep(2);
print "Enter username:\n";
$user = <STDIN>;
chomp($user);
print "Enter password:\n";
$pass = <STDIN>;
chomp($pass);
unless(open LINFO, '>'.$login) {
# Die with error message
# if we can't open it.
die "\nUnable to create $login\n";
}
print LINFO "$user\n";
print LINFO "$pass\n";
close LINFO;
}
if (-e $login) {
open (LINFO, '$login') or die "Cant open\n";
while( my $line = <LINFO>) {
print $line;
chomp($line);
if ($. == 1) { $user = $line; }
elsif ($. == 2) { $pass = $line; }
last if $. == 2;
}
close LINFO;
}
print $pass;
print $user;
When the file is doesn't exists everything works fine (even $user and $pass gets printed), but will get a error message as below :-
readline() on closed filehandle LINFO at loginfile.pl line 43.
If the file already exists then I get following error
readline() on closed filehandle LINFO at loginfile.pl line 43.
Use of uninitialized value $pass in print at loginfile.pl line 59.
Use of uninitialized value $user in print at loginfile.pl line 60.
Not sure whats the issue here
Upvotes: 0
Views: 75
Reputation: 67900
Why is it exiting if it doesn't find the login.txt file? Because of the die
statement? Here's perldoc -f die
:
die LIST
die raises an exception. Inside an eval the error message is stuffed into $@ and the eval is terminated with the undefined value. If the exception is outside of all enclosing evals, then the uncaught exception prints LIST to STDERR and exits with a non-zero value. If you need to exit the process with a specific exit code, see exit.
While this is not terribly clear (to be precise, its horribly unclear for a newbie), you should probably know that die
means that the program exits, execution stops there.
When using open
, this is the recommended practice, since often any error in open calls should be considered fatal. You however ignore this open
call and use another open
on the same file handle later on in that block. The only reason I can see for
unless(open LINFO, $login) {
Would then be that you are trying to determine if the file exists, and you can read it. For this purpose, you can use the -X
commands, e.g.
if (-e $login) # if file exists
if (-r $login) # effective uid/gid can read file
...
Upvotes: 1
Reputation: 1420
You have to enclose the conditions for if
and elsif
in parentheses. Also be careful where to use =
and ==
. This should work for you:
if ($. == 1) { $user = $line; }
elsif ($. == 2) { $pass = $line; }
Upvotes: 1