Use of uninitialized value in concatenation (.) or string

A

Amaninder

Hi everyone
I am new to perl and i am using ActiveState and activePerl 5.6 Can
someone help in figuring out why the variable $error has value of 1 in
it.

Here is the code.

#!/usr/bin/perl -w
use strict;

sub getExpectedResultFromFile{

my $fileName = shift;
my $index = shift;

my ($errorMess, @resultAtIndex, $line);

#if the parameter are wrong then quit with an error
unless ( defined($fileName) && defined($index) &&
$fileName =~ /.+/ && $index =~ /.+/ ){
$errorMess = "getExpectedResultFromFile(): Either \$fileName or
\$index is not correct";
return($errorMess, @resultAtIndex);
}

#open the file

}

my ($error , @result) = getExpectedResultFromFile("file.txt", "abc");
print "\n\$error===='$error'";
print "\n\@result===='@result'";




Here is the result



$error===='1'
@result====''


I dont understand why $error = 1. I never assigned 1 to $error in my
code. If someone knows the reason please let me know.
Thanks in advance :)

Amaninder
 
P

Paul Lalli

Amaninder said:
Hi everyone
I am new to perl and i am using ActiveState and activePerl 5.6 Can
someone help in figuring out why the variable $error has value of 1 in
it.

Here is the code.

#!/usr/bin/perl -w
use strict;

sub getExpectedResultFromFile{

my $fileName = shift;
my $index = shift;

my ($errorMess, @resultAtIndex, $line);

#if the parameter are wrong then quit with an error
unless ( defined($fileName) && defined($index) &&
$fileName =~ /.+/ && $index =~ /.+/ ){
$errorMess = "getExpectedResultFromFile(): Either \$fileName or
\$index is not correct";
return($errorMess, @resultAtIndex);
}

#open the file

}

my ($error , @result) = getExpectedResultFromFile("file.txt", "abc");
print "\n\$error===='$error'";
print "\n\@result===='@result'";

Here is the result

$error===='1'
@result====''

I dont understand why $error = 1. I never assigned 1 to $error in my
code. If someone knows the reason please let me know.

In the absense of any explicit return statement, Perl subroutines
return the last value evaluated. In this case, that's the success of
the unless condition. The condition of the unless was true (ie, "1"),
so the unless block didn't happen, and the subroutine returned the last
value evaluated.

If you want it to return truly "nothing", put an explicit return at the
end of the subroutine:
return;

Paul Lalli
 
B

Brian McCauley

Amaninder said:
Hi everyone
I am new to perl and i am using ActiveState and activePerl 5.6 Can
someone help in figuring out why the variable $error has value of 1 in
it.

Here is the code.

#!/usr/bin/perl -w
use strict;

sub getExpectedResultFromFile{

my $fileName = shift;
my $index = shift;

my ($errorMess, @resultAtIndex, $line);

I suspect you're in the early stages of contracting a nasty case of
premature declaration. You should treat the symptoms now and move the
declaration of $errorMess into the correct scope before your affliction
really starts to cause problems for you.
#if the parameter are wrong then quit with an error
unless ( defined($fileName) && defined($index) &&
$fileName =~ /.+/ && $index =~ /.+/ ){
$errorMess = "getExpectedResultFromFile(): Either \$fileName or
\$index is not correct";
return($errorMess, @resultAtIndex);
}

#open the file

}

my ($error , @result) = getExpectedResultFromFile("file.txt", "abc");
print "\n\$error===='$error'";
print "\n\@result===='@result'";
I dont understand why $error = 1. I never assigned 1 to $error in my
code.

Er, yes you did, in the line:

my ($error , @result) = getExpectedResultFromFile("file.txt", "abc");
If someone knows the reason please let me know.

getExpectedResultFromFile returned the value (1).

Note there's no explicit return() at the end of
&getExpectedResultFromFile so the return value is the value of the last
evaluated expression. (See perlsub).

In the situation where the last statement in the subroutine was a
unsuccessful conditional then the last evaluated expression is the
condtion.

defined($fileName) && defined($index) && $fileName =~ /.+/ && $index =~
/.+/

