{"id":750,"date":"2014-11-21T09:24:13","date_gmt":"2014-11-21T01:24:13","guid":{"rendered":"http:\/\/ufqi.com\/blog\/?p=750"},"modified":"2014-11-21T09:28:30","modified_gmt":"2014-11-21T01:28:30","slug":"perl-stat-mailx-utf8","status":"publish","type":"post","link":"https:\/\/ufqi.com\/blog\/perl-stat-mailx-utf8\/","title":{"rendered":"perl\u7edf\u8ba1\u7b97\u6570\u53d1\u90ae\u4ef6\u7684\u51e0\u4e2a\u7ec6\u8282: utf8, mailx, json"},"content":{"rendered":"<p>\u6700\u8fd1\u91cd\u65b0\u5b9e\u73b0\u4e86\u4e00\u4e2a\u4f7f\u7528<a href=\"http:\/\/ufqi.com\/naturedns\/search?q=-perl\" target=\"_blank\">-perl<\/a> \u8bfb\u53d6\u6570\u636e\u65e5\u5fd7\u8fdb\u884c\u4e00\u4e9b\u7edf\u8ba1\u7136\u540e\u5c06\u7edf\u8ba1\u7ed3\u679c\u901a\u8fc7\u90ae\u4ef6\u53d1\u9001\u51fa\u6765\u7684\u4efb\u52a1\u3002\u6d89\u53ca\u5230\u51e0\u4e2a\u7ec6\u8282\uff0c\u8bb0\u5f55\u5982\u4e0b\u3002<\/p>\n<p><strong>1. Perl\u4e2d\u7684JSON\u4f7f\u7528<\/strong><br \/>\n\u5728perl\u4e2d\u8c03\u7528 <a href=\"http:\/\/ufqi.com\/naturedns\/search?q=-json\" target=\"_blank\">-json<\/a> \u9700\u8981<br \/>\nuse JSON;<br \/>\nmy $json = JSON-&gt;new-&gt;allow_nonref;<br \/>\nmy $scalar = $json-&gt;utf8(0)-&gt;decode($content_in_json);<br \/>\n\u8fd9\u91cc\u7684 utf8(0) \u5f88\u8be1\u5f02\uff0c\u610f\u601d\u662f\u5bf9\u540e\u9762\u7684$content\u4e0d\u8981\u4f7f\u7528utf8\u8fdb\u884c\u7f16\u7801\uff0c\u4e5f\u5373\u5982\u679c$content\u672c\u8eab\u5df2\u7ecf\u662futf8\u7684\u8bdd\uff0c\u4e0d\u8fd9\u6837\u64cd\u4f5c\u53ef\u80fd\u4f1a\u62a5\u9519wide character found at procedure entry\u7684\u9519\u8bef\uff0c\u6216\u8005\u4e71\u7801\u3002\u76f8\u5e94\u7684\uff0c\u5982\u679c$content\u672c\u8eab\u4e0d\u662futf8\u7684\uff0c\u5219\u9700\u8981\u4f7f\u7528utf8\u5bf9\u5176\u8fdb\u884c\u7f16\u7801\uff1a<br \/>\nmy $scalar = $json-&gt;utf8-&gt;decode($content);<br \/>\n\u53e6\u5916\uff0c$scalar\u4e5f\u9700\u8981\u6ce8\u610f\u53ef\u80fd\u662f@array\uff0c\u4e5f\u53ef\u80fd\u662f%hash\u3002\u9700\u4f9d\u7167\u5177\u4f53\u60c5\u51b5\u591a\u63a2\u6d4b\uff0c\u8f83\u597d\u7684\u65b9\u5f0f\u662fuse Dumper;\u3002<\/p>\n<p><strong>2. \u5199utf8\u7684\u5b57\u7b26\u987a\u5e8f\u6807\u8bb0BOM&#8211;Byte Order Mark<\/strong><br \/>\n\u5173\u4e8eutf8 \u7684BOM\u53ef\u4ee5\u8fdb\u4e00\u6b65\u641c\u7d22\uff0c\u5982\u679c\u8981\u5728\u4e00\u4e2a\u6587\u4ef6\u7684\u5934\u90e8\u52a0\u6ce8BOM\u9700\u8981\u4f7f\u7528binmode\u3002\u4e4b\u6240\u4ee5\u8981\u52a0\u6ce8\uff0c\u662f\u56e0\u4e3a\u4e00\u4e9b\u8f6f\u4ef6\u5982MS Word, Excel\u7b49\uff0c\u5bf9 utf8 w\/o BOM\u7684\u6587\u4ef6\uff08\u6ca1\u6709\u5b57\u7b26\u987a\u5e8f\u6807\u8bb0\u7684UTF)\u5c31\u4f1a\u663e\u793a\u6210\u4e71\u7801\uff0c\u800c\u52a0\u6ce8\u4e86BOM\u7684\u6587\u4ef6\uff0c\u624d\u80fd\u591f\u663e\u793a\u6b63\u5e38\u3002\u5bf9\u4e00\u4e2a\u6587\u4ef6\u52a0\u6ce8BOM\u7684\u9700\u6c42\u5c31\u8fd9\u6837\u4ea7\u751f\u7684\uff0c\u5b9e\u73b0\u8fc7\u7a0b\u5982\u4e0b\uff1a<br \/>\nopen(FILE, &#8220;&gt; test.file&#8221;) or warn &#8220;cannnot open test.file for write.\\n&#8221;;<br \/>\nbinmode FILE;<br \/>\nprint FILE &#8220;\\xff\\xbb\\xbf&#8221;;<br \/>\nclose(FILE);<\/p>\n<p><strong>3. mailx\u53d1\u9001\u90ae\u4ef6\u5e26\u9644\u4ef6<\/strong><br \/>\n\u8fd9\u6837\u7684\u9700\u6c42\u5728\u51e0\u5e74\u524d\u7684\u65f6\u5019\u5c31\u5b9e\u73b0\u8fc7\uff0c\u73b0\u5728\u91cd\u65b0\u5199\uff0c\u6709\u4e9b\u8bb0\u4e0d\u5f97\u7684\u7ec6\u8282\u5c31\u4ece\u7f51\u4e0a\u641c\u7d22\uff0c\u7ed3\u679c\u53d1\u73b0\u4f7f\u7528 mailx, nail\u7b49\u53d1\u9001\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6\uff0c\u7ed3\u679c\u53d1\u73b0\u4ecd\u5728\u4f7f\u7528 uuencode \u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u6211\u8bb0\u5fc6\u4e2d\u4e0d\u662f\u8fd9\u6837\u7684\uff0c\u4e8e\u662f\u53cd\u590d\u8bd5\u9a8c\uff0c\u771f\u7684\u4e0d\u7528\uff0c\u901a\u8fc7 man mailx \u7684\u624b\u518c\u5c31\u80fd\u591f\u770b\u5230\u591a\u52a0\u4e00\u4e2a\u53c2\u6570 -a attachedfile \u7684\u5f62\u5f0f\u5c31\u5b9e\u73b0\u4e86\u53d1\u9001\u9644\u4ef6\u7684\u90ae\u4ef6\u3002\u5c0f\u8ba1\u5982\u4e0b:<br \/>\nmy $mailcontent = &#8220;\/tmp\/mailcontent.tmp&#8221;;<br \/>\n`\/usr\/bin\/echo &#8220;Hi all, \\nThis is a test mail.\\nThere is a file attached with this message.\\n&#8221; &gt; $mailcontent`;<br \/>\n`\/usr\/bin\/mailx -s &#8220;mail with attached file.&#8221; -a attached.file \u00a0wadelau@ufqi.com &lt; $mailcontent`;<br \/>\n\u4e8b\u60c5\u5c31\u8fd9\u6837\u6210\u4e86\uff0c\u901a\u8fc7\u4e00\u4e2a\u4e34\u65f6\u90ae\u4ef6\u5185\u5bb9\u6587\u4ef6\uff0c\u7ed3\u5408\u7ba1\u9053\u547d\u4ee4\uff0c\u5b9e\u73b0\u4e86\u53d1\u751f\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6\u3002<br \/>\n\u8fd9\u8ba9\u6211\u60f3\u8d77\uff0c\u6709\u4e9b\u7f51\u4e0a\u5185\u5bb9\u9700\u8981\u66f4\u65b0\u7684\u95ee\u9898\uff0c\u7f51\u7edc\u7684\u53d1\u5c55\uff0c\u8d8a\u5f80\u540e\u671f\uff0c\u76ee\u524d\u7684\u5185\u5bb9\u5c31\u53ef\u80fd\u4f1a\u9648\u65e7\uff0c\u800c\u8fd9\u4e9b\u9648\u65e7\u7684\u5185\u5bb9\u5728\u641c\u7d22\u5f15\u64ce\u90a3\u91cc\u4f9d\u7136\u5b58\u5728\uff0c\u6709\u7684\u5185\u5bb9\u53ef\u80fd\u83b7\u5f97\u8f83\u9ad8\u7684\u63a8\u8350\u6743\u91cd\uff0c\u4f46\u5b9e\u9645\u4e0a\u67d0\u4e9b\u5185\u5bb9\u6240\u8ff0\u5df2\u7ecf\u88ab\u6dd8\u6c70\uff0c\u4f46\u7528\u6237\u5374\u5f97\u4e0d\u5230\u6700\u65b0\u7684\uff0c\u662f\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u603b\u7ed3\u968f\u60f3\uff0c\u5907\u5fd8\u3002<\/p>\n<p>\u8d60\u9001 <a href=\"http:\/\/ufqi.com\/naturedns\/search?q=-naturedns\" target=\"_blank\">-\u81ea\u7136\u57df\u540d<\/a> <a href=\"https:\/\/ufqi.com\/blog\/%E8%B4%BA-%E4%B8%96%E7%95%8C%E4%BA%92%E8%81%94%E7%BD%91%E5%A4%A7%E4%BC%9A-wic-%E5%BC%80\/\" target=\"_blank\">\u6ce8\u518c\u7801<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u91cd\u65b0\u5b9e\u73b0\u4e86\u4e00\u4e2a\u4f7f\u7528-perl \u8bfb\u53d6\u6570\u636e\u65e5\u5fd7\u8fdb\u884c\u4e00\u4e9b\u7edf\u8ba1\u7136\u540e\u5c06\u7edf\u8ba1\u7ed3\u679c\u901a\u8fc7\u90ae\u4ef6 &hellip; <a href=\"https:\/\/ufqi.com\/blog\/perl-stat-mailx-utf8\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,2],"tags":[48,47,23,46],"_links":{"self":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/750"}],"collection":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/comments?post=750"}],"version-history":[{"count":4,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions"}],"predecessor-version":[{"id":753,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions\/753"}],"wp:attachment":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/media?parent=750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/categories?post=750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/tags?post=750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}