in reply to How to enter a MAC address

Hi snotnose,

Here's something to get you started ...

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$..$/

Replies are listed 'Best First'.
Re^2: How to enter a MAC address
by snotnose (Novice) on Feb 14, 2007 at 22:13 UTC
    Thanks Liverpol, that should work. My problem was I was breaking the address into bytes, then displaying each byte as an entry widget, using a label widget with ':' in it as a separator. I couldn't get the entry widgets to show my numbers as hex, they kept getting converted to decimal.

    This is what I had. I'm pretty new to this stuff, learning perl and tk as I go.

    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";
          "... 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$..$/
        I'm close. How do I validate my entry? I need either 1 or 2 hexadecimal characters. I've got 6 entry widgets, 1 per byte. Each treats it's contents as a string. I'm validating on focusout. So far none of my regex's has worked, the naive version was:
        -validatecommand => sub{$_[0] =~ /\x/},
        Gee, hung up on a regex. Whoodathunk? :)

        I've got 1 other problem. The first entry widget doesn't have focus when it starts, I have to hit tab first. How do I fix this?