@_ uninitialized value

M

mbosogrp

I'm writing a small Gtk2 program to convert between bases. I'm getting
a strange error when i run my code:

Use of uninitialized value in exponentiation (**) at ./radix line 216.
Use of uninitialized value in exponentiation (**) at ./radix line 190.

The strange thing is, sometimes I get it, sometimes I don't. Anyone
have any insight into what's causing it?

Second question. How do you append text to a textbuffer without
overwriting it?

Also, while I'm here, I wouldn't mind some constructive criticism on my
coding style. Any input would be appreciated.

Here is the offending code.

#!/usr/bin/perl -w

use strict;
use Gtk2;

set_locale Gtk2;
init Gtk2;

my $true=1;
#####superfluous variable declarations cut out #####
my $vscrollbar;
my %radixFrameHash =( 'Decimal:'=>$decOut,
'Binary:' =>$binOut,
'Hex:' =>$hexOut,
'Octal:' =>$octOut,
'ASCII:' =>$asciiOut,
);
my $num_rows = 5;
my $num_columns = 10;
my $homogeneous = $true;

$window = new Gtk2::Window("toplevel");
$window->signal_connect("delete_event",sub{Gtk2->main_quit;});
$window->set_default_size(470, 650 );
$window->set_title( "Radix" );

$vbox = new Gtk2::VBox($true,0);

$window->add($vbox);
$hbox = new Gtk2::HBox($false,0);

#Create Entry label and inputbox
$label = new Gtk2::Label('Input: ');
$entry = new Gtk2::Entry();
$entry->signal_connect("activate", \&input_callback, $entry);
$entry->set_editable($true);
$entry->show();
$label->show();

#Start packing that box. hehehe. dirty perl.
$hbox->pack_start($label,$false,$true,5);
$hbox->pack_start($entry,$true,$true,0);
$button = new Gtk2::Button("enter");
$button->signal_connect("clicked",\&input_callback,$button);
$hbox->pack_start($button,$false,$true,3);
$button->show();
$hbox->show();
$button = new Gtk2::Button("clear");
$button->signal_connect("pressed",sub{$entry->set_text("");},$button);
$hbox->pack_start($button,$false,$true,3);
$vbox->pack_start($hbox,$false,$false,0);
$button->show();
$vbox->show();

$vbox->pack_start(&popOutputFrame,$true,$false,0);

#Create Message/Error Text Box
$text_view = new Gtk2::TextView();
$buffer = new Gtk2::TextBuffer();
$buffer = $text_view->get_buffer;

$buffer->set_text("Testing");

$frame = new Gtk2::Frame("Messages");
$frame->show();
#$vscrollbar = new Gtk::VScrollbar( $text->vadj );
$text_view->set_editable ($false);
$text_view->show();
$frame->add($text_view);
$vbox->pack_start($frame,$true,$true,0);
$vbox->show();

$window->show();
main Gtk2;
exit(0);


#-----------------Sub routines--------------------#
#-------------Main Conversion Routine-------------#
sub input_callback{
my $dec = $entry->get_text();
my $virgin = $dec;
my @binary;

if ($dec==0){
unshift @binary,0;

}
else{
while ($dec >= 1 ){

if ($dec==0){
unshift @binary,0;
last;
}
if ($dec==1){
unshift @binary,1;
$dec = $dec / 2;
last;
}
if ($dec % 2){
unshift @binary, 1;
$dec = $dec / 2;
}
else{
unshift @binary, 0;
$dec = $dec / 2;
}


}#while
}#top else
$radixFrameHash{'Binary:'}->set_text("@binary");
$radixFrameHash{'Decimal:'}->set_text("$virgin");
$radixFrameHash{'Octal:'}->set_text(&bin2oct(@binary));
$radixFrameHash{'Hex:'}->set_text(&bin2hex(@binary));
$radixFrameHash{'ASCII:'}->set_text(chr $virgin);
#$text_view->set_buffer ("converting decimal number to binary");

}#input callback
#-------------Main Conversion Routine-------------#


