2013年6月17日 星期一

[Perl] 常見特殊變數和一些語法

http://blog.lauct.org/archives/1438
查$$ 、 $_ 、 ... 等特殊變數

http://mirror.sars.tw/Perl_intro/x348.html
# 轉成大寫
$str = uc(字串);

# 轉成小寫
$str = lc(字串);

http://perl.hcchien.org/ch03.html
perl的map
http://www.linuxfly.org/post/370/
perl的ref函數
http://bbs.chinaunix.net/thread-981190-1-1.html
perl的bless


一些php和perl函數對照
http://langref.org/php+perl/numbers
比較perl和php的 mt_rand函數 =>延伸閱讀 http://www.w3school.com.cn/php/func_math_mt_rand.asp , http://php.net/manual/en/function.mt-rand.php ( Optional highest value to be returned (default: mt_getrandmax() = 2147483647) )

http://www.seancolombo.com/2010/03/24/quick-tip-clone-of-phps-microtime-function-as-a-perl-subroutine/
clone of PHP’s microtime() function as a Perl subroutine. =>http://www.w3school.com.cn/php/func_date_microtime.asp

http://publib.boulder.ibm.com/infocenter/wpdoc/v6r1/index.jsp?topic=/com.ephox.editlive.doc/encoding_content.htm
比較php和perl的rawurlencode函數

http://www.1-script.com/forums/perl/digest-hmac-112824-.htm
php 的 hash_hmac('sha1','Data to be hashed', 'some_secret_key');
perl
use Digest::SHA1 qw(sha1);
use Digest::HMAC qw(hmac);

hmac('some_data', 'some_key', \&sha1, 64);

當一個變數沒被定義時但要做正規式判斷要如何才不會發生警告 ( 類似js的 if (typeof xxx === "undefined") )
http://stackoverflow.com/questions/9493304/use-of-uninitialized-value-in-pattern-match-m
http://fecbob.pixnet.net/blog/post/38613059-null-in-perl
if (defined($element) && ($element =~ /$pattern/))

日期轉timestamp
問題:我用perl算出來的timestamp和mysql的不一樣
> select UNIX_TIMESTAMP('2014-01-22 12:10:18');
+---------------------------------------+
| UNIX_TIMESTAMP('2014-01-22 12:10:18') |
+---------------------------------------+
|                            1390421418 |
+---------------------------------------+
1 row in set (0.00 sec)

use strict;
use warnings;
use DateTime::Format::Strptime;

my $str = '2014-01-22 12:10:18';
my $parser =
  DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %H:%M:%S' );
my $dt = $parser->parse_datetime( $str );
print $dt->epoch;

解:用> show variables; 看mysql上設定的時區 time zone
再將perl的DateTime::Format::Strptime->new時新增時區參數,ex. time_zone =>'America/Los_Angeles'
能用的time_zone: http://search.cpan.org/dist/DateTime-TimeZone/  ex. DateTime::TimeZone::Australia::Melbourne => time_zone => 'Australia/Melbourne'

Is there any problem with “use diagnostics;”?
http://stackoverflow.com/questions/6397170/is-there-any-problem-with-use-diagnostics
diagnostics isn't a lexical pragma like warnings. Instead it sets the global $^W variable (like using the -w flag), which enables warnings for all code, whether it wants it or not, and whether you wrote it or not. That's kind of rude. diagnostics is also silly to use in production. I would recommend using warnings and then just piping your warnings to splain if you need an explanation.

PERL 语言中的q,qw,qr,qx,qq……符号用法
http://blog.sina.com.cn/s/blog_50873f890100a13i.html
''  q//  字面字串
""  qq//  字面字串
``  qx//  命令執行
()  qw//  單字串列
//  m//  樣式比對
s///  s///  樣式代換
y///  tr///  字符轉譯
""  qr//  正規表示式

example:
@test = qw/test perl is fun/;
use Data::Dumper;
print Dumper @test;
#$VAR1 = 'test';
#$VAR2 = 'perl';
#$VAR3 = 'is';
#$VAR4 = 'fun';
 
$reg = "test";
$test = "atesta";
$test =~ s/$reg/bear/;
print $test;  #abeara
 
`mkdir test2`;  #建立test2資料夾
qx/mkdir test3/;  #建立test2資料夾

perl 正規表示式
m|...\|...|
似乎等於
/...|.../
example:
my $content = "123bear2456";
 
my @personal = split(m|\|<\/html>|, $content);
my @personal = split(/|<\/html>/, $content);
 
use Data::Dumper;
print Dumper @personal;

m// 比對樣式
s///取代某個字串中符合樣式的子字串
tr///將一套字符轉譯成另一套
$haystack = "disneedler";
print $haystack =~ m/needle/; #1
print $haystack =~ /needle/; #1
print $haystack =~ m|needle|; #1
 
$a = 'abc123xyz';
$a =~ s/abc/def/; # $a is 'def123xyz' and
print $a;
 
$a = 'aBc123XyZ';
$a =~ tr/a-zA-Z/n-za-mN-ZA-M/;
print $a; #nOp123KlM

$0代表什麼意思?
$0即是程式本身的名字.
print "program name = $0\n"; #即可得知答案歐
print "first argument = $ARGV[0]\n"; # 順帶一提,參數放在@ARGV,與C不太一樣歐
# $1, $2…${N} ,則是Regular Express 取出結果歐,別與$0弄混了 

s

沒有留言:

張貼留言