[28131] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 9495 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 18 14:10:16 2006

Date: Tue, 18 Jul 2006 11:10:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 18 Jul 2006     Volume: 10 Number: 9495

Today's topics:
        Use of uninitialized value in concatenation (.) or stri <Amaninder.Saini@gmail.com>
    Re: Use of uninitialized value in concatenation (.) or  <mritty@gmail.com>
    Re: Use of uninitialized value in concatenation (.) or  <nobull67@gmail.com>
    Re: Use of uninitialized value in concatenation (.) or  <someone@example.com>
    Re: Use of uninitialized value in concatenation (.) or  <nobull67@gmail.com>
    Re: Use of uninitialized value in concatenation (.) or  anno4000@radom.zrz.tu-berlin.de
    Re: What is a type error? [correction] <dnew@san.rr.com>
    Re: What is a type error? [correction] <david.nospam.hopwood@blueyonder.co.uk>
    Re: What is the global hash %_ ? (SOLVED) <ozarfreo@yahoo.com>
    Re: What is the global hash %_ ? <rvtol+news@isolution.nl>
    Re: What is the global hash %_ ? <nobull67@gmail.com>
    Re: What is the global hash %_ ? <rvtol+news@isolution.nl>
    Re: When would you use qr// on a literal string? <nobull67@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 18 Jul 2006 09:58:48 -0700
From: "Amaninder" <Amaninder.Saini@gmail.com>
Subject: Use of uninitialized value in concatenation (.) or string
Message-Id: <1153241928.778031.60290@i42g2000cwa.googlegroups.com>

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



------------------------------

Date: 18 Jul 2006 10:12:24 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Use of uninitialized value in concatenation (.) or string
Message-Id: <1153242744.082289.163270@m79g2000cwm.googlegroups.com>

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



------------------------------

Date: 18 Jul 2006 10:19:22 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Use of uninitialized value in concatenation (.) or string
Message-Id: <1153243162.834893.164560@i42g2000cwa.googlegroups.com>


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



------------------------------

Date: Tue, 18 Jul 2006 17:20:48 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Use of uninitialized value in concatenation (.) or string
Message-Id: <QL8vg.51805$B91.21814@edtnps82>

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


------------------------------

Date: 18 Jul 2006 10:21:36 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Use of uninitialized value in concatenation (.) or string
Message-Id: <1153243296.037405.301990@m73g2000cwd.googlegroups.com>

Amaninder wrote:

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

[ snip -  message body not mentioning this warning ]

Huh?



------------------------------

Date: 18 Jul 2006 17:28:50 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Use of uninitialized value in concatenation (.) or string
Message-Id: <4i4k2iF24jetU1@news.dfncis.de>

Amaninder <Amaninder.Saini@gmail.com> 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


------------------------------

Date: Tue, 18 Jul 2006 16:13:34 GMT
From: Darren New <dnew@san.rr.com>
Subject: Re: What is a type error? [correction]
Message-Id: <OM7vg.29227$uy3.13575@tornado.socal.rr.com>

David Hopwood wrote:
> Darren New wrote:
> 
>>David Hopwood wrote:
>>
>>
>>>public class LoopInitTest {
>>>    public static String getString() { return "foo"; }
>>>
>>>    public static void main(String[] args) {
>>>        String line = getString();
>>>        boolean is_last = false;
>>>
>>>        while (!is_last) {
>>>            if (line.charAt(0) == 'q') {
>>>                is_last = true;
>>>            }
>>>
>>>            // insert line into inputs (not important for analysis)
>>>
>>>            if (!is_last) {
>>>                line = getString();
>>>            }
>>>        }
>>>    }
>>>}
>>>
>>>which compiles without error, because is_last is definitely initialized.
>>
>>At what point do you think is_last or line would seem to not be
>>initialized? They're both set at the start of the function, and (given
>>that it's Java) nothing can unset them.
>>
>>At the start of the while loop, it's initialized. At the end of the
>>while loop, it's initialized. So the merge point of the while loop has
>>it marked as initialized.
> 
> 
> Apparently, Hermes (at least the version of it described in that paper)
> essentially forgets that is_last has been initialized at the top of the
> loop, and so when it does the merge, it is merging 'not necessarily initialized'
> with 'initialized'.


