Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

POD Meditation?

by temporal (Pilgrim)
on Apr 27, 2012 at 20:31 UTC ( #967715=perlquestion: print w/replies, xml ) Need Help??
temporal has asked for the wisdom of the Perl Monks concerning the following question:


I was perusing Meditations, as I am wont to do on a Friday afternoon. I stumbled upon Don’t Repeat Your… version number. It's probably because my brain is already off for the weekend, but I can't seem to wrap my head around the code:

my %const = map { s/\s+//; $_ } map { split /\s*=>\s*/ } grep { /=>/ } map { split /\n/ } << '=cut'; =pod This module uses the following constants: bang_eth => 1 biff => 2 krunch => 3 =cut

I don't do a lot of POD, so I'm pretty lost. I can discern that the code is parsing something that looks like the POD at the end, grabbing the key/value pairs and putting them into a hash. Can anyone shed some (step-by-stepish) light on this?

Strange things are afoot at the Circle-K.

Replies are listed 'Best First'.
Re: POD Meditation?
by JavaFan (Canon) on Apr 27, 2012 at 20:40 UTC
    It's code that is tricking all the POD parsers. See, in your code snippet, there is no POD. POD section are defined as to start with the a '=' (followed by a letter) at the beginning of a line, when perl expects a new statement. But here, at the moment of =pod, perl isn't expecting a new statement. In fact, it's busy finding the end of a here document -- whose end is =cut.

    So, what's happening here is that:

    =pod This module uses the following constants: bang_eth => 1 biff => 2 krunch => 3
    is a here document (it starts with a blank line above =pod, and ends with a blank line), with =cut as terminator. The rest is just parsing this string.

    Why does it appear in the documentation? Because POD parsers actually don't parse POD. They parse anything between ^=\w and ^=cut, without looking at context, assuming to capture all the POD (and nothing but the POD). Most of the time, this heuristic works well, but as with all heuristics, it sometimes will break down, and someone will (a)buse it.

      Wow, that is pretty neat. So the here document (creatively!) grabs the info in the POD for the perl interpreter, while POD parsers will just see the POD block. Allowing you to have a single point of change for those values.

      Thanks, JavaFan!

      Fun note, this code also plays holy hell with the syntax highlighter in my IDE.

      Strange things are afoot at the Circle-K.
        Except that there isn't any POD... ;-) It's the POD parsers that think there's POD hiding inside a string (here doc).

        Fun note, this code also plays holy hell with the syntax highlighter in my IDE.
        I've never bothered with syntax highlighters, and your remark doesn't convince me I was wrong ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967715]
Front-paged by Corion
[Corion]: Eily: Yeah - my French isn't even good to understand jokes, much less to make them :-D
[Corion]: marto: That post is a repost/duplicate of Excel sheet data write issue :-D
[Corion]: Whoops - Merging worksheets in .xls in one Excel sheet
[marto]: Corion, whoops, can the moderation me amended?
[Corion]: marto: We could vote, unconsider and reconsider ;)
[Corion]: "vote keep", I meant ;)
[Corion]: marto: But that aside, I hope you are well!
Corion is slowly acclimatizing to working only four days a week
[marto]: the back pain is still there, it's been a rough week, trying to do building work with a bad back
[Corion]: marto: Ow! That doesn't sound good :-/ The work won't help your back...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2017-09-21 14:40 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (248 votes). Check out past polls.