Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Use of uninitialized value in concatenation (.) or string

Reply
Thread Tools

Use of uninitialized value in concatenation (.) or string

 
 
Amaninder
Guest
Posts: n/a
 
      07-18-2006
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-18-2006
Amaninder wrote:
> 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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      07-18-2006

Amaninder wrote:
> 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".

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-18-2006
Amaninder wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      07-18-2006
Amaninder wrote:

> Subject: Use of uninitialized value in concatenation (.) or string


[ snip - message body not mentioning this warning ]

Huh?

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-18-2006
Amaninder <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      07-18-2006
Amaninder wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Newbie) Use of uninitialized value in concatenation (.) or string Jesse Cary Perl Misc 2 10-15-2004 09:47 PM
Use of uninitialized value in concatenation smartins68 Perl 1 06-09-2004 05:46 AM
Re: Use of uninitialized value in concatenation (.) or string Error Sukhbir Dhillon Perl 1 04-05-2004 02:31 AM
Use of uninitialized value in concatenation (.) or string S Perl Misc 3 02-03-2004 09:54 PM
Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10. G Kannan Perl 1 10-11-2003 11:58 AM



Advertisments