Doing Nothing

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Doing Nothing as PDF for free.

More details

  • Words: 576
  • Pages: 20
Doing Nothing Marcel Grünauer (hanekomu) [email protected] Zentraler Informatikdienst der Universität Wien Vienna.pm Tech Meeting, 6th October 2008 Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Central IT Services of the University of Vienna Registry software for the .at zone (also used by .no, .ie, .nl, .au) Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Tatsuhiko Miyagawa “20 modules I haven’t yet talked about”

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Tatsuhiko Miyagawa

10 “20 modules I haven’t yet talked about”

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

➊ Class::Null

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

References may be undef. They need to be checked to ensure they are not undef before invoking any methods on them. This tends to make code less readable. Instead of using an undef reference to convey absence of an object, you use a Null object – an object which implements the expected interface, but whose method body is empty.

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

What the hell does that mean?

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

The usual way sub do_it { my $self = shift; if (defined (my $log = $self->log)) { $log->some_method(...); } ... if (defined (my $log = $self->log)) { $log->some_method(...); } ... if (defined (my $log = $self->log)) { $log->some_method(...); } }

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

There are three problems.

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

(1) The code becomes less readable. Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

(2) You might forget to check definedness. Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

(3) It is boring.

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Solution: Class::Null sub init { my $self = shift; $self->log(Class::Null->new); } sub do_it { my $self = shift; $self->log->some_method(...); ... $self->log->some_method(...); ... $self->log->some_method(...); }

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

A very efficient way of doing nothing.

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Class::Null concept package Class::Null; sub new { our $singleton ||= bless {}, shift } sub AUTOLOAD { our $singleton }

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Method Chaining sub AUTOLOAD { our $singleton } sub do_it { my $self = shift; ... $self->log->foo(...)->bar(...)->baz; ... }

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Boolean Context sub do_it { my $self = shift; ... if ($self->log->foo(...)) { ... } ... }

The null object returns 0 (a false value) in boolean context.

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Class::Null complete package Class::Null; use warnings; use strict; use overload 'bool' '""' '0+' fallback

=> => => =>

sub { 0 }, sub { '' }, sub { 0 }, 1;

sub new { our $singleton ||= bless {}, shift } sub AUTOLOAD { our $singleton }

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Questions?

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Thank you! http://search.cpan.org/dist/Class-Null/ http://www.pdfcoke.com/hanekomu http://hanekomu.at/blog

Marcel Grünauer Wednesday, May 27, 2009

Vienna.pm Tech Meeting, 2. Juni 2009

Related Documents

Doing Nothing
May 2020 13
Nothing
May 2020 40
Nothing
December 2019 42
Nothing
October 2019 70
Nothing
October 2019 48