Friday, January 26, 2007

Perl オブジェクト指向 Class 継承

HogeA.pm
package HogeA;
use strict;
sub new {
my $class = shift;
my $self = bless {}, $class;
return $self;
}
sub hoge {
my $self = shift;
print "Hoge::hoge\n";
}
1;


HogeB.pm
package HogeB;
use strict;
use base qw(HogeA);
sub new {
my $class = shift;
my $self = bless {}, $class;
return $self;
}
sub hogehoge{
print "hogehoge\n";
}
1;

---sample.pl
use strict;
use HogeB;
my $b = HogeB->new();
$b->hoge;

ISAも使えます。

use vars

use strict
を使った場合のグローバル変数をするときにつかう。
最新のバージョンではoursが推奨されますが Classの中で定義した方がよりベータ

Wednesday, January 24, 2007

Data::Pageのサンプル Template Toolkit

1..2 3 4 5 6 7 8 9 10 11 12 ..28
のようなページ支援の例


[% FOREACH num = [pager.first_page .. pager.last_page] %]
[% IF pager.current_page - pager.first_page > 5 && num == pager.first_page %]
<a href="hoge?page=[% num %]">[% num %]</a>..
[% END %]
[% IF num <= (pager.current_page + 5 ) && num >= (pager.current_page - 5 ) %]
[% IF num == pager.current_page %]<b>[% num %]</b>
[% ELSE %]<a href="hoge?page=[% num %]">[% num %]</a>[% END %]
[% END %]
[% IF (pager.last_page - pager.current_page) > 5 && num == pager.last_page %]
..<a href=hoge?page=[% num %]">[% num %]</a>
[% END %]
[% END %]

Thursday, January 18, 2007

Template::Plugin::FillInFormの使い方

--temp.html--



[% USE FillInForm %]

[% FILTER fillinform fdat => fdat1 %]

<form action="http://www.blogger.com/aaa" method="post">

<input type="text" name="hoge">

</form>

[% END %]



--- perl ----

#!/usr/local/bin/perl



use strict;

use Template;



my %values = (

hoge => 'hoge1',

);



my $output;

my $template = Template->new({

OUTPUT => \$output,

TRIM =>1,

});





$template->process(

'temp.html',

{ fdat1 =>\%values,

},

) or print $template->error;



print $output;







--- 実行結果







 

Template-Toolkit

サンプル

任意でconfigの指定ができる。
PRE_PROCESS ヘッダファイルの指定
POST_PROCESS フッタファイルの指定
INCLUDE_PATH テンプレートファイルの置く場所の指定
OUTPUT 出力の制御(詳細は後のURLを参照)
TRIM 改行などの削除(詳細は後のURLに書かれたBLOCKとINCLUDEの説明を参照)


my $output;
my $template = Template->new({
PRE_PROCESS => 'header.html',
POST_PROCESS => 'footer.html',
INCLUDE_PATH => '/home/hoge/temp',
OUTPUT => \$output,
TRIM =>1,
});

$template->process(
'sample.html',
);

print $output;


http://www.template-toolkit.org/docs/plain/Manual/Config.html