Use of uninitialized value in concatenation (.) or string

Discussion in 'Perl Misc' started by Amaninder, Jul 18, 2006.

  1. Amaninder

    Amaninder Guest

    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
    Amaninder, Jul 18, 2006
    #1
    1. Advertising

  2. Amaninder

    Paul Lalli Guest

    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
    Paul Lalli, Jul 18, 2006
    #2
    1. Advertising

  3. 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".
    Brian McCauley, Jul 18, 2006
    #3
  4. 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
    John W. Krahn, Jul 18, 2006
    #4
  5. Amaninder wrote:

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


    [ snip - message body not mentioning this warning ]

    Huh?
    Brian McCauley, Jul 18, 2006
    #5
  6. Amaninder

    -berlin.de Guest

    Amaninder <> 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
    -berlin.de, Jul 18, 2006
    #6
  7. Amaninder

    J. Gleixner Guest

    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.
    J. Gleixner, Jul 18, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,234
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,253
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    6,003
    Joe Smith
    Jun 9, 2004
  4. S
    Replies:
    3
    Views:
    327
    Gunnar Hjalmarsson
    Feb 3, 2004
  5. Jesse Cary
    Replies:
    2
    Views:
    220
    J├╝rgen Exner
    Oct 15, 2004
Loading...

Share This Page