in reply to create arbitrarily named scalars

Okay, I give. You guys convince me. Thanks. 

FWIW my problem was that end user (a comittee, alas) could 
not make up its mind what all to put into a Perl/Tk form by 
way of entry widgets. I was sick and tired of re-writing 
them over every time. So I wanted a way to auto-instantiate 
them from a list.

You convince me to go with hashes, as below. Now I can
re-write the rest of the code to test for definedness of
entry box data by row (if hash exists) and by column (if
value exists). If I am careful, then when next they change
their collective mindes, I can just change the hash values
and the rest should track.

This works for me on both Win2K and NetBSD, and time presses
so I am going with it. Pray this method offends no-one.


#!c:\perl\bin\perl.exe
#!/usr/pkg/bin/perl

use Tk;
use strict;

use vars qw/$mw $col %row_1 %row_2 %row_3/;

# The main window.
$mw = MainWindow->new(-title=>'Fatigue Tracking Sheet');

%row_1 = (
	parent_frame => $mw,
	label_width => 12, entry_width => 10,
	label_1 => 'Alpha 1',
	label_2 => 'Bravo 2',
);
%row_2 = (
	parent_frame => $mw,
	label_width => 12, entry_width => 10,
	label_1 => 'Charlie 3',
	label_2 => 'Delta 4',
	label_3 => 'Echo 5',
	label_4 => 'Foxtrot 6',
);
%row_3 = (
	parent_frame => $mw,
	label_width => 12, entry_width => 10,
	label_1 => 'Hotel 7',
	label_2 => 'India 8',
	label_3 => 'Juliet 9',
	label_4 => 'Kilo A',
);

sub mk_entrybox_row {
	my ($row_ref,) = @_;
	$$row_ref{'row_frame'} = $$row_ref{'parent_frame'}->Frame(
		-relief=>'flat',
		-borderwidth=>5);
	for ($col = 1; $col < 9; $col++){
		last unless (defined($$row_ref{"label_$col"}));
		$$row_ref{"label_$col"} = $$row_ref{'row_frame'}->Label(
			-width=>$$row_ref{"label_width"},
			-text=> $$row_ref{"label_$col"});
		$$row_ref{"entry_$col"} = $$row_ref{'row_frame'}->Label(
			-width=>$$row_ref{"entry_width"},
			-textvariable=>\$$row_ref{"textvar_$col"},-font =>'courier',
			-background=>"white",-foreground=>'blue');	}
	for ($col = 1; $col < 9; $col++){
		last unless (defined($$row_ref{"label_$col"}));
		$$row_ref{"label_$col"}->pack(-side=>'left');
		$$row_ref{"entry_$col"}->pack(-side=>'left',-expand=>1,-fill=>'x');

	}
	$$row_ref{"row_frame"}->pack(-side=>'top',-expand=>0,-fill=>'x');
}

mk_entrybox_row(\%row_1);
mk_entrybox_row(\%row_2);
mk_entrybox_row(\%row_3);

# Backup results and quit program.
sub quit_MainLoop {
  $mw->destroy() if Tk::Exists($mw);
}

MainLoop;

Replies are listed 'Best First'.
(jeffa) 2Re: create arbitrarily named scalars
by jeffa (Bishop) on Jul 27, 2003 at 16:21 UTC
    Untested, but you could store those hashes (%row_1 %row_2 %row_3) in an array:
    use vars qw/$mw $col @row/; @row = ( { parent_frame => $mw, label_width => 12, entry_width => 10, label_1 => 'Alpha 1', label_2 => 'Bravo 2', }, { parent_frame => $mw, label_width => 12, entry_width => 10, label_1 => 'Charlie 3', label_2 => 'Delta 4', label_3 => 'Echo 5', label_4 => 'Foxtrot 6', }, { parent_frame => $mw, label_width => 12, entry_width => 10, label_1 => 'Hotel 7', label_2 => 'India 8', label_3 => 'Juliet 9', label_4 => 'Kilo A', }, ); # and then later: mk_entrybox_row($_) for @row;
    much easier!!

    Oh, by the way, i personally consider it bad style (or false laziness) to wrap your entire writeup in code tags. Try to only use code tags for code (sometimes output) and use <p> tags to separate your paragraphs. See Perl Monks Approved HTML tags for more info.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)