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

Is there any way to create a subroutine whose name contains an embedded ':'?

Why would anyone want to do such a thing, you ask. Well, the Subs style of XML::Parser allows one to process XML while conveniently ignoring items of no interest. Works fine until you are processing an RSS feed with something like <itunes:duration>, which requires sub itunes:duration{}. There are other ways to process this, but stamps foot I want to do it with a sub!

Replies are listed 'Best First'.
Re: sub foo:bar {}?
by Corion (Patriarch) on Aug 13, 2008 at 20:13 UTC

    I think you want to read up on how XML::Parser::Expat does namespaces.

    As an answer to your immediate question,

    no strict 'refs'; *{"itunes:duration"} = sub { ... }

    creates a subroutine named itunes:duration, but you can't call it directly.

Re: sub foo:bar {}?
by AnomalousMonk (Archbishop) on Aug 13, 2008 at 23:36 UTC
    Here's the full incantation:

    perl -wMstrict -e " { no strict 'refs'; local *{ 'foo:bar' } = sub { print qq{hi there $_[0]} }; *{ 'foo:bar' }{CODE}->('sailor'); } " hi there sailor
Re: sub foo:bar {}?
by geoffleach (Scribe) on Aug 13, 2008 at 20:54 UTC
    There's at least one way to do it (tm). Just what I needed, thanks. I may not be able to call it, but XML::Parser does it just fine.
Re: sub foo:bar {}?
by Anonymous Monk on Aug 14, 2008 at 05:44 UTC
    my $poop = {}; $poop->{"anything you want goes here"} = sub {warn(@_)}; $poop->{"anything you want goes here"}->('arg1',2,3);

      Interesting choice of words...

      I'm so adjective, I verb nouns!

      chomp; # nom nom nom

        Its a song :D
Re: sub foo:bar {}?
by Jenda (Abbot) on Aug 14, 2008 at 10:52 UTC

    I know I'm getting annoying, but ... you might want to have a look at XML::Rules. It's built on top of XML::Parser (well, XML::Parser::Expat) and even though it's basicaly an event based parser (though you don't have to look at it that way) the handlers tend to be simpler than the ones you have to write for XML::Parser or other event based parsers.