Bryan R Harris wrote:
>
> Can someone explain this behavior?
perldoc perlfunc
[snip]
Any function in the list below may be used either with or without
parentheses around its arguments. (The syntax descriptions omit the
parentheses.) If you use the parentheses, the simple (but occasionally
surprising) rule is this: It looks like a function, therefore it is a
function, and precedence doesn't matter. Otherwise it's a list
operator or unary operator, and precedence does matter. And whitespace
between the function and left parenthesis doesn't count--so you need to
be careful sometimes:
print 1+2+4; # Prints 7.
print(1+2) + 4; # Prints 3.
print (1+2)+4; # Also prints 3!
print +(1+2)+4; # Prints 7.
print ((1+2)+4); # Prints 7.
If you run Perl with the -w switch it can warn you about this. For
example, the third line above produces:
print (...) interpreted as function at - line 1.
Useless use of integer addition in void context at - line 1.
> % perl -e 'print -12.17**0.2, "\n"'
> -1.64838295714428
$ perl -wle' print -12.17 ** 0.2 '
-1.64838295714428
perldoc perlop
[snip]
Exponentiation
Binary "**" is the exponentiation operator. It binds even more tightly
than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is
implemented using C's pow(3) function, which actually works on doubles
internally.)
> % perl -e 'print (-12.17)**(0.2), "\n"'
> -12.17
$ perl -wle' print (-12.17) ** (0.2) '
print (...) interpreted as function at -e line 1.
Useless use of exponentiation (**) in void context at -e line 1.
-12.17
>% perl -e 'print ((-12.17)**(0.2)), "\n"'
> nan%
$ perl -wle' print ((-12.17) ** (0.2)), "\n" '
print (...) interpreted as function at -e line 1.
Useless use of a constant in void context at -e line 1.
nan
> Yes, the "\n" isn't getting printed for some reason on the 2nd two examples.
> Bottom line:
>
> -12.17**0.2 ==> -1.65
> (-12.17)**(0.2) ==> -12.17
> ((-12.17)**(0.2)) ==> nan
>
> I have absolutely zero idea what could be going on here... Please help!
Use the -l switch to always get a newline on output from print. Use a unary
'+' to tell print that the leading parenthesis is for precenece.
$ perl -wle' print +(-12.17) ** 0.2 '
nan
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>