SlideShare a Scribd company logo
1 of 14
Constructive Destructor Use in Object-Oriented Perl Terrence Brannon http://www.metaperl.org [email_address]
Vanilla Perl OO Destructors a class with destructor package X;  use strict; use warnings; sub new {  bless {}, __PACKAGE__;  }  sub DESTROY {  my($self)=@_;  print "$self is dead"; } program and output print "before birth"; { my $x = X->new; } print "post-mortem"; before birth X=HASH(0x10023ce8) is dead post-mortem
Moose Destructors program and output before birth X=HASH(0x10654b68) is dead post-mortem print "before birth"; { my $x = X->new; } print "post-mortem"; package X;  use Moose; sub DEMOLISH { my($self)=@_; print "$self is dead"; }
So Far.. So what?! Just a bit more background...
XML::Writer Code and output $writer->startTag(&quot;level1&quot;); $writer->startTag(&quot;level2&quot;);  $writer->startTag(&quot;level3&quot;); $writer->endTag();  $writer->endTag();  $writer->endTag(); Code critique ,[object Object],[object Object],<level1> <level2> <level3></level3> </level2> </level1>
XML::Writer has no API support for  element  nesting <level1> 45 </level1> use XML::Writer; my $writer = XML::Writer->new; $writer->dataElement(&quot;level1&quot;, 45);
XML::Writer::Nest – Phase 1 my $writer = new XML::Writer; my $level1 = XML::Writer::Nest->new (tag => 'level1', writer => $writer ); my $level2 = $level1->nest ('level2'); my $level3 = $level1->nest('level3'); ,[object Object],[object Object],[object Object]
XML::Writer::Nest Phase 2 my $writer = new XML::Writer; { my $level1 = XML::Writer::Nest->new(tag => 'level1', writer => $writer ); {  my $level2 = $level1-> nest ('level2');  {  my $level3 = $level2->nest('level3'); } }  }  ,[object Object],[object Object],[object Object]
if-then, nesting, loops ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
more stack, less vars use XML::Writer::Nest; my $writer = new XML::Writer; { local $xml = XML::Writer::Nest->new(tag => 'level1', writer => $writer ); {  local $xml = $xml-> nest ('level2');  {  local $xml = $xml->nest('level3'); } }  }
Implementation of XML::Writer::Nest package XML::Writer::Nest; use Moose; has 'tag'  => (isa => 'Str', is => 'ro', required => 1); has 'attr'  => ( isa => 'ArrayRef[Maybe[Str]]',  is => 'ro',  default => sub { [] }  );  has 'writer' => (isa => 'XML::Writer', is => 'ro', required => 1);
Implementation of XML::Writer::Nest sub BUILD { my($self)=@_; my @attr = defined($self->attr) ? @{$self->attr} : () ; $self->writer->startTag($self->tag, @attr); } sub DEMOLISH { my($self)=@_; $self->writer->endTag(); }
Related XML Generation Work ,[object Object],[object Object],my $h = HTML::Element->new_from_lol ( [ level1 => [ level2 => [ 'level3'] ], ] ); warn $h->as_HTML('<>&', &quot;&quot;); # http://pastie.org/pastes/738006
For more complex examples... HTML::Element::Replacer  is a more complex example of &quot;constructive destruction&quot; used to simplify use of HTML::TreeBuilder  <http://search.cpan.org/dist/HTML-Element-Replacer/>

More Related Content

What's hot

優しいWAFの作り方
優しいWAFの作り方優しいWAFの作り方
優しいWAFの作り方
techmemo
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5
Yusuke Wada
 

What's hot (19)

優しいWAFの作り方
優しいWAFの作り方優しいWAFの作り方
優しいWAFの作り方
 
How to develop modern web application framework
How to develop modern web application frameworkHow to develop modern web application framework
How to develop modern web application framework
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
Perl
PerlPerl
Perl
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Asynchronous programming patterns in Perl
Asynchronous programming patterns in PerlAsynchronous programming patterns in Perl
Asynchronous programming patterns in Perl
 
Perl5i
Perl5iPerl5i
Perl5i
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 
Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5Twib in Yokoahma.pm 2010/3/5
Twib in Yokoahma.pm 2010/3/5
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Getting out of Callback Hell in PHP
Getting out of Callback Hell in PHPGetting out of Callback Hell in PHP
Getting out of Callback Hell in PHP
 
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
 
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Metadata-driven Testing
Metadata-driven TestingMetadata-driven Testing
Metadata-driven Testing
 

