Hi All,
  When I posted to PerlMonks recently about using map on a basic data 
file (key = value) they said to use YAML encase I later want to add more 
complicated data (this is probably the 5th time PerlMonks people of told 
me to use YAML). This coupled with Alex's recommendation to use JSON, 
and finding that I could in fact make files that were both valid YAML 
and JSON, it seem time to finally give in a give it a go.
  When I looked at YAML.pm is was surprised to see it export functions 
called Dump and Load by default. I'm using this from within cgi-app and 
I really don't like the idea of those names messing with other methods. 
I decided to write a simple plugin to expand my cgi-app object with 
another object called YAML which contains the Load and Dump methods.
  I've got a fully working module but looking back over it, I'm sure 
some of you here will read it and have your head screaming "There is a 
much easier way to do that!". If you aren't familiar with cgi-app all I 
need do is return the methods that I want to become part of the main object.

Script below, I've cut out the POD, if you want it let me know.

Don't worry it's very short!

package CGI::Application::Plugin::YAML;

use strict;
use warnings;
use Carp;

use vars qw ( $VERSION @ISA @EXPORT %EXPORT_TAGS $IMPORTGROUP );

require Exporter;
@ISA = qw(Exporter);

@EXPORT = ( 'YAML' );

%EXPORT_TAGS = (
    all => [ 'YAML' ],
    std => [ 'YAML' ],
);

$VERSION = '0.03';

$IMPORTGROUP = ':std';

my $yaml;

sub import {
    $IMPORTGROUP = $_[1];
    $yaml = new CGI::Application::Plugin::YAML::guts;
    CGI::Application::Plugin::YAML->export_to_level(1, @_);
}#sub

sub YAML {
    return $yaml;
}#sub


package CGI::Application::Plugin::YAML::guts;

sub new {
    my $class = shift;
    require YAML::Any;
    my $obj = {};
    if ( $CGI::Application::Plugin::YAML::IMPORTGROUP eq ':all' ) {
        YAML->import( qw( Dump Load DumpFile LoadFile ) );
        ### Overloading imported routines as class causes problems when 
called
        sub LoadFile {
            shift; ### get rid of class
            YAML::Any::LoadFile( @_ );
        }#sub
        sub DumpFile {
            shift;
            YAML::Any::DumpFile( @_ );
        }#sub
    }#if
    else {
        YAML->import( qw( Dump Load ) );
    }#else
    sub Load {
        shift;
        YAML::Any::Load( @_ );
    }#sub
    sub Dump {
        shift;
        YAML::Any::Dump( @_ );
    }#sub
    bless( $obj, $class );
    return $obj;
}#sub


1;


Lyle

_______________________________________________
BristolBathPM mailing list
[email protected]
http://mailman.bristolbath.org/mailman/listinfo/bristolbathpm

Reply via email to