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

I need to let the user give me an Ethernet MAC address. FYI, that's usually represented as 6 bytes, printed as hex, with colons between them. For example: de:ca:f0:23:42:ac

Anybody know where I can scrounge a perl/Tk app to let the user input this? It's turning out to be trickier than I thought it would be.

Replies are listed 'Best First'.
Re: How to enter a MAC address
by liverpole (Monsignor) on Feb 14, 2007 at 22:00 UTC
    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$..$/
      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$..$/
Re: How to enter a MAC address
by andyford (Curate) on Feb 14, 2007 at 21:39 UTC

    What have you tried that's failing?

    non-Perl: Andy Ford

Re: How to enter a MAC address
by moklevat (Priest) on Feb 14, 2007 at 22:00 UTC
    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?
      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.