Viewers also liked (8)

Caries
CariesCaries
Caries
 
Ushahidi VOA Hong Kong
Ushahidi VOA Hong KongUshahidi VOA Hong Kong
Ushahidi VOA Hong Kong
 
Repair And Service
Repair And ServiceRepair And Service
Repair And Service
 
Presentation
PresentationPresentation
Presentation
 
Cmu
CmuCmu
Cmu
 
Reading Assessment English by Zaid Ayoub
Reading Assessment English by Zaid AyoubReading Assessment English by Zaid Ayoub
Reading Assessment English by Zaid Ayoub
 
Hacia donde va la Gestión de Recursos Humanos
Hacia donde va la Gestión de Recursos HumanosHacia donde va la Gestión de Recursos Humanos
Hacia donde va la Gestión de Recursos Humanos
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
 

Similar to Constructive Destructor Use

Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
ddn123456
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
Attila Balazs
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked about
Tatsuhiko Miyagawa
 

Similar to Constructive Destructor Use (20)

Moose (Perl 5)
Moose (Perl 5)Moose (Perl 5)
Moose (Perl 5)
 
Moose talk at FOSDEM 2011 (Perl devroom)
Moose talk at FOSDEM 2011 (Perl devroom)Moose talk at FOSDEM 2011 (Perl devroom)
Moose talk at FOSDEM 2011 (Perl devroom)
 
Php2
Php2Php2
Php2
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Perl from the ground up: objects and testing
Perl from the ground up: objects and testingPerl from the ground up: objects and testing
Perl from the ground up: objects and testing
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
 
全裸でワンライナー(仮)
全裸でワンライナー(仮)全裸でワンライナー(仮)
全裸でワンライナー(仮)
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked about
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
 
Top 10 php classic traps php serbia
Top 10 php classic traps php serbiaTop 10 php classic traps php serbia
Top 10 php classic traps php serbia
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

Constructive Destructor Use

  • 1. Constructive Destructor Use in Object-Oriented Perl Terrence Brannon http://www.metaperl.org [email_address]
  • 2. Vanilla Perl OO Destructors a class with destructor package X; use strict; use warnings; sub new { bless {}, __PACKAGE__; } sub DESTROY { my($self)=@_; print &quot;$self is dead&quot;; } program and output print &quot;before birth&quot;; { my $x = X->new; } print &quot;post-mortem&quot;; before birth X=HASH(0x10023ce8) is dead post-mortem
  • 3. Moose Destructors program and output before birth X=HASH(0x10654b68) is dead post-mortem print &quot;before birth&quot;; { my $x = X->new; } print &quot;post-mortem&quot;; package X; use Moose; sub DEMOLISH { my($self)=@_; print &quot;$self is dead&quot;; }
  • 4. So Far.. So what?! Just a bit more background...
  • 5.
  • 6. XML::Writer has no API support for element nesting <level1> 45 </level1> use XML::Writer; my $writer = XML::Writer->new; $writer->dataElement(&quot;level1&quot;, 45);
  • 7.
  • 8.
  • 9.
  • 10. more stack, less vars use XML::Writer::Nest; my $writer = new XML::Writer; { local $xml = XML::Writer::Nest->new(tag => 'level1', writer => $writer ); { local $xml = $xml-> nest ('level2'); { local $xml = $xml->nest('level3'); } } }
  • 11. Implementation of XML::Writer::Nest package XML::Writer::Nest; use Moose; has 'tag' => (isa => 'Str', is => 'ro', required => 1); has 'attr' => ( isa => 'ArrayRef[Maybe[Str]]', is => 'ro', default => sub { [] } ); has 'writer' => (isa => 'XML::Writer', is => 'ro', required => 1);
  • 12. Implementation of XML::Writer::Nest sub BUILD { my($self)=@_; my @attr = defined($self->attr) ? @{$self->attr} : () ; $self->writer->startTag($self->tag, @attr); } sub DEMOLISH { my($self)=@_; $self->writer->endTag(); }
  • 13.
  • 14. For more complex examples... HTML::Element::Replacer is a more complex example of &quot;constructive destruction&quot; used to simplify use of HTML::TreeBuilder <http://search.cpan.org/dist/HTML-Element-Replacer/>