#-------------Create and fill Frames--------------#
sub popOutputFrame{
my $table = new Gtk2::Table( $num_rows, $num_columns, $homogeneous);
my $frame = new Gtk2::Frame("Output");
my $vbox = new Gtk2::VBox($false,0);
my $key;
my $value;
my $temp;
my $j =0;
my $row = \$j; #row contains pointer to value stored in $j
my $spacing = 10;
while(($key,$value) = each %radixFrameHash){
$hbox = new Gtk2::HBox($false,0);
$label = new Gtk2::Label($key);
$radixFrameHash{$key} = new Gtk2::Entry();
$temp = $radixFrameHash{$key};
$label->show();
$temp->show();
$hbox->show();
$table->attach_defaults( $label, 0, 1, $j, $j+1 );
$table->attach_defaults( $temp, 1, 10, $j, $j+1 );
$table->set_row_spacing ($$row, $spacing);
$j++;
}
$frame->add($table);
$table->show();
$frame->show();
return ($frame);
}
#-------------Create and fill Frames--------------#

#-------------Binary 2 Hex--------------#
sub bin2hex{
my $sum;
my $base;
my $loop;
my $hex = " ";
$buffer->set_text("printing b2h inital \@_: @_");
while(@_){
$loop=1;
$sum=0;
while($loop < 9){
$sum += ($loop) * (pop (@_)**2);
$loop *= 2;
}
if($sum > 9){
$sum = chr($sum + 55);
}#unless
$hex = $sum.$hex;
}#outer while
return("0x".$hex);
}#bin2hex
#-------------Binary 2 Hex--------------#



#-------------Binary 2 Octal--------------#
sub bin2oct{
my $sum;
my $base;
my $loop;
my $oct = " ";

$buffer->set_text("printing b20 inital \@_: @_");
while(@_){
$loop=1;
$sum=0;
while($loop < 5){
$sum += ($loop) * (pop (@_)**2);
$loop *= 2;
}
$oct = $sum.$oct;
}#outer while
return("0".$oct);
}
#-------------Binary 2 Octal--------------#
 
J

John W. Krahn

I'm writing a small Gtk2 program to convert between bases. I'm getting
a strange error when i run my code:

Use of uninitialized value in exponentiation (**) at ./radix line 216.
Use of uninitialized value in exponentiation (**) at ./radix line 190.

The strange thing is, sometimes I get it, sometimes I don't. Anyone
have any insight into what's causing it?

Yes, you are using an overly complex algorithm to do something that Perl
provides a function to do for you.

Second question. How do you append text to a textbuffer without
overwriting it?

Use the concatenation operator:

perldoc perlop

Also, while I'm here, I wouldn't mind some constructive criticism on my
coding style. Any input would be appreciated.

Here is the offending code.

#!/usr/bin/perl -w

use strict;

[snip]

#-------------Main Conversion Routine-------------#
sub input_callback{
my $dec = $entry->get_text();
my $virgin = $dec;
my @binary;

if ($dec==0){
unshift @binary,0;

}
else{
while ($dec >= 1 ){

if ($dec==0){
unshift @binary,0;
last;
}
if ($dec==1){
unshift @binary,1;
$dec = $dec / 2;
last;
}
if ($dec % 2){
unshift @binary, 1;
$dec = $dec / 2;
}
else{
unshift @binary, 0;
$dec = $dec / 2;
}


}#while
}#top else
$radixFrameHash{'Binary:'}->set_text("@binary");
$radixFrameHash{'Decimal:'}->set_text("$virgin");
$radixFrameHash{'Octal:'}->set_text(&bin2oct(@binary));
$radixFrameHash{'Hex:'}->set_text(&bin2hex(@binary));
$radixFrameHash{'ASCII:'}->set_text(chr $virgin);
#$text_view->set_buffer ("converting decimal number to binary");

}#input callback
#-------------Main Conversion Routine-------------#


#-------------Main Conversion Routine-------------#
sub input_callback{
my $dec = $entry->get_text();

$radixFrameHash{'Binary:'}->set_text( sprintf '%#b', $dec );
$radixFrameHash{'Decimal:'}->set_text( $dec );
$radixFrameHash{'Octal:'}->set_text( sprintf '%#o', $dec );
$radixFrameHash{'Hex:'}->set_text( sprintf '%#x', $dec );
$radixFrameHash{'ASCII:'}->set_text( chr $dec );
#$text_view->set_buffer ("converting decimal number to binary");
}
#-------------Main Conversion Routine-------------#


perldoc -f sprintf



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top