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

Brethern

How can I tell if a string is a pragma? I'm scanning perl code for dependencies, and want to ignore use xxx when xxx is a pragma. I was advised in the Chatterbox:

pragma's are traditionally all lowercase but I think you are likely to only find good rules of thumb not exact assurances
Problem is that, although modules are traditionally capitalized, a bunch of them in the codebase I'm looking at are not.

I'm extending Module::Dependency. For my present purpose (building a set of graphs of a large codebase) it may be enough to make a list of the most popular pragmas (pragmae?) and just exclude those. However, I'd like to actually post my updates back to CPAN.1 If I do that, I'd like a comprehensive solution.

throop


1Whether I actually get to do that is a question in process. I work for a large company. My coding labor is billed to a client. I asked my manager and a company lawyer shaman whether it was proper for me to post improvements back to CPAN. They're still busy reading bird entrails and casting bones, trying to decide.

Replies are listed 'Best First'.
Re: Is it a pragma or a module?
by ikegami (Patriarch) on Dec 14, 2006 at 17:04 UTC

    You can't. Sometimes, you can't even tell by reading the docs! There's a couple of signs, however.

    • Does the module use %^H or $^H? If so, almost definitely.
    • Does the module use %^H or $^H? If not, it could still be.
    • Does the module (or a parent) have an unimport method? If so, it's almost certainly a pragma.
    • Does the module (or a parent) have an unimport method? If not, it's definitely not a pragma.
    • Is the module name lowercase? If so, it has a higher chance of being a pragma.
    • Is the module name lowercase? If not, it has a lower chance of being a pragma.
    • Is the module a core module with a lowercase name? If so, then it's a pragma.
    • Is the module a core module? If not, it unlikely to be a pragma.

    Based on the last two points, you'll be almost guaranteed to be correct if you simply check the module name against the list of core modules that are pragmas.

    Update: My post and the list Mutant posted discuss lexically scoped pragmas. The list I posted above define the term "pragma" more loosely. I'm not sure which one you want.

      >I'm not sure which [list] you wanted
      Probably the list you posted. It's a question of what the user will want to see — why does anybody want to see a graphic map of the dependencies between a bunch of modules, anyways? Usually, to answer the question "If I muck around with this module, what else gets broken?" Hardly anybody is going to be changing the strict or warnings code. A bunch of map lines showing that all the modules depend on them is unhelpful. So I want to be able to exclude (at least as a default) all the modules that people are unlikely to be mucking with. Pretty much everything in your 'looser' def of pragma fills that bill.

      thanks,
      throop

Re: Is it a pragma or a module?
by Mutant (Priest) on Dec 14, 2006 at 16:59 UTC
    Since there aren't many pragmas (see the full list) you might be able to just hardcode the list in.

    (Actually, not sure that list is complete, because it doesn't contain 'warnings', but you can probably find a list fairly easily).
Re: Is it a pragma or a module?
by perrin (Chancellor) on Dec 14, 2006 at 17:56 UTC
    Unfortunately, a number of people have broken the conventions and released their non-pragma modules to CPAN with all lowercase names, just because they like the way it looks. That means you pretty much have no choice but to hard-code the list of real pragmas.
      ...and then put it on CPAN as a pragma, or would that be a module?
        Only code that is part of the core and maintained as part of perl itself can be properly called a pragma. Otherwise it's just a module getting uppity.
Re: Is it a pragma or a module?
by halley (Prior) on Dec 14, 2006 at 20:39 UTC

    It's kind of like deciding what is a medicine and what is simply an herbal remedy. A pragma, in concept, means that it changes the actual core Perl language in some way so that it is better able to work with you on expressing a solution. A non-pragma module, in concept, offers you a set of tools separate from the actual Perl language, with which you build your solution. Clear enough for you? Sometimes, me neither.

    --
    [ e d @ h a l l e y . c c ]

      halley: I like your explanation, although I'd add that it needs to be delivered with core perl. Otherwize, all modules using source filters would become additional pragmata.

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

        I don't agree that being in the core perl distribution is a trait of pragmatic modules.

        See my use deprecated; node.

        --
        [ e d @ h a l l e y . c c ]

Re: Is it a pragma or a module?
by crashtest (Curate) on Dec 14, 2006 at 22:29 UTC

    ... list of the most popular pragmas (pragmae?) and ...
    It's Greek. The plural is "pragmata". The "ae" ending forms a plural for a certain class of Latin words.

Re: Is it a pragma or a module?
by jbert (Priest) on Dec 15, 2006 at 14:09 UTC
    If I were in your shoes (writing a module to recursively scan modules and wanting to ignore some), I'd provide four modes of operation:
    1. "Ignore commonly used" - this is where you hardcode a list of modules/pragmata to exclude from your processing.
    2. "Ignore supplied list only" - allow the caller to replace the list of modules to ignore
    3. "Ignore commonly used list plus supplied list" - allow the caller to add to the list of modules to ignore.
    4. Process everything (a special case of 2 - where the ignore list is empty)
    And you might want to make the "supplied list" accessible as part of your API too, so that calling code can make educated decisions about what to do.