Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Overriding Subroutines in Package

by Tetramin (Sexton)
on Oct 07, 2001 at 19:15 UTC ( [id://117311]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing on a template module that allows embedding perl code which is then evaled within the modues namespace. As a side effect the templates can override a subroutines like in

*HSP::echo = sub { do something other than it normally would };

Running as CGI there is no problem with it. But embedding perl in HTML makes more sense when running under mod_perl. With mod_perl the modified subroutine stays modified. If someone does

    *HSP::echo = sub{ die };

then the system doesn't work anymore because it allways dies when it wants to print something out.

I know that I can make a subroutine private by saying my $sub = sub{ something private }; but since the templates are eval()ed in the same namespace it would also be possible to undef $sub. Am I lost now? Does someone know of a way how to protect a subroutine from being overridden? Something like this

sub echo : protected { do something }
would be ideal maybe together with die "You are not allowed to override this subroutine".

Replies are listed 'Best First'.
Re: Overriding Subroutines in Package
by merlyn (Sage) on Oct 07, 2001 at 19:21 UTC
    If you allow Perl, you allow mischief. There's really no way around it.

    That's why packages like Template Toolkit have a "mini language" that allows most of what you need in a template without having to expose raw Perl.

    Consider that instead of writing Yet Another Templating Language, please!

    -- Randal L. Schwartz, Perl hacker

      I know Mason and like it very much. Among the mini template tools I like the plain s/\$(\w+)/$values->{$1}/eg; most. So I combine both and it lets me write

      <html> <body><h1>Hello $world</h1></body> </html> <% BEGIN{ $world = "World"; } %>

      Actually no new template language, only a recombination. I wonder if I can override subroutines of Mason, too. Hopefully not.

        In Template, that's
        [% world = "World" -%] <html> <body><h1>Hello $world</h1></body> </html>
        And you don't have to expose Perl. Template is good. use Template!

        -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://117311]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-24 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found