Обсуждение: Strange behaviour of PL/Perl
Hello All!
I just included PL/Perl language in my database.
Created function:
CREATE FUNCTION and_with_mask(int2, int2) RETURNS int2 AS
'
return $_[0] &
$_[1]
' LANGUAGE 'plperl';
select and_with_mask(4,1);
and_with_mask
---------------
0
select and_with_mask(8,1);
and_with_mask
---------------
0
select and_with_mask(16,1);
and_with_mask
---------------
1
select and_with_mask(32,1);
and_with_mask
---------------
1
And just in Perl:
#!/usr/bin/perl
$val = 32 &
1;
print("val = $val\n");
val = 0
Where am I mistaken or something is in PL/Perl?
--
with respection Andrey Feofilactovich.
e-mail: feo@ttn.ru, feo@feo.org.ru
ICQ: 28073807
"Andrey Y. Mosienko" <feo@ttn.ru> writes:
> I just included PL/Perl language in my database.
> Created function:
> CREATE FUNCTION and_with_mask(int2, int2) RETURNS int2 AS
> [ behaves strangely ]
What Postgres version is this, on what platform?
Does the function behave more reasonably if you declare the
inputs and result as int4?
regards, tom lane
"Andrey Y. Mosienko" wrote:
>
> Hello All!
>
> I just included PL/Perl language in my database.
> Created function:
>
> CREATE FUNCTION and_with_mask(int2, int2) RETURNS int2 AS
> '
> return $_[0] &
> $_[1]
> ' LANGUAGE 'plperl';
> select and_with_mask(32,1);
> and_with_mask
> ---------------
> 1
> #!/usr/bin/perl
> $val = 32 & 1;
> print("val = $val\n");
>
> val = 0
>
> Where am I mistaken or something is in PL/Perl?
perl -e 'print ("32" & "1"),"\n"'
Values are being passed in as strings so perl is doing bitwise string
operation (perldoc perlop). Convert to numbers by doing +0 or similar.
- Richard Huxton