17
Scalar::Footnote A lightening talk by Steve Purkis July 2007

Scalar::Footnote

Embed Size (px)

DESCRIPTION

A quick overview of Scalar::Footnote, the Perl module available here: http://search.cpan.org/dist/Scalar-Footnote/

Citation preview

Page 1: Scalar::Footnote

Scalar::Footnote

A lightening talk by Steve Purkis

July 2007

Page 2: Scalar::Footnote

Scalar::Footnote

The IdeaSome Beer

Under the hood in 30sQuestions

Page 3: Scalar::Footnote

The Idea

• Boy, you think,

Wouldn’t it be great if I could hide stuff from myself so I didn’t have to look at it?

Well, now you can.

Page 4: Scalar::Footnote

The Idea

• Scalar::Footnote lets you attach hidden scalars to references

• So you don’t have to look at them• No, it doesn’t cat them to /dev/null• (that wouldn’t be very portable anyway)• It tucks them away where no-one can see them

• Using magic.

Page 5: Scalar::Footnote

Synopsis

use Scalar::Footnote;

my $obj = bless {}, 'Foo';

# attach invisible footnote to $obj:

$obj->Scalar::Footnote::set( my_key => ’my footnote' );

# get it back:

$obj->Scalar::Footnote::get( 'my_key' );

# remove it:

$obj->Scalar::Footnote::remove( 'my_key' );

Page 6: Scalar::Footnote

As usual…

• It’s on CPAN• It’s been tested• If you’ve any problems let me know

Page 7: Scalar::Footnote

And really…

• It’s not that useless• No, really.• It’s actually quite useful!

• Ok, so it’s M. Friebe’s idea, not mine.(and maybe a little bit of Piers’)

Page 8: Scalar::Footnote

Scalar::Footnote

The IdeaSome Beer

Under the hood in 30sQuestions

Page 9: Scalar::Footnote

Some Beer

Sorry folks…(but ask Mark about the beer ;-)

A Quick Example

Page 10: Scalar::Footnote

A Quick Exampleuse Data::Dumper;use Scalar::Footnote;

my $obj = bless [qw( foo bar)], 'Foo';$obj->Scalar::Footnote::set( Foo => 'foo note' );print Dumper( $obj );print 'footnote: ' . $obj->Scalar::Footnote::get( 'Foo' );

Prints:

$VAR1 = bless( [

'foo',

'bar'

], 'Foo' );

footnote: foo note

Page 11: Scalar::Footnote

Plays nicely• Keys => don’t overwrite• Refcounted => destroyed as expected

• Pretty much any kind of scalar• Pretty much any kind of ref

Page 12: Scalar::Footnote

Doesn’t like Clone.pm :-/

Attempts to clone objects using Clone.pm die:

Don't know how to handle magic of type \37777777633 at ...

(Other things that use perl’s internals to clone SV’s will croak too.)

Page 13: Scalar::Footnote

Scalar::Footnote

The IdeaSome Beer

Under the hood in 30sQuestions

Page 14: Scalar::Footnote

Under the hood in < 1 min

•So how’s it work?•Well, it’s pretty simple actually…

Page 15: Scalar::Footnote

Under the hood in < 1 min

• You take a $ref, and a $footnote• Then you write some XS to attach a bit of ‘footnote’ magic to the ref…• And a bit more to stick a copy of the footnote in the magic• and presto!

SvRV

Sv

Sv(footnote)

FootnoteMagic

SvPVMG

$ref

$footnote

Sv $footnote(copy)

Page 16: Scalar::Footnote

Scalar::Footnote

The IdeaSome Beer

Under the hood in 30sQuestions

Page 17: Scalar::Footnote

Questions?