PrePAN

Sign in to PrePAN

Text::FrontMatter::YAML Reads YAML Front Matter text format

Good

Synopsis

#!/usr/bin/perl

# A broader synopsis is in the POD and README.
# Requires YAML::Tiny.

use Text::FrontMatter::YAML;

my $tfm = Text::FrontMatter::YAML->new( fh => main::DATA );
my $metadata = $tfm->get_frontmatter_hashref;

print qq(This is $metadata->{artist} with "$metadata->{title}":\n\n);
print $tfm->get_data_text;


__DATA__
---
title: Midnight Cruiser
artist: Steely Dan
album: "Can't Buy a Thrill"
year: 1972
---
Where are you going, midnight cruiser?
Where is your bounty of fortune and fame?
I am another gentleman loser.
Drive me to Harlem, or somewhere the same.

Description

Reads the "YAML front matter" format like you see in GitHub and Jekyll and various other blog engines. I've found it to be useful in a few personal projects of mine. I have, at various times, used all of the API. It does include tests.

This would be my first module upload, so I'm seeking comments on just about everything, including the module name. ("Text::FrontMatter::YAML" presumes that there might be other "Text::FrontMatter" modules, but I'm not sure that would ever happen.)

Thoughts? Is this worth uploading? Thanks!

Comments

Seems like it could be useful. I think the method names are a bit verbose -- e.g. "get_XXX" vs just "XXX".

In terms of implementation, I personally would probably have slurped the file and split it with a regex rather than reading line by line. Line by line string appends are pretty inefficient. For most files that might not matter a bit, but it's just not how I'd have done it.

I might also suggest not having the file/handle reading bits at all. Just have it take a string. That way, users can decide how they want to load the data. It also pushes the encoding problem back to the users.

Then this is just really focused on doing the front-matter split, YAML decoding and giving some handy accessors to the result. It's really clearly a "text" module then, not a "text and file" module, and I think narrower, more-focused modules tend to be more reusable.

You might also consider whether you want to go the other way -- given hashref and data, return string in front-matter format.
David, thank you very much for your comments. High privilege!

I'd never considered it without the input routines before -- I tend to like "give me a filename and I'll do the rest" APIs. But taking them out makes a lot of sense: I can avoid having to think about file encodings (in this module, at least), and it makes going the other way (which I'd wanted to do) much simpler. I think I'll do it.

Thanks again for the review!
You're welcome.

For example, personally I'd probably use it with [Path::Tiny](http://p3rl.org/Path::Tiny]:

my $tfm = Text::FrontMatter::YAML->new( string => path($whatever)->slurp_utf8 );

Please sign up to post a review.