Translate

2008年7月31日木曜日

URIモジュール

URLを触る時は、URI(perlモジュール名)が超便利だと思う。

と、言うわけで触ってみた。
-分解編-
use strict;
use warnings;
use URI;

print "URI_VERSION: ", URI->VERSION, "\n\n";
my $uri = URI->new("http://www.hoge.com/path1/path2/?q1=abc&q2=cde");

print "URI: ", $uri, "\n";
print "scheme: ", $uri->scheme, "\n";
print "host: ", $uri->host, "\n";
print "path: ", $uri->path, "\n";
print "query: ", $uri->query, "\n\n";

$uri = URI->new("http://www.hoge.com/path1/path2/index.html#hoge");
print "URI: ", $uri, "\n";
print "fragment: ", $uri->fragment, "\n";

実行結果



-結合編-
use strict;
use warnings;
use URI;

my $uri = URI->new("http://www.hoge.com/path1/path2/");
$uri->query_form(q1 => 'abc', q2 => 'def');

print "URI: ", $uri, "\n";


実行結果
-------------------------------------------------------------
URI: http://www.hoge.com/path1/path2/?q1=abc&q2=def
-------------------------------------------------------------

2008年7月28日月曜日

use CGI;のタグリスト

タイトルのまんま。
よく使うのに、いまだにどのタグリストが何をサポートしてるんだかだったんで
CGI.pmよりがっつりハードコピー。

%EXPORT_TAGS = (
':html2'=>['h1'..'h6',qw/p br hr ol ul li dl dt dd menu code var strong em
tt u i b blockquote pre img a address cite samp dfn html head
base body Link nextid title meta kbd start_html end_html
input Select option comment charset escapeHTML/],
':html3'=>[qw/div table caption th td TR Tr sup Sub strike applet Param
embed basefont style span layer ilayer font frameset frame script small big Area Map/],
':html4'=>[qw/abbr acronym bdo col colgroup del fieldset iframe
ins label legend noframes noscript object optgroup Q
thead tbody tfoot/],
':netscape'=>[qw/blink fontsize center/],
':form'=>[qw/textfield textarea filefield password_field hidden checkbox checkbox_group
submit reset defaults radio_group popup_menu button autoEscape
scrolling_list image_button start_form end_form startform endform
start_multipart_form end_multipart_form isindex tmpFileName uploadInfo URL_ENCODED MULTIPART/],
':cgi'=>[qw/param upload path_info path_translated request_uri url self_url script_name
cookie Dump
raw_cookie request_method query_string Accept user_agent remote_host content_type
remote_addr referer server_name server_software server_port server_protocol virtual_port
virtual_host remote_ident auth_type http append
save_parameters restore_parameters param_fetch
remote_user user_name header redirect import_names put
Delete Delete_all url_param cgi_error/],
':ssl' => [qw/https/],
':cgi-lib' => [qw/ReadParse PrintHeader HtmlTop HtmlBot SplitParam Vars/],
':html' => [qw/:html2 :html3 :html4 :netscape/],
':standard' => [qw/:html2 :html3 :html4 :form :cgi/],
':push' => [qw/multipart_init multipart_start multipart_end multipart_final/],
':all' => [qw/:html2 :html3 :netscape :form :cgi :internal :html4/]
);

こうしてみると、パラメータのデータはCGI.pmで受け取りたいけど
HTMLはヒアドキュメントを使ってがっつりモリモリ書いてます…
という人なら
use CGI qw/:cgi/;
で、おkなのね。

パラメータをCGI.pmで受け取って、HTMLもCGI.pmで生成するのであれば
use CGI qw/:standard/;
で、おkなのね。

CGI.pmを解説しているページ
 →http://tuka.s12.xrea.com/index.xcg?p=CGI.pm

じゃんじゃん参考にさせていただきまひょ。

2008年7月20日日曜日

|| && 演算子のメモ

初期値をセットするのによく使うor演算子。
sub hoge{
my $arg = shift || "hogehoge";
print $arg, "\n";
}

上の場合は、引数があればその値を表示し
なければ"hogehoge"を表示することになる。
じゃ、下の場合は何を表示する?
use strict;
use warnings;

print 1 || 2, ",\n";
print 0 || 3, ",\n";
print "" || 4, ",\n";
print "" || "", ",\n";
print "" || 0, ",\n\n";


print 5 && 6, ",\n";
print 7 && 0, ",\n";
print 0 && 8, ",\n";
print 9 && "" && 0, ",\n";

最後に評価した物が返されるってことさえ頭にあればとくに問題ないですよね。
or演算子が最後に評価するものといえば、
一つでも真がある時、一番最初の真の値
真がない時、一番最後の偽の値

and演算子が最後に評価するものといえば
全て真の時、一番最後の真の値
偽の時、一番最初の偽の値


リファレンスの復習

用語的なもの。
リファレンス・・・値、変数を指し示す物
リファレント・・・リファレンスが指し示す元の値・変数
$scalar(変数)
$ref_scalar = \$scalar(リファレンス)
$value = ${$ref_scalar}(リファレント)

ref関数で返す値
スカラ値:undef
スカラへのリファレンス:"SCALAR"
配列へのリファレンス:"ARRAY"
ハッシュへのリファレンス:"HASH"
サブルーチンへのリファレンス:"CODE"
ファイルハンドルへのリファレンス:"IO"または"IO::Handle"
型グロブへのリファレンス:"GLOB"
事前コンパイルパターンへのリファレンス:"Regexp"
別リファレンスへのリファレンス:"REF"
bless済みの変数:クラス名

2008年7月19日土曜日

いろんなHello,World!

プログラムの最初に必ずと言って登場する言葉ですよね 笑
そんなHellow,World!突き詰めてみると案外新しい発見が
あったりするもので、なかなか侮れません 笑

そんなわけで、いろんなHellow, world!を扱った
ページでものっけておきますねぇ。

-マイコミの場合-
Hello Worldコレクション(Perlなら→Perl編 - 意外!?に多芸多才なスクリプト言語

-どう書く?orgの場合-
どう書く?org(Hello, world!)
どう書く?org(Hello, world!2)

-wikipediaの場合-
Hello worldプログラムの一覧