B5603dc8f1e87c251bf9b1d28f31d38f

Converting the return from an SNMP walk into a colon-delimited MAC address.

#!/usr/bin/perl
use strict;
use warnings;

# sample snmp return
my $mac_address = '0x001617479a5e';

print qq{Original MAC Address: $mac_address\n};

# reformat the address as a standard MAC
$mac_address =~ s/\A..//xms;     # remove the 0x
$mac_address =~ s/(..)/$1:/g;    # add a colon between bytes
$mac_address =~ s/:$//;          # remove the trailing :

print qq{Reformatted MAC Address: $mac_address\n};

Refactorings

No refactoring yet !

B5603dc8f1e87c251bf9b1d28f31d38f

mrxinu

July 21, 2008, July 21, 2008 00:33, permalink

No rating. Login to rate!

This occurred to me as I was looking through some other code - unpack() and join().

#!/usr/bin/perl
use strict;
use warnings;

# sample snmp return
my $mac_address = '0x001617479a5e';

print qq{Original MAC Address: $mac_address\n};

# reformat the address as a standard MAC
$mac_address =~ s/^..//;                                   # remove the 0x
$mac_address = join(':', unpack('A2' x 6, $mac_address));  # reformat with colons

print qq{Reformatted MAC Address: $mac_address\n};
0706636fd5e30fa66019d7ffacdb5b11

Marco Valtas

July 21, 2008, July 21, 2008 05:59, permalink

1 rating. Login to rate!

I think there's a million ways to this, I think a simple call will keep the code smaller. The trick here is, first the 'g' flag turns the match operation a kind a loop, if put it in a while will work too. The join function catches all matches and glue with ':' and the group (?:0x) will take care on avoiding match the prefix '0x'.
Hope this helps.

#!/usr/bin/perl
use strict;
use warnings;

# sample snmp return
my $mac_address = '0x001617479a5e';

print qq{Original MAC Address: $mac_address\n};

$mac_address = join(':',($mac_address =~ m/(?:0x)?(\w{2})/g));

print qq{Reformatted MAC Address: $mac_address\n};
B5603dc8f1e87c251bf9b1d28f31d38f

mrxinu

July 21, 2008, July 21, 2008 06:34, permalink

No rating. Login to rate!

@Marco Brilliant! Thanks man. I don't think I'd used clustering much beyond making sure I wasn't causing needless backreference.

55502f40dc8b7c769880b10874abc9d0

dionys.myopenid.com

September 11, 2008, September 11, 2008 09:23, permalink

No rating. Login to rate!
$mac_address = join(':', ($mac_address =~ /(..)/g)[1 .. 6]);
7223c62b7310e164eb79c740188abbda

Xavier Noria

October 7, 2008, October 07, 2008 12:39, permalink

No rating. Login to rate!

Make octets apparent, and remove unneeded capture.

my @octets = substr($mac_address, 2) =~ /../g;
$mac_address = join ':', @octets;
3e54d4042d90094f2a6adba06865974e

Seanbo

September 3, 2009, September 03, 2009 23:25, permalink

No rating. Login to rate!

Why bother with the additional variable declaration for the individual octets?

my $reformatted = join ':', substr($mac_address, 2) =~ /../g;
53e3d1467d76485f11f104efa03ceb93

Alex

November 15, 2010, November 15, 2010 07:43, permalink

No rating. Login to rate!

Using CPAN, we have a module that takes care of the fiddly parsing of MAC addresses for us - though it doesn't handle SNMP style addresses just yet.

#!/usr/bin/env perl
use strict;
use warnings;
use Net::MAC;

my $mac_address = '0x001617479a5e';

$mac_address =~ s/^0x//;
my $mac = Net::MAC->new( 'mac' => $mac_address );
my $reformatted = $mac->as_IEEE;
print "Reformatted MAC address: $reformatted.\n";

Your refactoring





Format Copy from initial code

or Cancel