Skip to content

Instantly share code, notes, and snippets.

@anazawa
anazawa / make_accessors.pl
Created February 4, 2012 04:51
make accessors
for my $field (qw(type nph expires cookie charset attachment p3p)) {
my $slot = __PACKAGE__."::$field";
my $code = sub {
my $self = shift;
my $value = shift;
if ($value) {
$self->set($field => $value);
}
else {
@anazawa
anazawa / gist:1817205
Created February 13, 2012 14:14
transform OO interface into functional one
# $value = get_header( $blosxom::header, 'foo' );
# $bool = exists_header( $blosxom::header, 'foo' );
# set_header( $blosxom::header, 'foo' => 'bar' );
# remove_header( $blosxom::header, 'foo' );
for my $method ( qw(get set remove exists) ) {
my $slot = __PACKAGE__ . "::${method}_header";
my $code = sub {
my ( $header_ref, @args ) = @_;
__PACKAGE__->new( $heaeder_ref )->$method( @args );
@anazawa
anazawa / gist:1828017
Created February 14, 2012 16:43
Blosxom::Header::Prototype
package Blosxom::Header::Prototype;
use strict;
use warnings;
use Carp;
use Scalar::Util qw(refaddr);
{
my %prototype_of;
sub can {
@anazawa
anazawa / gist:1846400
Created February 16, 2012 16:50
Blosxom::Header::Object
package Blosxom::Header::Object;
sub new {
my ( $class, %method ) = @_;
while ( my ( $method, $code_ref ) = each %method ) {
next if ref $code_ref ne 'CODE';
my $slot = __PACKAGE__ . "::$method";
{
@anazawa
anazawa / gist:1992461
Created March 7, 2012 10:40
CGI::Application で Text::MicroTemplate::Extended を使う
# 目的: CGI::Application のテンプレートエンジンを Text::MicroTemplate::Extended で置き換える
# 方法: MyApp::View::MT で HTML::Template との API の差を吸収する
# 結果: できない -> https://gist.github.com/1993446
# 参考:
# https://metacpan.org/module/CGI::Application#load_tmpl-
# https://metacpan.org/module/Text::MicroTemplate::Extended
# lib/MyApp.pm
# html_tmpl_class で load_tmpl が呼ぶコンストラクタを変更する。
sub setup {
@anazawa
anazawa / gist:1993446
Created March 7, 2012 14:27
MyApp::View::MT
package MyApp::View::MT;
use strict;
use warnings;
use parent 'Exporter';
use Text::MicroTemplate::Extended;
our @EXPORT_OK = qw( render_mt );
sub import {
my $caller = scalar caller;
# while_each_last.pl
use strict;
use warnings;
my %foo = ( foo => 'bar' );
while ( my ( $k, $v ) = each %foo ) {
print "$k: $v\n";
last;
}
@anazawa
anazawa / gist:2246049
Created March 30, 2012 02:52
好みの問題?
{
my %method_of = (
get => \&Blosxom::Header::get_header,
set => \&Blosxom::Header::set_header,
exists => \&Blosxom::Header::exists_header,
delete => \&Blosxom::Header::delete_header,
push => \&Blosxom::Header::push_header,
);
while ( my ( $method, $sub_ref ) = each %method_of ) {
@anazawa
anazawa / gist:2466717
Created April 22, 2012 20:34
呼び出し可能なサブルーチンを限定する
{
my %foo = (
start => 0,
template => 0,
entries => 0,
filter => 1,
skip => 1,
interpolate => 1,
head => 1,
sort => 1,
@anazawa
anazawa / gist:2778763
Created May 24, 2012 01:11
Apache 風のアクセス・ログを出力する Blosxom plugin
package access_log;
use strict;
use warnings;
use POSIX qw/strftime/;
sub start { !$blosxom::static_entries }
sub last {
my $h = $ENV{REMOTE_ADDR};
my $l = '-';