{"id":117437,"date":"2018-09-04T08:06:35","date_gmt":"2018-09-04T00:06:35","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=117437"},"modified":"2018-08-27T11:07:55","modified_gmt":"2018-08-27T03:07:55","slug":"perl-programming-skills","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/perl-programming-skills.html","title":{"rendered":"\u5927\u795e\u6559\u4f60\uff1aPerl \u7f16\u7a0b\u6280\u5de7"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPerl \u8d77\u521d\u662f\u7531 Larry Wall \u4e3a\u4e86\u8ba9\u5728 UNIX \u4e0a\u8fdb\u884c\u62a5\u8868\u5904\u7406\u7684\u5de5\u4f5c\u53d8\u5f97\u66f4\u52a0\u65b9\u4fbf\u800c\u8bbe\u8ba1\u548c\u5f00\u53d1\u7684\u5b9e\u7528\u6458\u5f55\u548c\u62a5\u544a\u8bed\u8a00\uff08Practical Extraction and Report Language\uff09\uff0c\u5b83\u501f\u9274\u4e86 C\u3001awk\u3001shell \u811a\u672c\u4ee5\u53ca\u5f88\u591a\u5176\u5b83\u7f16\u7a0b\u8bed\u8a00\u7684\u7279\u6027\u3002Larry Wall \u672c\u4eba\u4e5f\u662f\u4e00\u4e2a\u8bed\u8a00\u5b66\u5bb6\uff0c\u4ed6\u8bbe\u8ba1 perl \u8bed\u8a00\u65f6\u4f7f\u7528\u4e86\u5f88\u591a\u8bed\u8a00\u5b66\u7684\u601d\u7ef4\uff0c\u4ed6\u4e5f\u7ecf\u5e38\u4f7f\u7528\u8bed\u8a00\u5b66\u8bf4\u660e perl \u8bed\u8a00\u7684\u67b6\u6784\uff0c\u50cf\u201c\u53d8\u91cf\u201d\u548c\u201c\u51fd\u6570\u201d\uff0c\u4ed6\u6709\u65f6\u8bf4\u6210\u201c\u540d\u8bcd\u201d\u548c\u201c\u52a8\u8bcd\u201d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5229\u7528\u6ce8\u91ca\u8c03\u8bd5\uff1a\u4f7f\u7528 Smart::Comments \u6a21\u5757<\/strong><\/span><\/div>\n

\u8c03\u8bd5 Perl \u811a\u672c\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u4e00\u79cd\u662f\u4f7f\u7528 Perl \u7684\u5185\u7f6e\u8c03\u8bd5\u5668\uff0c\u53e6\u4e00\u79cd\u662f\u5728\u811a\u672c\u4e2d\u5d4c\u5165 print \u8bed\u53e5\u3002\u5982\u679c\u662f\u7b2c\u4e8c\u79cd\uff0c\u5927\u6982\u5c31\u4f1a\u4e86\u89e3\u5230\uff0c\u50cf\u90a3\u6837\u624b\u5de5\u8c03\u8bd5\u7684\u6700\u5927\u95ee\u9898\u662f\uff1a\u4e00\u65e6\u79fb\u9664\u4e86 bug\uff0c\u5c31\u5f97\u4e5f\u540c\u65f6\u901a\u7bc7\u79fb\u9664\u8c03\u8bd5\u8bed\u53e5\u3002\u4f46\u662f\u5982\u679c\u80fd\u5c06\u8fd9\u4e9b\u8bed\u53e5\u5b89\u5168\u5730\u7559\u5728\u4ee3\u7801\u91cc\u4e0d\u662f\u66f4\u597d\u5417\uff1f\u6bd5\u7adf\u5f88\u53ef\u80fd\u518d\u6b64\u9700\u8981\u4ed6\u4eec\uff0c\u7279\u522b\u662f\u5f53\u53c8\u6709 bug \u51fa\u73b0\u7684\u65f6\u5019\u3002<\/p>\n

\u73b0\u5728\uff0c\u5728 Perl \u91cc\u6709\u8fd9\u6837\u4e00\u4e2a\u6a21\u5757\uff1a\u5b83\u53ef\u4ee5\u5229\u7528\u6ce8\u91ca\u6765\u5f00\u542f\u8c03\u8bd5\u8bed\u53e5\uff0c\u8fd9\u5c31\u662f Smart::Comments \u6a21\u5757\u3002\u4e0b\u9762\u662f\u6700\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5f53\u4f7f\u7528 Smart::Comments \u65f6\uff0c\u4efb\u4f55\u7531\u4e09\u4e2a\u6216\u66f4\u591a\u4e2a # \u5f00\u5934\u7684\u6ce8\u91ca\u5c31\u4f1a\u53d8\u6210\u8c03\u8bd5\u8bed\u53e5\uff0c\u5e76\u4f1a\u628a\u6ce8\u91ca\u7684\u6240\u6709\u5185\u5bb9\u9001\u5230\u5c4f\u5e55\uff1a<\/p>\n

\r\n#!\/usr\/bin\/perl\r\nuse Smart::Comments;\r\n\r\nmy @ipaddr = split \/\\.\/, \"10.109.32.151\";\r\n\r\n### @ipaddr;\r\n<\/pre>\n

\u5f53\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0cSmart::Comments \u4f1a\u627e\u5230\u4e09\u4e2a\u4e00\u7ec4\u7684 # \u6ce8\u91ca\uff0c\u5e76\u6253\u5370\u51fa\u5b83\u4eec\u6240\u5305\u542b\u7684\u6240\u6709\u5185\u5bb9\uff1a<\/p>\n

\r\n### @ipaddr: [\r\n###            '10',\r\n###            '109',\r\n###            '32',\r\n###            '151'\r\n###          ]\r\n<\/pre>\n

Smart::Comments \u7684\u7528\u6cd5\u4e0d\u53ea\u9650\u4e8e\u6253\u5370\u53d8\u91cf\u503c\uff0c\u5b83\u751a\u81f3\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u5faa\u73af\u90e8\u5206\u7528\u8fdb\u5ea6\u6761\u7684\u5f62\u5f0f\u52a8\u6001\u73b0\u5b9e\uff0c\u66f4\u52a0\u8be6\u7ec6\u7684\u63cf\u8ff0\uff0c\u8bf7\u53c2\u8003 perldoc \u6587\u6863\u3002<\/p>\n

\u4e00\u6b21\u8bfb\u5165\u6574\u4e2a\u6587\u4ef6\u7684\u6280\u5de7<\/strong><\/span><\/div>\n

\u6211\u60f3\u4f60\u4e00\u5b9a\u77e5\u9053 Perl \u4e2d\u7684\u94bb\u77f3\u64cd\u4f5c\u7b26\uff08<>\uff09\u3002\u6240\u4ee5\u5982\u679c\u60f3\u628a\u6587\u4ef6\u4e00\u6b21\u6027\u8bfb\u5165\u662f\uff0c\u5e94\u8be5\u9996\u5148\u4fee\u6539 $\/ \u53d8\u91cf\uff1a<\/p>\n

\r\nopen CONF, \"< \", $file;\r\nmy $text = do { local $\/;  };\r\n<\/pre>\n

\u6a21\u5757 File::Slurp \u4e2d\u6709\u5173\u4e8e\u6587\u4ef6\u64cd\u4f5c\u7684\u66f4\u591a\u65b9\u5f0f\u3002<\/p>\n

\u5c01\u88c5 SQL \u8bed\u53e5\uff1a\u4e0d\u8981\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 SQL \u8bed\u53e5<\/strong><\/span><\/div>\n

\u5bf9\u4e8e\u5411\u6570\u636e\u5e93\u4e2d\u63d2\u5165\u6570\u636e\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u4ee3\u7801\u6765\u5b9e\u73b0 SQL \u8bed\u53e5\uff1a<\/p>\n

\r\nsub insert {\r\n    my ($table, $data) = @_;\r\n\r\n    my $sql = \"insert into `$table` \";\r\n\r\n    my $insert_fields = join \", \", map { \"`$_`\" } keys   %$data;\r\n    my $insert_values = join \", \", map { \"'$_'\" } values %$data;\r\n\r\n    $sql .= join \" \", \"(\", $insert_fields, \") \";\r\n    $sql .= join \" \", \"values \", \"(\", $insert_values, \")\";\r\n\r\n    # open a database and return $dbh\r\n    my $sth = $dbh->prepare($sql);\r\n    $sth->execute() or die;\r\n\r\n    $sth->finish();\r\n}\r\n<\/pre>\n

\u8c03\u7528\u65f6\u53ea\u9700\u8981\u4f9d\u636e\u8868\u540d\u548c\u54c8\u5e0c\u6570\u636e\u5373\u53ef\uff1a<\/p>\n

\r\nmy %data = (\r\n        name    => \"Alice\",\r\n        age     => \"23\",\r\n        country => \"U.S.\",\r\n);\r\ninsert \"student\", \\%data;\r\n<\/pre>\n
\u5229\u7528\u5217\u8868\u8d4b\u503c\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c<\/strong><\/span><\/div>\n

\u6211\u4eec\u77e5\u9053\u4e00\u822c\u60c5\u51b5\u4e0b\u5982\u679c\u9700\u8981\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u90a3\u4e48\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u624d\u80fd\u5b8c\u6210\uff0c\u4f46\u662f\u5728 perl \u4e2d\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a<\/p>\n

\r\nmy ($foo, $bar) = (\"foo\", \"bar\");\r\n($foo, $bar) = ($bar, $foo);\r\n<\/pre>\n
\n

\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/myanbin.github.io\/post\/perl-programming-skills.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/perl-programming-skills.html<\/a>\u7f16\u8f91\uff1a\u5411\u91d1\u5e73\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb Perl \u8d77\u521d\u662f\u7531 Larry Wall \u4e3a\u4e86\u8ba9\u5728 UNIX \u4e0a\u8fdb\u884c\u62a5\u8868\u5904\u7406\u7684\u5de5\u4f5c\u53d8\u5f97\u66f4\u52a0\u65b9\u4fbf\u800c\u8bbe\u8ba1\u548c\u5f00 […]<\/p>\n","protected":false},"author":1482,"featured_media":82515,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-117437","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/117437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=117437"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/117437\/revisions"}],"predecessor-version":[{"id":117741,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/117437\/revisions\/117741"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/82515"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=117437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=117437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=117437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}