Created
May 20, 2010 14:15
-
-
Save azurestone/407599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use strict; | |
| my $name = 'hoge'; | |
| my $value = 'xxxxxxx'; | |
| my $json = new TestJSON( name => $name, value => $value ); | |
| # $json の中身は { name => 'hoge', value => 'xxxxxxx' } | |
| # $json は 'TestJSON' というpackage nameと関連付け(blessされてる) | |
| # 以下同様 | |
| my $yaml = new TestYAML( name => $name, value => $value ); | |
| my $html = new TestHTML( name => $name, value => $value ); | |
| $json->output(); | |
| # こう書くと $json は 'TestJSON' という package name でblessされているので | |
| # perlの便利機能によって、 TestJSON::output($json) が自動で実行される | |
| # 以下同様 | |
| $yaml->output(); | |
| $html->output(); | |
| # データは所属するpackageを最初に覚えてるので、 -> 演算子を使えばパッケージ名を指定しなくても | |
| # 自動で所属しているpackageのメソッドを呼んでくれる。また、自分自身を引数として渡さなくても | |
| # 自動で第一引数として渡してくれる。便利。 | |
| exit; | |
| #----------------------------------------------------- | |
| package TestJSON; | |
| sub new { | |
| my ($class, %args) = @_; | |
| my $self = {}; | |
| $self->{name} = $args{name}; | |
| $self->{value} = $args{value}; | |
| return bless $self, $class; | |
| } | |
| sub output { | |
| my $self = shift; | |
| printf '{ "%s" => "%s" }', $self->{name}, $self->{value}; | |
| } | |
| #----------------------------------------- | |
| package TestYAML; | |
| sub new { | |
| my ($class, %args) = @_; | |
| my $self = {}; | |
| $self->{name} = $args{name}; | |
| $self->{value} = $args{value}; | |
| return bless $self, $class; | |
| } | |
| sub output { | |
| my $self = shift; | |
| printf '%s: %s', $self->{name}, $self->{value}; | |
| } | |
| #----------------------------------------- | |
| package TestHTML; | |
| sub new { | |
| my ($class, %args) = @_; | |
| my $self = {}; | |
| $self->{name} = $args{name}; | |
| $self->{value} = $args{value}; | |
| return bless $self, $class; | |
| } | |
| 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