Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > This looks like a Perl bug

Reply
Thread Tools

This looks like a Perl bug

 
 
George Mpouras
Guest
Posts: n/a
 
      04-18-2013
# it should print string, string but it prints string number
# there is no explanation for this behavior

weird('2') for 1..2;

sub weird {
my $num = $_[0] eq '' ? 0 : $_[0] ^ $_[0] ? 0 : 1;
my $col = $_[0] - 1;
print $num ? "number\n" : "string\n"
}
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      04-18-2013
On 2013-04-18 17:33, George Mpouras wrote:
> # it should print string, string but it prints string number
> # there is no explanation for this behavior
>
> weird('2') for 1..2;
>
> sub weird {
> my $num = $_[0] eq '' ? 0 : $_[0] ^ $_[0] ? 0 : 1;
> my $col = $_[0] - 1; print $num ? "number\n" : "string\n"
> }


Not a bug. The parameter '2' becomes numeric, because of the operations.

Just rewrite it to:

sub silly {
my ($p) = @_; # copy
my $num = ($p eq "") ? 0 : ($p ^ $p) ? 0 : 1;
my $col = $p - 1;
print $num ? "number\n" : "string\n";
}


But what are you trying to achieve?

--
Ruud

 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      04-18-2013
"Dr.Ruud" <(E-Mail Removed)> writes:
> On 2013-04-18 17:33, George Mpouras wrote:
>> # it should print string, string but it prints string number
>> # there is no explanation for this behavior
>>
>> weird('2') for 1..2;
>>
>> sub weird {
>> my $num = $_[0] eq '' ? 0 : $_[0] ^ $_[0] ? 0 : 1;
>> my $col = $_[0] - 1; print $num ? "number\n" : "string\n"
>> }

>
> Not a bug. The parameter '2' becomes numeric, because of the
> operations.


This is not quite true. Same effect with somewhat less code:

------------
weird('2') for 1 .. 2;

sub weird {
my $num = $_[0] ^ $_[0] ? 0 : 1;
my $col = $_[0] - 1;
print $num ? "number\n" : "string\n"
}
------------

What happens here is that $_[0] ^ $_[0] returns a string whose value
is a single 0-byte during the first call. This is not a 'logical false
value', hence $num becomes 1. The $_[0] - 1 causes the scalar supplied
as first argument to weird to become IOK, that is, it acquires an IV
whose value is 2. The same scalar is used as argument to the second
weird call. Consequently, $_[0] ^ $_[0] becomes an integer operation
whose result is 0 which counts as false and $num becomes 1.

If this is not considered a bug, this kind of 'silent' runtime
modification of compile-time literals is at least very
counter-intuitive.


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      04-18-2013
Rainer Weikusat <(E-Mail Removed)> writes:

[...]

> my $num = $_[0] ^ $_[0] ? 0 : 1;
> my $col = $_[0] - 1;
> print $num ? "number\n" : "string\n"
> }
> ------------
>
> What happens here is that $_[0] ^ $_[0] returns a string whose value
> is a single 0-byte during the first call. This is not a 'logical false
> value', hence $num becomes 1.


s/becomes 1/becomes 0/
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      04-18-2013
the same peace of code is called with exact the same conditions and doing
different things

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      04-18-2013

>> But what are you trying to achieve?


its a very big project , what you see here is a very small piece of code
that expose the strange behaviour .

we had a very big headache to find what was wrong and where .



 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      04-18-2013
"George Mpouras"
<(E-Mail Removed) m.com.nospam> wrote:
>the same peace of code


It would help if you would include what piece (I'm assuming you meant
piece and not peace) of code you are talking about.

>is called with exact the same conditions


It would help if you would state what those conditions are.

> and doing different things


It would help if you would state what those different things are.

Without those details your statement is about as useful as "This thingy
is made of that green whatever".

jue
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      04-18-2013
"George Mpouras"
<(E-Mail Removed) m.com.nospam>
writes:
> the same peace of code is called with exact the same conditions and
> doing different things


It is not called 'with the exact same conditions': The '2' ends
up as an anonymous scalar initialized to the value of the
string literal and the state of this anonymous scalar is changed by
the routine.

NB: I do not claim that this is not a bug, especially considering that
there's apparently some special-case code to prevent undefined values
from becoming defined as side effect of using them as numbers.

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      04-18-2013

>> It would help if you would state what those conditions are.


all you need to know is the piece of code I provide . nothing else .


 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      04-18-2013


Ο "Rainer Weikusat" *γραψε στο μήνυμα
news:(E-Mail Removed) om...

"George Mpouras"
<(E-Mail Removed) m.com.nospam>
writes:
> the same peace of code is called with exact the same conditions and
> doing different things


>> It is not called 'with the exact same conditions': The '2' ends



no you are not right. it is well defined subroutine with one argument.

 
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
Really throwing this out there - does anyone have a copy of my oldDancer web browser? steven.miale@gmail.com Python 1 04-10-2013 03:32 PM
acts like an anchor, i.e., links to another page, but looks like a button AAaron123 ASP .Net 11 11-27-2008 01:01 PM
Torrent looks good. . . converted DVD files looks horrible. . . WHY!?! novak.arthur@gmail.com DVD Video 4 02-11-2007 06:57 PM
Tomcat Struts ClassCastException - looks like a Tomcat bug Stewart Java 3 08-18-2005 10:24 AM
this looks like a bug in generator comprehension Michele Simionato Python 1 11-12-2004 09:49 AM



Advertisments