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

Hey everybody,
Thanks for all your help on my last problem, Heres my lastest stummper. Bare with me now...
O.k I'm trying to create a random image generator or some such thing like that. Just experimenting really. But I've kind of come to a dead end. When I try run the following code I get two errors(that I can see, because the help scrolls out of the screen in dos):
Useless use of private variable in void context at rand.pl line 38 (#2 +) Useless use of a constant in void context at rand.pl line 25 (#2)
Now this proves to be a wee bit confusing... Take the latter of the two, this refers to the MainLoop; function which, correct me if I'm wrong, tells perl to stop drawing widgets and start looking at the subs.
But I copied and pasted the origonal canvas code that I used from another script which works perfect.
CONFUSING, NO?

Here is the code:
use warnings; use diagnostics; use TK; my (@coLour, $COLOUR, @list, @tmplist, $key); my $width = 600; my $height = 600; my $i = 0; my $randi = &randi(); my $mw=MainWindow->new; my $canvas =$mw->Canvas(width => $width, height => $height)->pack(); for($i = 0; $i < $randi; $i += 1){ my $colour = &colour(); my @tmp = &xy(); $canvas->create('line', $tmp[0], $tmp[1], $tmp[2], $tmp[3], -fill +=> $colour); } MainLoop; sub colour{ @coLour = ('red', 'blue', 'green', 'black', 'orange', 'yellow'); $COLOUR = $coLour[int(rand(6))]; return $COLOUR; } sub xy{ @list = ( 1, 2, 3, 4); @tmplist; $key; foreach $key(@list){ my $temp = rand($width); push(@tmplist, $temp); } return @tmplist; } sub randi{ my $temp = rand(50); return $temp; }
Any help would be greatly appricated, and I know it is probrably a stupid mistake but can't see it.
Thanks in Advance.
All the Best, Eoin...

If everything seems to be going well, you obviously don't know what the hell is going on.

Replies are listed 'Best First'.
Re: Trouble Tracks Tk
by Thelonius (Priest) on May 27, 2003 at 11:12 UTC
    The main problem is that you have "use TK;" when you should have "use Tk;" (lowercase k). You should also have "use strict;". The "useless" warnings come from these lines:
    	@tmplist;
    	$key;
    
    Update: You have a bug, by the way. You really should have my @tmplist; inside the sub xy, so it is initialized each time. Right now you keep adding on to a global array, but you keep using the same four coordinates from the beginning of the array. Since I'm feeling generous today--okay, I'm just avoiding my own work--here is the corrected program, with some stylistic improvements (IMHO):
    use warnings; use diagnostics; use Tk; use strict; my @coLour = ('red', 'blue', 'green', 'black', 'orange', 'yellow'); my $width = 600; my $height = 600; my $randi = &randi(); my $mw=MainWindow->new; my $canvas =$mw->Canvas(width => $width, height => $height)->pack(); for (1 .. $randi) { $canvas->create('line', &xy(), -fill => &randcolour()) } MainLoop; sub randcolour{ return $coLour[rand(@coLour)]; } sub xy{ my @tmplist; foreach (1 .. 4) { push @tmplist, rand($width); } return @tmplist; } sub randi{ return rand(50); }
      AAAAHHHHH. Supid stupid stupid.
      Sorry

      All the Best, Eoin...

      If everything seems to be going well, you obviously don't know what the hell is going on.