No, it's not that it "forgets". It's that the "insert line into inputs" 
unitializes "line". Hence, "line" is only conditionally set at the 
bottom of the loop, so it's only conditionally set at the top of the loop.

> This sounds like a pretty easy thing to fix to me (and maybe it was fixed
> later, since there are other papers on Hermes' typestate checking that I
> haven't read yet).

You simply misunderstand the "insert line into inputs" semantics. Had 
that line actually been commented out in the Hermes code, the loop would 
have compiled without a problem.

That said, in my experience, finding this sort of typestate error 
invariably led me to writing clearer code.

boolean found_ending = false;
while (!found_ending) {
   string line = getString();
   if (line.charAt(0) == 'q')
     found_ending = true;
   else
     insert line into inputs;
}

It seems that's much clearer to me than the contorted version presented 
as the example. If you want it to work like the Java code, where you can 
still access the "line" variable after the loop, the rearrangement is 
trivial and transparent as well.

-- 
   Darren New / San Diego, CA, USA (PST)
     This octopus isn't tasty. Too many
     tentacles, not enough chops.


------------------------------

Date: Tue, 18 Jul 2006 17:18:08 GMT
From: David Hopwood <david.nospam.hopwood@blueyonder.co.uk>
Subject: Re: What is a type error? [correction]
Message-Id: <kJ8vg.2656$q97.1718@fe3.news.blueyonder.co.uk>

Darren New wrote:
> David Hopwood wrote:
> 
[...]
>> Apparently, Hermes (at least the version of it described in that paper)
>> essentially forgets that is_last has been initialized at the top of the
>> loop, and so when it does the merge, it is merging 'not necessarily
>> initialized' with 'initialized'.
> 
> No, it's not that it "forgets". It's that the "insert line into inputs"
> unitializes "line". Hence, "line" is only conditionally set at the
> bottom of the loop, so it's only conditionally set at the top of the loop.
> 
>> This sounds like a pretty easy thing to fix to me (and maybe it was fixed
>> later, since there are other papers on Hermes' typestate checking that I
>> haven't read yet).
> 
> You simply misunderstand the "insert line into inputs" semantics.

Yes, you're right, I did misunderstand this.

> Had that line actually been commented out in the Hermes code, the loop would
> have compiled without a problem.
> 
> That said, in my experience, finding this sort of typestate error
> invariably led me to writing clearer code.
> 
> boolean found_ending = false;
> while (!found_ending) {
>   string line = getString();
>   if (line.charAt(0) == 'q')
>     found_ending = true;
>   else
>     insert line into inputs;
> }
> 
> It seems that's much clearer to me than the contorted version presented
> as the example. If you want it to work like the Java code, where you can
> still access the "line" variable after the loop, the rearrangement is
> trivial and transparent as well.

I agree.

-- 
David Hopwood <david.nospam.hopwood@blueyonder.co.uk>


------------------------------

Date: 18 Jul 2006 09:04:14 -0700
From: "ozarfreo" <ozarfreo@yahoo.com>
Subject: Re: What is the global hash %_ ? (SOLVED)
Message-Id: <1153238654.269777.141100@m79g2000cwm.googlegroups.com>

Ferry Bolhar wrote:
>Those described in perlvar have additional functionality.
>Those not described there may have one in the future, so you shouldn't
>use them nevertheless.

> From perlmod:
> << Only identifiers starting with letters (or underscore) are stored
> in a package's symbol table.  All other symbols are kept in package
> "main", including all punctuation variables, like $_. >>

Thanks to all! That completely answers my question.



------------------------------

Date: Tue, 18 Jul 2006 16:36:18 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: What is the global hash %_ ?
Message-Id: <e9j2mh.jg.1@news.isolution.nl>

ozarfreo@yahoo.com schreef:

> I've seen that %_ is treated as a special variable in Perl, as $_ and
> @_ are: no warning is issued when using it without a package name, no
> 'my' is allowed on that var; the problem is that I could not find it
> among Perl's documentation.
>
> I'd like to use it in a program but am not sure whether I might be
> interfering with some other intended use for %_ . Can someone point
> out what is this variable exactly, and whether it is safe to use it?

$ perl -Mstrict -wle '
  $, = "<\n";
  @_ = (one=>1, two=>2, three=>3);
  print @_, "$_[1] <--"
'
one<
1<
two<
2<
three<
3<
1 <--


$ perl -Mstrict -wle '
  $, = "<\n";
  %_ = (one=>1, two=>2, three=>3);
  print %_, "$_{one} <--"
'
three<
3<
one<
1<
two<
2<
1 <--

-- 
Affijn, Ruud

"Gewoon is een tijger."




------------------------------

Date: 18 Jul 2006 09:46:10 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: What is the global hash %_ ?
Message-Id: <1153241170.500527.29820@m79g2000cwm.googlegroups.com>

Gunnar Hjalmarsson wrote:

> Considering similar variables that are used by Perl and documented in
> perlvar, it does not seem unlikely that it will be predefined in future
> Perl versions. Hence, I for one would not use it. To be safe.

It seems moderately unlikely to be and I do sometimes %_ it to
represent the "current context" in callback APIs.

{
   local *_ =  \%context;
   $foo_callback->($foo_callback_arg1,$foo_callback_arg2);
}



------------------------------

Date: Tue, 18 Jul 2006 19:18:30 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: What is the global hash %_ ?
Message-Id: <e9jceb.h4.1@news.isolution.nl>

Brian McCauley schreef:
> Gunnar Hjalmarsson wrote:
>
>> Considering similar variables that are used by Perl and documented in
>> perlvar, it does not seem unlikely that it will be predefined in
>> future Perl versions. Hence, I for one would not use it. To be safe.
>
> It seems moderately unlikely to be and I do sometimes %_ it to
> represent the "current context" in callback APIs.
>
> {
>    local *_ =  \%context;
>    $foo_callback->($foo_callback_arg1,$foo_callback_arg2);
> }

I once tried to make &_ do as much as possible what say() will, a bit
like this:

  sub _{print @_, "\n"}

  sub _{print +(@_ ? @_ : $_), $/}

  sub _(*@)
  {
      local $\ = "\n" ;
      0 == @_ and return print ;
      1 == @_ and ref $_[0] ? return print @{ $_[0] }
                            : return print    $_[0] ;
      my $fh = $_[0] ;
      ref $_[1] ? print $fh @{ $_[1] }
                : print $fh    $_[1] ;
  }

See also:
  perl -MPerl6::Say -e 'say for (1,2,3)'

-- 
Affijn, Ruud

"Gewoon is een tijger."




------------------------------

Date: 18 Jul 2006 10:05:08 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: When would you use qr// on a literal string?
Message-Id: <1153242308.661720.22140@b28g2000cwb.googlegroups.com>


it_says_BALLS_on_your forehead wrote:

> I thought that qr was mainly used for pre-compiling variables into
> regex patterns, but a colleague uses it like so:
>
> my ( $name, $value ) = split qr/=/, $string;
>
> Are there any benefits to doing this?

It could be argued that it aids clarity.

The built-in split() function has a magic prototype such that
split(/=/, $string) is, in effect, treated as if you'd said
split(qr/=/, $string). If you wanted to write your own split()-like
function you'd need a qr// in the call.

That said, I think most Perl programmers have gotten used to seeing
split(/=/, $string) and would find the explicit qr// less clear.



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 9495
***************************************


home help back first fref pref prev next nref lref last post