The our is lexically scoped. Move it out of the if.

I thought this meant that the variable $bin1 I declared was not global

Quite the opposite, our means you want to use the global variable. There's no reason to use our here. There's almost never a reason to use it. You want to create a variable, so you should be using my.

my $bin1; if ($binc1 != 0) { $bin1 = 1; } else { $bin1 = 0; }

The else clause is executed when $binc1 isn't not equal to zero. Avoiding the double negative:

my $bin1; if ($binc1 == 0) { $bin1 = 0; } else { $bin1 = 1; }

But you could just simplify instead:

my $bin1; if ($binc1) { $bin1 = 1; } else { $bin1 = 0; }

Simplified further:

my $bin1 = $binc1 ? 1 : 0;

Since the purpose is to format the bit for output, we actually want a string:

my $bin1 = $binc1 ? '1' : '0';
or
print "The number in binary is: ", ($binc1 ? '1' : '0'), "\n";

Update: Fixed problem identified in reply.


In reply to Re: Need a little coding help by ikegami
in thread Need a little coding help by samaniac

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.