By David Krupička


2019-06-12 07:30:29 8 Comments

I am just wondering what happens with that piece of code. Why the result is incorrect only when printed directly, why is the newline ignored?

[email protected]_09:22 AM: perl
print 2 >> 1, "\n";
print 2 & 2, "\n";
print (2 & 2) >> 1, "\n";
1
2
[email protected]_09:22 AM: perl
$a = (2 & 2) >> 1;
print "$a\n";
1

2 comments

@zdim 2019-06-12 07:37:36

When you print it with warnings it becomes clear(er)

perl -we'print (2 & 2), "\n"'

says

print (...) interpreted as function at -e line 1.
Useless use of a constant ("\n") in void context at -e line 1.

It works out print(2&2) as a function call to print, which prints 2, and then it keeps evaluating the comma operator, with "\n" in void context next, which it warns us about.

With >> 1 also there, the return 1 of print(2&2) (for success) is bit shifted to 0, which disappears into the void, and we get another "Useless use of ... in void context."

One fix is to add a + to let the interpreter know that ( is meant for an expression

perl -we'print +(2 & 2) >> 1, "\n"'

Or, make a proper call to print, with parenthesis around the whole thing

perl -we'print((2 & 2) >> 1, "\n")'

Both print a line with 1.

This is mentioned in print, and more fully documented in Terms and List operators and in Symbolic Unary operators, both in perlop.

@vlumi 2019-06-12 07:39:26

Or you could add parenthesis around the parameters, too, of course: print((2 & 2) >> 1, "\n");

@zdim 2019-06-12 07:43:54

@vlumi Yes, thank you -- just edited

@David Krupička 2019-06-13 08:32:14

Many thanks for the explanation!

@zdim 2019-06-13 18:23:11

@DavidKrupička Welcome :) Let me know if questions come up

@choroba 2019-06-12 07:38:22

Perl interprets the parentheses as function arguments marker, as you can verify with

perl -MO=Deparse,-p -e 'print (2 & 2) >> 1'

Output:

(print(2) >> 1);

The canonical way is to precede the left parenthesis with a +:

print +(2 & 2) >> 1

@ikegami 2019-06-12 12:18:41

Well, it's more idiomatic to add the omitted parens. But yeah, the idiomatic way of leaving them off is to use a unary +.

@ikegami 2019-06-12 12:19:34

It's worth noting that unary + work with any kind of values, not just numbers. It literally has no effect on the value passed to it.

@David Krupička 2019-06-13 08:32:31

Many thanks for the explanation!

@David Krupička 2019-06-13 08:38:52

I like the -MO=Deparse suggestion, but if you don't mind, I marked zdim's post as the answer, because it is slightly more detailed.

Related Questions

Sponsored Content

7 Answered Questions

[SOLVED] Why does modern Perl avoid UTF-8 by default?

  • 2011-05-28 15:12:36
  • w.k
  • 96396 View
  • 552 Score
  • 7 Answer
  • Tags:   perl unicode utf-8

9 Answered Questions

3 Answered Questions

[SOLVED] perl false values are not printed

  • 2017-11-12 19:37:10
  • Bram vanDem
  • 110 View
  • 0 Score
  • 3 Answer
  • Tags:   perl

1 Answered Questions

[SOLVED] Perl will not print multiple variables without a newline

  • 2017-05-09 14:19:38
  • guidotex
  • 336 View
  • 2 Score
  • 1 Answer
  • Tags:   perl

3 Answered Questions

[SOLVED] What does \x do in print

  • 2015-04-12 19:12:11
  • Blacklabel524
  • 565 View
  • 1 Score
  • 3 Answer
  • Tags:   perl

1 Answered Questions

[SOLVED] Side-effect of Perl's print function

  • 2014-06-25 15:10:45
  • ajwood
  • 205 View
  • 4 Score
  • 1 Answer
  • Tags:   perl

2 Answered Questions

[SOLVED] Why is my string still empty?

  • 2013-10-21 00:50:12
  • Timmy
  • 70 View
  • 1 Score
  • 2 Answer
  • Tags:   perl

2 Answered Questions

[SOLVED] What does "$$q" mean in Perl?

  • 2013-04-06 20:02:15
  • user2870
  • 479 View
  • 5 Score
  • 2 Answer
  • Tags:   perl

2 Answered Questions

[SOLVED] What means "not running under some shell" in Perl scripts?

  • 2012-02-04 19:18:26
  • Беров
  • 626 View
  • 5 Score
  • 2 Answer
  • Tags:   perl cpan

4 Answered Questions

[SOLVED] Why is it printing all three printing statements?

  • 2011-05-16 07:51:10
  • Man
  • 309 View
  • 10 Score
  • 4 Answer
  • Tags:   perl

Sponsored Content