Upload
steve-purkis
View
488
Download
0
Embed Size (px)
DESCRIPTION
A quick overview of Scalar::Footnote, the Perl module available here: http://search.cpan.org/dist/Scalar-Footnote/
Citation preview
Scalar::Footnote
A lightening talk by Steve Purkis
July 2007
Scalar::Footnote
The IdeaSome Beer
Under the hood in 30sQuestions
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.
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.
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' );
As usual…
• It’s on CPAN• It’s been tested• If you’ve any problems let me know
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’)
Scalar::Footnote
The IdeaSome Beer
Under the hood in 30sQuestions
Some Beer
Sorry folks…(but ask Mark about the beer ;-)
A Quick Example
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
Plays nicely• Keys => don’t overwrite• Refcounted => destroyed as expected
• Pretty much any kind of scalar• Pretty much any kind of ref
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.)
Scalar::Footnote
The IdeaSome Beer
Under the hood in 30sQuestions
Under the hood in < 1 min
•So how’s it work?•Well, it’s pretty simple actually…
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)
Scalar::Footnote
The IdeaSome Beer
Under the hood in 30sQuestions
Questions?