Hello Monks!-

I think I need a knowledge recharge on the 'or' versus '||' question, as I'm finding the attached program's results hard to explain.

To start at the beginning, I have a Tk canvas that may or may not have someting on it. I used this syntax...

my @coords = $can->bbox('all') || ( 0, 0, 5, 5);
...to try and have @coords = (0, 0, 5, 5) if the canvas was empty, otherwise have the coordinates of the surrounding box for all its contents.

This didn't work the way I wanted it to, so I came up with this program, partially for fun, and partially to help me understand things better:

use strict; use warnings; use Data::Dumper; use Tk; sub x { return () }; my @list = x() | (1,2,3); print "List:\n",Dumper(\@list), "\n"; my @list0 = x() || (1,2,3); print "List0:\n",Dumper(\@list0), "\n"; my @list1 = x() or (1,2,3); print "List1:\n",Dumper(\@list1), "\n"; my $t=MainWindow->new(); my $c=$t->Scrolled('Canvas')->pack; $c->createArc(5,5,100,100); $t->update; my @list2 = $c->bbox('all') || (1,2,3); print "List2:\n", Dumper(\@list2), "\n"; my @list3 = $c->bbox('all') or (1,2,3); print "List3:\n", Dumper(\@list3), "\n";
My output looks like this:
Useless use of a constant in void context at /tmp/crap line 10. Useless use of a constant in void context at /tmp/crap line 14. Useless use of a constant in void context at /tmp/crap line 14. Useless use of a constant in void context at /tmp/crap line 24. Useless use of a constant in void context at /tmp/crap line 24. Use of uninitialized value in bitwise or (|) at /tmp/crap line 10. List: $VAR1 = [ 3 ]; List0: $VAR1 = [ 1, 2, 3 ]; List1: $VAR1 = []; List2: $VAR1 = [ [ 51, 3, 102, 55 ] ]; List3: $VAR1 = [ 51, 3, 102, 55 ];
I see that the warnings are trying to tell me something, but it's not clear enough to me what that problem(s) is (are).

LIST: was just for fun, but I don't understand why I got what I got

LIST0: I think I understand this one, but then...
LIST1: ...why doesn't this work as well?

LIST2: I really don't understand how I get a list of a list here, and then...
LIST3: ...I'm not sure I understand why this works!

Many thanks in advance for your guidance.

-Craig


In reply to 'or' versus '||' - Unexpected Results by cmv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.