2008ǯ07·î02Æü 23:00 [Edit]
perl - ¥Ç¥Õ¥©¥ë¥ÈÃͤÎperl¤é¤·¤¤»ØÄêË¡
¤³¤ì¤Ç¤â¤è¤¤¤Î¤À¤±¤É¡¢¤µ¤é¤Ë³æ¹¥¤Ä¤±¤ë¤È¤è¤êperl¤é¤·¤¯¤Ê¤ë¤Î¤Ç¡£
Scalar¤Ë¤è¤ë¥Ç¥Õ¥©¥ë¥ÈÃÍ
sub num{
my $num = shift;
# ....
}
¤³¤³¤Ï
sub num{
my $num = shift || -1;
# ....
}
¤ÎÊý¤¬¤ï¤«¤ê¤ä¤¹¤¤¡£0¤Þ¤¿¤Ï''¤òÆþÎÏÃÍ¡¢¤¹¤Ê¤ï¤ÁÈó¥Ç¥Õ¥©¥ë¥ÈÃͤȤ·¤Æ°·¤¤¤¿¤¤¾ì¹ç¤Î¤ß¡¢
sub num{
my $num = shift;
$num = -1 if not defined $num;
# ....
}
¤È¤¹¤ì¤ÐOK¡£
sub num{
my $num = defined $_[0] ? shift : -1;
# ....
}
¤ÏÆóÅÙ@_¤Ë¥¢¥¯¥»¥¹¤·¤Æ¤¤¤ëʬ¤Á¤ç¤Ã¤È¤¤¤±¤Æ¤Ê¤¤¡£
¤â¤Á¤í¤ó¡¢Perl 5.10.0¤Ê¤é¤Ð
sub num{
my $num = shift // -1;
# ....
}
¤È¤¹¤ë¤³¤È¤¬½ÐÍè¤ë¡£
Hash¤Ë¤è¤ë¥Ç¥Õ¥©¥ë¥ÈÃÍ
ËÜÂê¤Ï¤³¤Á¤é¤«¤Ê¡£¤³¤Î¥Æ¥¯¥Ë¥Ã¥¯¡¢°Õ³°¤ÈÃΤé¤ì¤Æ¤¤¤Ê¤¤¤Î¤Ç¡£
sub conf{
my %arg = @_;
my %opt = ( mode => 1, warn => 1 );
%opt = ( %opt, %arg );
# ...
}
Àˤ·¤¤!»ÅÁȤߤÏÍý²ò¤·¤Æ¤¤¤Æ¤¤¤ë¤Î¤Ë¡£
¤³¤¦¤¹¤ì¤ÐOK.
sub conf{
my %arg = (
lang => 'perl',
rank => 1,
@_ # ¤³¤³¤¬·è¤á¼ê!
);
# ...
}
¤³¤ì¤Ç¤¦¤Þ¤¯¹Ô¤Ã¤Æ¤·¤Þ¤¦¤Î¤Ç¤¹¡£
¤³¤ì¤Ï¡¢¥ª¥Ö¥¸¥§¥¯¥È¤Î½é´ü²½¤ÎºÝ¤Ë¼Â¤Ë¼ê·Ú¤Ç¤¹¡£¤³¤ó¤Ê´¶¤¸¤Ë¡£
package A::Module
sub new{
my $pkg = shift;
bless {
lang => 'perl',
rank => 1,
@_
}, ref $pkg || $pkg;
}
¤Ê¤ª¡¢¤³¤Î¥Æ¥¯¥Ë¥Ã¥¯¤ÏCookbook¤Ë¤¤Á¤ó¤ÈºÜ¤Ã¤Æ¤¤¤Þ¤¹¡£¼«¸Êή¤â°¤¯¤Ê¤¤¤Î¤Ç¤¹¤¬¡¢ËܤⰤ¯¤Ê¤¤¤Ç¤¹¥è¡£Perl¤Î¾ì¹ç¡¢Îò»Ë¤¬Â¾¤ÎLL¤è¤ê¤ä¤äŤ¤¤³¤È¤â¤¢¤Ã¤Æ½¼¼Â¤·¤Æ¤¤¤Þ¤¹¤·¡¢»²¹Í½ñ¤ËÌäÂ꤬¤¢¤ì¤Ð¤¢¤ì¤Ð¤Ç¤½¤ì¤ËÂФ¹¤ë¤Ä¤Ã¤³¤ß¤â¿ô¿¤¯¤¢¤ê¤Þ¤¹¡£»²¹Í½ñ¤ò¡Ö¥Ç¥Õ¥©¥ë¥ÈÃ͡פȤ·¤Æ¸¯¤Ã¤Æ¤ß¤Þ¤·¤ç¤¦¡£
Dan the Perl Monger
¤³¤Îµ»ö¤Ø¤Î¥È¥é¥Ã¥¯¥Ð¥Ã¥¯URL
¤·¤«¤·¤Þ¤¢¡¢¤Ê¤ó¤Æ³Ú¤·¤¤¸À¸ì¤Ê¤Î¤À¤í¤¦
¤¢¤ê¤ã¤Þ¤Û¤ó¤È¤À¡£shiftu¤ì¤¤¤·¤Þ¤·¤¿:)
Dan the Typo Generator
In your sample code.
package A::Module
sub new{
my $pkg = shift;
bless {...}, ref $pkg || $pkg;
}
"ref $pkg || $pkg" seems to be old-style.
Please check out these URLs.
www(dot)perlfoundation(dot)org/perl5/index.cgi?ref_proto_in_object_constructors
perltraining(dot)com(dot)au/tips/2005-02-14.html
http://go4wap.net
