niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
Use of uninitialized value in concatenation (.) or string
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
Amaninder.Saini@gmail.com
*nix forums beginner


Joined: 02 Jun 2006
Posts: 11

PostPosted: Tue Jul 18, 2006 4:58 pm    Post subject: Use of uninitialized value in concatenation (.) or string Reply with quote

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
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Tue Jul 18, 2006 5:12 pm    Post subject: Re: Use of uninitialized value in concatenation (.) or string Reply with quote

Amaninder wrote:
Quote:
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
Back to top
nobull@mail.com
*nix forums Guru Wannabe


Joined: 09 Nov 2005
Posts: 240

PostPosted: Tue Jul 18, 2006 5:19 pm    Post subject: Re: Use of uninitialized value in concatenation (.) or string Reply with quote

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

Quote:
#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");

Quote:
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".
Back to top
John W. Krahn
*nix forums Guru


Joined: 27 Feb 2005
Posts: 602

PostPosted: Tue Jul 18, 2006 5:20 pm    Post subject: Re: Use of uninitialized value in concatenation (.) or string Reply with quote

Amaninder wrote:
Quote:

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 Smile

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
Back to top
nobull@mail.com
*nix forums Guru Wannabe


Joined: 09 Nov 2005
Posts: 240

PostPosted: Tue Jul 18, 2006 5:21 pm    Post subject: Re: Use of uninitialized value in concatenation (.) or string Reply with quote

Amaninder wrote:

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

[ snip - message body not mentioning this warning ]

Huh?
Back to top
anno4000@radom.zrz.tu-ber
*nix forums beginner


Joined: 05 Jul 2006
Posts: 40

PostPosted: Tue Jul 18, 2006 5:28 pm    Post subject: Re: Use of uninitialized value in concatenation (.) or string Reply with quote

Amaninder <Amaninder.Saini@gmail.com> wrote in comp.lang.perl.misc:
Quote:
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.

Quote:

#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.

Quote:
$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;

Quote:
#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
Back to top
J. Gleixner
*nix forums Guru Wannabe


Joined: 10 Mar 2005
Posts: 195

PostPosted: Tue Jul 18, 2006 5:29 pm    Post subject: Re: Use of uninitialized value in concatenation (.) or string Reply with quote

Amaninder wrote:
Quote:
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????


Quote:

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 Smile

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.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 7:33 am | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts FAQ 4.32 How do I strip blank space from the beginning/en... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 pm
No new posts FAQ 4.34 How do I extract selected columns from a string? PerlFAQ Server Perl 0 Fri Jul 21, 2006 7:03 am
No new posts print all permutations of string anurag C 10 Thu Jul 20, 2006 5:57 pm
No new posts converting array values to monomaniac21 PHP 11 Thu Jul 20, 2006 10:17 am
No new posts Depricated String Functions in Python Anoop python 14 Thu Jul 20, 2006 6:26 am

Fish Tank Help | Watch Anime Online | Buy Used Cell Phone | Myspace Codes | Looking for Credit Cards?
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2751s ][ Queries: 16 (0.1725s) ][ GZIP on - Debug on ]