Dear Monks,
In the pursuit of Laziness, I am attempting to wrap a sub with another sub, and thought that manipulating the symbol table of the package would be the way to go. Since it should only be a couple of lines of code, I'd rather not use a module. Besides, it's nice to know how to do this.
The sub I am wrapping is CGI's param(), and the reason is so that I can call Encode::decode_utf8() on everything returned from param(). Instead of modifying hundreds of individual calls to param(), I'll just hook param() in one place. But I can't quite get it to work, and I couldn't find very many clear explanations with examples here in the monastery.
My code so far:
no strict 'refs'; # get a copy of the real sub our $real_param = *{"CGI::param"}{CODE}; # automagically create a new sub that contains the old sub *{"CGI::_real_param"} = \&{$real_param}; # override param() *{"CGI::param"} = sub { return Encode::decode_utf8(shift->_real_param(@_)); };

But it doesn't work. I'm finding the syntax a bit tricky for getting a typeglob out of CGI's symbol table hash and into a scalar reference, and then putting that sub reference into a new typeglob that can be called as a sub. What am I doing wrong and what is a better way?
Thanks!

In reply to How to change the symbol table to wrap a sub in another sub ? by Withigo

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.