Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Function templates done right

by drix (Initiate)
on Feb 14, 2002 at 18:54 UTC ( #145530=perlquestion: print w/replies, xml ) Need Help??

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

What's the right way to do this here newfangled "function template"? I'm subclassing HTML::Filter and overriding all the methods in an identical fashion. Here's the relevant code:

#!/usr/bin/perl sub fillTemplate { my $funcName = shift; my $index = shift; sub { my $self = shift; sanityCheck($self); ($self->{inTheComment} == 1) ? push @{$self->{killBuffer}}, $_[$index] : $self->SUPER::$funcName(@_); } } for $function ( [ 'declaration', 0 ], [ 'start', 3 ], [ 'text', 0 ], [ 'end', 1 ] ) { *($function->[0]) = fillTemplate($function->[0],$function->[1]); + }

Perl doesn't like me referring to $self->SUPER::$funcName. What's the proper way? Dispensing with the template and writing it all out, the line reads $self->SUPER::start, $self->SUPER::end, etc.

Replies are listed 'Best First'.
Re: Function templates done right
by gav^ (Curate) on Feb 14, 2002 at 19:16 UTC
    According to the docs for HTML::Filter:
    This module is deprecated. HTML::Parser now provides the functionally of HTML::Filter much more efficiently with the the default handler.
    You might find using HTML::Parser makes your life a little easier...


Re: Function templates done right
by vladb (Vicar) on Feb 14, 2002 at 19:49 UTC
    This worked for me:
    use strict; sub foo { print "inside main foo ($_[0])\n"; } sub bar { print "inside main bar ($_[0])\n"; } foreach my $sub (qw(foo bar)) { $main::{$sub}->($sub); }
     inside main foo (foo)
    inside main bar (bar)

    In case of $self->SUPER::$funcName(@_), I guess you could try $self->SUPER{$funcName}->(@_)?

    The trick is you can't just write something like $main::$sub_name(), but have to go the $main::{$sub}->(@args) way instead. This works since $main::{$sub} would return a typeglob for the name '$sub', and when you apply () operator to that typeglob, appropriate subroutine will be invoked.

    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-09-20 00:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found