use strict;
use warnings;
use Tk;
my $mac_addr = "";
my $mw = new MainWindow(-title => 'Enter a MAC address');
my $fr = $mw->Frame()->pack(-expand => 1, -fill => 'both');
my $lb = $fr->Label(-text => "Enter MAC address ")->pack(-side => 'lef
+t');
my $en = $fr->Entry(-textvar => \$mac_addr)->pack(-side => 'left');
my $bt = $fr->Button(-text => 'Validate', -background => 'green');
$bt->configure(-command => sub { validate() });
$bt->pack(-side => 'left');
MainLoop;
sub validate {
my $valid = '[0-9a-f]' x 2;
if ($mac_addr =~ /($valid:){5}$valid/i) {
print "Address '$mac_addr' is VALID\n";
} else {
print "Address '$mac_addr' is INVALID\n";
}
}
Does that help you?
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
| [reply] [d/l] |
use strict;
use Tk;
# Take a 6 element array and make it string.
sub makeString {
my @data = @_;
my $result = sprintf "%02x:%02x:%02x:%02x:%02x:%02x",
$data[0], $data[1], $data[2], $data[3], $data[4], $data
+[5];
return $result;
}
sub doit {
my @addy = @_;
my $mw = MainWindow->new();
# Show the current address
my $current = makeString(@addy);
$mw->Label(-text => $current)->pack(-side => 'top');
# now build some entry widgets to change it
for (my $i = 0; $i < 6; $i++) {
$mw->Entry(-textvariable => \$addy[$i], -width => 3)
->pack(-side => 'left');
$mw->Label(-text => ':', -width => 1)->pack(-side => 'left');
}
$mw->Button(-text => "Done", -command => sub {$mw->destroy;})
->pack(-side => 'bottom');
MainLoop;
return(@addy);
}
my @foo = (0x10, 0x22, 0x33, 0x4b, 0x5f, 0x6d);
print "Starting with: " . makeString(@foo) . "\n";
my @bar = doit(@foo);
print "Ended with: " . makeString(@bar) . "\n";
| [reply] [d/l] |
"... learning perl and tk as I go"
Well, I'm impressed. Learning Perl takes enough effort (though mostly fun :-D), and Tk is no simple discipline to master. Good for you!
If you want to display hexadecimal, you could just use sprintf to reformat each hex pair before the assignment:
$mw->Entry(-textvariable => [ sprintf "%02x", $addy[$i] ], -width => 3
+)
->pack(-side => 'left');
Another minor suggestion: check your loop variable so you don't print an extra ':' after the final byte:
if ($i < 5) {
$mw->Label(-text => ':', -width => 1)->pack(-side => 'left');
}
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
| [reply] [d/l] [select] |
| [reply] |
Just so that you don't have to deal with figuring all of the possible ways that a user might enter a MAC address, you might consider offering 6 separate two character wide text boxes to accept input. What have you tried so far? | [reply] |
ug, I hate entering IP addresses in Windows's "[box].[box].[box].[box]". Can't paste. I'd go with a straight text box, and validate the input.
| [reply] |