Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Use of the m/.../g idiom in list context (formatting MAC addresses)

by grinder (Bishop)
on Jan 17, 2005 at 08:52 UTC ( #422695=snippet: print w/ replies, xml ) Need Help??


I was slurping an internal web page that contains MAC addresses as a 12 char hexadecimal string, e.g. 01fe9d8d68e7.

I wanted to format these strings in a more conventional representation, e.g. 01:fe:9d:8d:68:e7. And when I wrote this snippet, it worked perfectly the first time!

I don't think I could have written this 4 years ago. Reading Perlmonks all this time has improved my Perl immeasurably. So, thanks.

# $mac = '01fe9d8d68e7';
$mac = join( ':' => ($mac =~ /(..)/g) );
Comment on Use of the m/.../g idiom in list context (formatting MAC addresses)
Download Code
Re: Use of the m/.../g idiom in list context (formatting MAC addresses)
by uksza (Monsignor) on Jan 17, 2005 at 13:22 UTC

    You're obviously more experienced, but I just wonder if '=>' is good choice?
    In join function ',' is more readable, isn't it?
    $mac = join( ':' => ($mac =~ /(..)/g) ); $mac = join( ':',($mac =~ /(..)/g) );
    I know it is just convention and individual style, but for 2 first seconds I was wondering "hmm... what is it doing?". And I change "=>" to "," and I understand everything ;-)

      I just wonder if '=>' is good choice?

      A legitimate question. Let me try and explain.

      comma (,) and fat arrow (=>) can be used pretty much interchangeably. The main difference is that fat arrow forces string context on its left hand argument. This is notably useful for hash keys that are matched by \w+, since it means you can do away with single quoting of keys. Less noise, less fuss, less chance of error.

      In the case of join, I can make a pretty strong argument for why the use of fat comma is a good idea as well. Look at the prototype for join:

      perl -le 'print prototype "CORE::join"' # prints '$@'

      That is, join takes a scalar and a list. Which makes sense when you know what join does. It takes the first argument and applies it to the rest of the arguments. Thus, the use of fat arrow here serves as a discriminant to better help you distinguish between the first argument and the rest of the list. In my code, one of the most frequent characters I use to join arrays is... a comma! From a whitespace point of view, I like my commas to cuddle up right behind the preceding argument, which means I find that

      join( ',', '-', '.', '"' );

      much harder to parse than

      join( ',' => '-', '.', '"' );

      Regardless of whether the above is a highly contrived example, just the apostrophe, comma, apostrophe, comma by itself gives me a headache. This doesn't mean you should go overboard and use fat comma anywhere and everywhere, but I think that in the case of join the result is worth it.

      - another intruder with the mooring in the heart of the Perl

        I dislike "cute" uses of fat arrow, because the following code is broken:
        use constant COMMA => ','; ... my $result = join COMMA => @list; # should be: my $result = join COMMA, @list;
        Yes, smart people know that fat arrow autoquotes the left argument. But why should you require your code to be maintained by smart people?

        Be kind to your maintenance programmer. Don't use fat arrow except when you want the quoting feature.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Use of the m/.../g idiom in list context (formatting MAC addresses)
by eyepopslikeamosquito (Canon) on Jan 18, 2005 at 06:47 UTC

    $mac = join( ':' => ($mac =~ /(..)/g) );
    Why not drop the parens?
    $mac = join( ':' => ($mac =~ /../g) );

    BTW, I did something a bit similar today to get all the pod sections with name $sname in file $fname as a string:

    join("", my_slurp_file_function($fname) =~ /^=begin[ \t]+$sname[ \t]*\n(.*?)^=end[ \t]+$sname[ \t]*$/msg);

Back to Snippets Section

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://422695]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-11-23 20:26 GMT
Find Nodes?
    Voting Booth?

    My preferred Perl binaries come from:

    Results (134 votes), past polls