Skip to content

Instantly share code, notes, and snippets.

@azurestone
Created May 20, 2010 14:15
Show Gist options
  • Save azurestone/407601 to your computer and use it in GitHub Desktop.
Save azurestone/407601 to your computer and use it in GitHub Desktop.
use strict;
my $name = 'hoge';
my $value = 'xxxxxxx';
my $json = { name => $name, value => $value };
# ただのhashref
my $yaml = { name => $name, value => $value };
my $html = { name => $name, value => $value };
# $json は JSON で出したいので...
TestJSON::output($json);
# $yaml は YAML で出したいので......
TestYAML::output($yaml);
# $html は HTML で出したいので.........
TestHTML::output($html);
# データはただのhashrefなので、それのoutputを担当するpackageは
# 自分でその都度選んであげるしかない。なのでめんどい。
exit;
#-----------------------------------------------------
package TestJSON;
sub output {
my $self = shift;
printf '{ "%s" => "%s" }', $self->{name}, $self->{value};
}
#-----------------------------------------
package TestYAML;
sub output {
my $self = shift;
printf '%s: %s', $self->{name}, $self->{value};
}
#-----------------------------------------
package TestHTML;
sub output {
my $self = shift;
printf '<dl><dt>%s</dt><dd>%s</dd></dl>', $self->{name}, $self->{value};
}
#------------------------------------------
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment