samaniac has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I've heard you're the ones to go to if in need of help with anything perl-related. Well, I have a question for you. First look at this part of my code:

#!/usr/bin/perl #intconv2.plx use warnings; use strict; print "Please enter a normal decimal number less than 256: ", "\n"; my $dec; $dec=<STDIN>; my$binc1=$dec&128; if ($binc1!=0) { our$bin1=1; } else { our$bin1=0; } print"The number in binary is: $bin1 \n";

I know this isn't a correct way of converting from decimal to binary but as I said this is only part of my code. My problem is that I get an error saying "Variable "$bin1" is not imported at C:\PerlScripts\intconv2.plx line 90. Global symbol "$bin1" requires explicit package name at C:\PerlScripts\intconv2.plx line 90. I thought this meant that the variable $bin1 I declared was not global and I don't know how to fix this, unless that's not even the problem. Please help me.

Replies are listed 'Best First'.
Re: Need a little coding help
by ikegami (Patriarch) on Nov 25, 2010 at 18:21 UTC

    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.

      Thanks a lot man, just one thing, in the avoiding negative:

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

      Shouldn't it be the opposite? Like:

      if($binc1){ $bin1=1; } else{ $bin1=0; }
        Fixed.
Re: Need a little coding help
by shawnhcorey (Friar) on Nov 25, 2010 at 23:36 UTC

    Try:

    perl -e'printf"%08b\n",$_ for@ARGV' 200 128 77

    See `perldoc -f sprintf` for details.

Re: Need a little coding help
by umasuresh (Hermit) on Nov 25, 2010 at 17:58 UTC
    Try these options:
    1. I don't see a space between our and $bin1 in whichever line it is in. Try  our $bin1
    2. Check my previous post on using debugging tool Re: RegEx and Packaging Name Problem
    Good Luck!
Re: Need a little coding help
by biohisham (Priest) on Nov 25, 2010 at 22:52 UTC
    pack and unpack can just let you do what you want, in Perl there is not like an intrinsic way to convert between binary and decimal but pack and unpack can allow you to do the interchanging...
    my $decimal = 4; $binary=unpack("B32", pack("N",$decimal)); print $binary; print "\n"; my $newdecimal=unpack("N",pack("B32",$binary)); print $newdecimal; # 32 in the template means the binary number will have 32 places


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
Re: Need a little coding help
by roc (Sexton) on Nov 29, 2010 at 16:02 UTC

    is it get solved.... i'm wondering because he's using windows(Variable "$bin1" is not imported at C:\PerlScripts\intconv2.plx), but he mentioned that #!/usr/bin/perl ... i hope its the default path in linux.... so it could be the problem...but i'm not sure... if not can any one clear my doubt...