Dev Web
Dev Web

Reputation: 21

Perl "Use of uninitialized value"

Perl script

my ($directory) = @ARGV; #"www"

if ( not defined $directory ) {
    die "ERROR";
}

if ( defined $directory ) {

    print "Yes or No ? (y/n) : ";
    my $terminer = <>; 
    chomp $terminer;

    if ( $terminer eq "o" ) {       
        print "OK";     
    }
    elsif ( $terminer eq "n" ) {
        ##########
    }
}

An error message appears:

Use of uninitialized value $terminer in scalar chomp at test.pl
Use of uninitialized value $terminer in string eq at test.pl

Can you help me?

Upvotes: 0

Views: 1587

Answers (2)

rawatsandeep1989
rawatsandeep1989

Reputation: 95

Additionally, you can also do the following:

chomp(my $terminar = <STDIN>);

Upvotes: 0

Chris Charley
Chris Charley

Reputation: 6613

When reading from empty angle brackets, <>, Perl reads from the files supplied in @ARGV if there are any. But you didn't have a file there: it was a directory name.

You copy an entry in @ARGV to $directory but also leave it in @ARGV. Then, further down in your code, my $terminer = <> tries to read from the "file" (that you have in @ARGV as the directory).

A fix could be either my $directory = shift @ARGV which should empty @ARGV and let you read keyboard input from the empty brackets further on in your code.

Or you could write my $terminer= <STDIN>, so that Perl will read only from the keyboard and not from the files listed in the @ARGV array.

Upvotes: 6

Related Questions