@_ 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.

this is the line of code that generates the warning in both instances:
$sum += ($loop) * (pop (@_)**2);


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--------------#
 
F

Fabian Pilkowski

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.

this is the line of code that generates the warning in both instances:
$sum += ($loop) * (pop (@_)**2);

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

Within your sub bin2oct() you have two encapsulated while-loops. The
outer one tests if there is *one* element in array @_. The inner one
pops *three* elements from @_. Thats why your programm prints no
warning if you pass 3, 6, 9, 12 etc elements (binary digits) to your
sub. In other cases pop will return undef (if @_ is already empty) and
the exponentation will cause an error. Try out

$sum += $loop * ( pop @_ || 0 ** 2 );

to use 0 whenever pop returns undef. Pay attention at the operators
precedence or, to be on the safe side, write it as

$sum += $loop * ( (pop(@_)||0) ** 2 );

Btw, I think your subs bin2oct() and bin2hex() could be smarter. I would
not write down such an *self-made* algorithm rather than:

sub bin2oct {
my $bin = join '', @_;
my $dec = oct( "0b$bin" );
return sprintf '0%o', $dec;
}

sub bin2hex {
my $bin = join '', @_;
my $dec = oct "0b$bin";
return sprintf '0x%X', $dec;
}
Second question. How do you append text to a textbuffer without
overwriting it?

The Docs on CPAN refer to the "GTK+ Reference Manual" where the
corresponding C API is described. Try to read

http://developer.gnome.org/doc/API/2.0/gtk/GtkTextBuffer.html

to get an overview about "GtkTextBuffer". The method you're currently
using seems to be gtk_text_buffer_set_text(). Perhaps you can use
$buffer->insert() or $buffer->insert_at_cursor(). I think it's not too
hard for you to try out which parameters these methods are expecting in
the perl interface for GTK+.

regards,
fabian
 
M

mbosogrp

Thank you for the reply. You've been extremely helpful. That link will
come in very handy. Up till now I've been using painfully incomplete
perl gtk2 documentation. It left a lot to be desired. That link is a
breath of fresh air.

Thanks again,
Mboso
 
E

Eric Schwartz

Thank you for the reply. You've been extremely helpful. That link will
come in very handy. Up till now I've been using painfully incomplete
perl gtk2 documentation. It left a lot to be desired. That link is a
breath of fresh air.

Next time, it would really help if you quoted a bit of context here.
I am tangentially interested in this thread, as I have an idea for a
GTK+ app I'd like to write in Perl, but I have no idea what link
you're referring to.

Not everybody gets all the articles at the same time in USENET, you
know.

-=Eric
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top