The value of this is 1. ( Even thought this behaviour is predicable
it's not something you really should depend upon. )

If, as I suspect, you want &getExpectedResultFromFile to return
nothing[1] if it succedes then you need to insert a bare return at the
end.

[1] "Nothing" is short-hand for "an empty list in a list context or
undef in a scalar one".
 
J

John W. Krahn

Amaninder said:
I am new to perl and i am using ActiveState and activePerl 5.6 Can
someone help in figuring out why the variable $error has value of 1 in
it.

Here is the code.

#!/usr/bin/perl -w
use strict;

sub getExpectedResultFromFile{

my $fileName = shift;
my $index = shift;

my ($errorMess, @resultAtIndex, $line);

#if the parameter are wrong then quit with an error
unless ( defined($fileName) && defined($index) &&
$fileName =~ /.+/ && $index =~ /.+/ ){
$errorMess = "getExpectedResultFromFile(): Either \$fileName or
\$index is not correct";
return($errorMess, @resultAtIndex);
}

#open the file

}

my ($error , @result) = getExpectedResultFromFile("file.txt", "abc");
print "\n\$error===='$error'";
print "\n\@result===='@result'";


Here is the result

$error===='1'
@result====''


I dont understand why $error = 1. I never assigned 1 to $error in my
code. If someone knows the reason please let me know.
Thanks in advance :)

perldoc perlsub
[snip]
The return value of a subroutine is the value of the last expression
evaluated by that sub, or the empty list in the case of an empty sub.

So the last expression "unless () {}" is returning '1'. Make:

return;

the last line in your sub.

Also "$fileName =~ /.+/ && $index =~ /.+/" would be better written as
"length($fileName) && length($index)" unless you really meant to check for
non-newline characters in which case "$fileName =~ /./ && $index =~ /./" would
be better.


John
 
A

anno4000

Amaninder said:
Hi everyone
I am new to perl and i am using ActiveState and activePerl 5.6 Can
someone help in figuring out why the variable $error has value of 1 in
it.

Here is the code.

#!/usr/bin/perl -w
use strict;

sub getExpectedResultFromFile{

my $fileName = shift;
my $index = shift;

my ($errorMess, @resultAtIndex, $line);

Declare variables on first use, if possible. $line is never used
and shouldn't be declared at all.
#if the parameter are wrong then quit with an error
unless ( defined($fileName) && defined($index) &&
$fileName =~ /.+/ && $index =~ /.+/ ){

Apparently you're testing if $fileName and $index aren't empty
strings. That is better tested using the length() function in
boolean context.
$errorMess = "getExpectedResultFromFile(): Either \$fileName or
\$index is not correct";
return($errorMess, @resultAtIndex);
}

You could replace the "unless"-block with this (untested):

my $errorMess = "getExpectedResultFromFile(): Either \$fileName " .
"or > \$index is not correct";
defined and length or return $errorMess for $fileName, $index;
#open the file

}

my ($error , @result) = getExpectedResultFromFile("file.txt", "abc");
print "\n\$error===='$error'";
print "\n\@result===='@result'";

Here is the result



$error===='1'
@result====''


I dont understand why $error = 1. I never assigned 1 to $error in my
code. If someone knows the reason please let me know.

It would be interesting to know what you expected, since you don't
return anything.

The "1" you're seeing is the result of (arguably) a bug in Perl.
If the last statement in a sub is an "if () {}" construct, the
sub may return a spurious value in list context. If it happens
every time and if it is always a "1" is anybody's guess, but it
has been seen before. Perl shouldn't return a value in that case.

Apparently the caller of your routine expects an error message
and/or a list of results. That's fine, but you should take care
actually to return some error message on every return path. Use
'' if there is no error.

Anno
 
J

J. Gleixner

Amaninder said:
Hi everyone
I am new to perl and i am using ActiveState and activePerl 5.6 Can
someone help in figuring out why the variable $error has value of 1 in
it.

What does that have to do with your subject????

Here is the code.

#!/usr/bin/perl -w
use strict;

sub getExpectedResultFromFile{

my $fileName = shift;
my $index = shift;

my ($errorMess, @resultAtIndex, $line);

#if the parameter are wrong then quit with an error
unless ( defined($fileName) && defined($index) &&
$fileName =~ /.+/ && $index =~ /.+/ ){
$errorMess = "getExpectedResultFromFile(): Either \$fileName or
\$index is not correct";
return($errorMess, @resultAtIndex);
}

#open the file

}

my ($error , @result) = getExpectedResultFromFile("file.txt", "abc");
print "\n\$error===='$error'";
print "\n\@result===='@result'";




Here is the result



$error===='1'
@result====''


I dont understand why $error = 1. I never assigned 1 to $error in my
code. If someone knows the reason please let me know.
Thanks in advance :)

From perlsub:

"If no return is found and if the last statement is an expression, its
value is returned."

In this case the $index =~ /.+/ was the last expression, and was 1, so
that's what's returned.

Use return, if you want to set what's returned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top