Re: [Perl-widget-developer] Widget and templates - QED - stringify overloading
Status: Alpha
Brought to you by:
spadkins
From: <ja...@la...> - 2001-06-08 18:40:33
|
On the subject of overloading - not overlording.... I made a sample where I have: Widget_Test ISA Widget_SuperTest ISA Widget_Base The overload syntax that I showed below would go into Widget_Base Try 1 - Widget_Base.pm has: use overload '""' => &display; This has the unfortunate consequence of binding to the Widget_Base::display method right off the bat. No good for subclasses. Try 2 - Widget_Base.pm has: use overload '""' => "display"; This allows for the object instance class to provide the method and will follow ISA if not defined in calling class. MAJOR BONUS FOR ALL The performance difference between: print $widget->display [direct] and print $widget [overload] is a whopping 30-35 times on my machine. direct: 42 wallclock secs (42.48 usr + 0.00 sys = 42.48 CPU) @ 11770.24/s (n=500000) overload: 1 wallclock secs ( 1.35 usr + 0.00 sys = 1.35 CPU) @ 370370.37/s (n=500000) Wow, eh! Jay On Fri, 08 June 2001, Stephen Adkins wrote: > > At 10:10 AM 6/8/2001 -0700, ja...@la... wrote: > >There were a couple of good points about my proposed how to use widgets in > Templating systems. I know the thought of changing a system like Template > Toolkit is unattractive. In the long run I feel a few strategic changes > could make all lives easier. > > > >HOWEVER - GET THIS: > > > >I thought to myself if we do something like: > > $widget=[ a widget instance ]; > > print $widget; > > > >We get a lovely output like: > > Widget::HTML::Thinggy=HASH(0x80903040) > > > >Using the overload (perldoc overload) package we can simply overload the > stringify operator to whatever we wish: > > > >Pacakge Widget::HTML::Thinggy; > > > >sub render_html { > >} > > > >use overload '""' => \&render_html; # or *whatever* > > Good idea. > > >Now print $widget will actually call render_html to create what to print. > Delicious. > > > >I can rip out the funky code I whacked into Template Toolkit and still > just place my widget where I want, calling it by name and seeing its > default rendering (HTML) in all its glory. > > > >We should look at putting this into the Widget::HTML::Base.pm package. > > > > Good idea. > > >use overload '""' => \&render; # Does this inherit properly? Maybe not. > Might need a bit of fancy footwork for subclasses to Widget::HTML::Base > > Give this a try and let us know what works. > > Stephen |