This is more of a success story than a CUFP, but I had to share.

I've been learning and using Perl more or less since I signed up to PM (so, 18 days or so), though a programmer for some time longer. Still, this was a nice way to validate to myself that I could use it for some "off the cuff" tasks.

A coworker asked me (as the resident Vim guru) if I knew a Vim trick that would quickly convert an Erlang term in an error log he was reading into something readable. For those who don't know, Erlang has a funny way of doing strings: it doesn't! A list of integers which are all printable ASCII (e.g. [65,66,67]) will be pretty-printed as "ABC", and any list where at least one element isn't printable will be rendered as a list instead. That means, for example, that a single Unicode character will make your entire string print as a nice long list of numbers.

Lists are stored as cons cells in Erlang; i.e. not very efficiently for strings, so they also have a "binary string", which more like normally malloced buffer of bytes with known length. They're pretty-printed as <<"hello">>, or again, <<65, 10, 192>> if there are unprintables.

In the situation that unfolded at work, we had the full HTML of a webpage being spewed out in a debug log, except some stranger characters caused the entire thing to print as a very long list of numbers (which didn't do wonders for debugging). Thus my Vim skills were consulted -- and honestly, I had no idea with Vim. But Vim and Perl?

:'<,'>!perl -pe 'chomp; $_ =~ s/[<>]*//g; $_ = join "", map { chr $_ } split ",", $_'

Voilà! HTML came spewing onto the page. I was so happy that I got it on the first try. :) On reflection, map chr, split "," would have worked just as well. Next time!

Anne

Edit: on reflection, how about :'<,'>!perl -ne 'print join "", map chr, split ",", s/[<>]*//gr'? I didn't need chomp after all!


In reply to A little win by anneli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.