{"id":331,"date":"2015-09-14T08:09:56","date_gmt":"2015-09-14T08:09:56","guid":{"rendered":"http:\/\/onlinelab.info\/?p=331"},"modified":"2015-09-14T08:09:56","modified_gmt":"2015-09-14T08:09:56","slug":"perl-bai-2-cu-phap-perl-co-ban","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2015\/09\/14\/perl-bai-2-cu-phap-perl-co-ban\/","title":{"rendered":"Perl: B\u00e0i 2 &#8211; C\u00fa ph\u00e1p Perl c\u01a1 b\u1ea3n"},"content":{"rendered":"<p>Perl vay m\u01b0\u1ee3n kh\u00e1 nhi\u1ec1u c\u00fa ph\u00e1p v\u00e0 \u00fd t\u01b0\u1edfng t\u1eeb c\u00e1c ng\u00f4n ng\u1eef kh\u00e1c: awk, sed, C, Bourne Shell, &#8230; Tuy nhi\u00ean c\u00f3 m\u1ed9t v\u00e0i \u0111i\u1ec3m kh\u00e1c gi\u1eefa c\u00e1c ng\u00f4n ng\u1eef. Trong b\u00e0i n\u00e0y s\u1ebd cung c\u1ea5p m\u1ed9t c\u00e1ch t\u1ed5ng quan nh\u1ea5t v\u1ec1 c\u00fa ph\u00e1p c\u1ee7a Perl.<\/p>\n<p>V\u00ed d\u1ee5: \u0110\u1ec3 in ra chu\u1ed7i &#8220;Hello World&#8221; ra m\u00e0n h\u00ecnh ta c\u00f3 c\u00e1c c\u00e1ch\u00a0sau:<\/p>\n<p>Ch\u1ea1y tr\u1ef1c ti\u1ebfp t\u1eeb tr\u00ecnh th\u00f4ng d\u1ecbch:<\/p>\n<pre>$perl e 'print \"Hello World\\n\"'<\/pre>\n<p>ho\u1eb7c c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t file v\u1edbi n\u1ed9i dung nh\u01b0 sau:<\/p>\n<pre>#!\/usr\/bin\/perl\n# This will print \"Hello, World\"\nprint\"Hello, world\\n\"<\/pre>\n<p>Sau \u0111\u00f3, b\u1ea1n th\u1ef1c hi\u1ec7n ch\u1ea1y c\u00e2u l\u1ec7nh \u0111\u00f3.<\/p>\n<p>File ch\u01b0a m\u00e3 ngu\u1ed3n \u0111\u01b0\u1ee3c \u0111\u1eb7t t\u00ean v\u1edbi du\u00f4i m\u1edf r\u1ed9ng l\u00e0 .pl, \u0111u\u1ed5i m\u1edf r\u1ed9ng n\u00e0y d\u00f9ng \u0111\u1ec1 ph\u00e2n bi\u1ec7t v\u1edbi c\u00e1c ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh kh\u00e1c.<\/p>\n<p>D\u1ea5u commnet<\/p>\n<p>D\u1ea5u comment kh\u00e1 quen thu\u1ed9c v\u1edbi c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean. Comment d\u00f9ng \u0111\u1ec3 gi\u1ea3i th\u00edch l\u00e0m cho ch\u01b0\u01a1ng tr\u00ecnh t\u01b0\u1eddng minh v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n. V\u00ed d\u1ee5:<\/p>\n<p># This is comment in Perl &#8211; Python Viet Nam<\/p>\n<p>\u0110\u1ec3 comment m\u1ed9t \u0111o\u1ea1n v\u0103n b\u1ea3n b\u1ea1n c\u1ea7n \u0111\u1eb7t ch\u00fang trong c\u1eb7p = v\u00e0 =cut. V\u00ed d\u1ee5 nh\u01b0 ch\u01b0\u01a1ng tr\u00ecnh sau:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"http:\/\/i.imgur.com\/OHIE56b.jpg\" alt=\"\" width=\"545\" height=\"230\" title=\"\"><\/p>\n<p>Khi ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh, k\u1ebft qu\u1ea3 s\u1ebd nh\u01b0 sau:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"http:\/\/i.imgur.com\/9ofjJGX.jpg\" alt=\"\" width=\"549\" height=\"158\" title=\"\"><\/p>\n<p>Kho\u1ea3ng tr\u1eafng trong Perl<\/p>\n<p>C\u00f3 m\u1ed9t \u0111i\u1ec1u kh\u00e1 th\u00fa v\u1ecb l\u00e0 Perl kh\u00f4ng quan t\u00e2m \u0111\u1ebfn d\u1ea5u kho\u1ea3ng tr\u1eafng. Ch\u01b0\u01a1ng tr\u00ecnh v\u1eabn th\u1ef1c hi\u1ec7n t\u1ed1t khi c\u00f3 d\u1ea5u kho\u1ea3ng tr\u1eafng:<\/p>\n<pre>#!\/usr\/bin\/perl\nprint       \"Hello, world\\n\";\n<\/pre>\n<p>K\u1ebft qu\u1ea3 s\u1ebd v\u1eabn in ra d\u00f2ng Hello, world<\/p>\n<p>N\u1ebfu kho\u1ea3ng tr\u1eafng \u1edf gi\u1eef \u0111o\u1ea1n th\u00f4ng b\u00e1o th\u00ec s\u1ebd hi\u1ec7n nguy\u00ean nh\u01b0 v\u1eady, v\u00ed d\u1ee5:<\/p>\n<pre>#!\/usr\/bin\/perl\n\n# This would print with a line break in the middle - Python Viet Nam\nprint \"Hello\n          world\\n\";\n<\/pre>\n<p>K\u1ebft qu\u1ea3 nh\u01b0 sau:<\/p>\n<pre>Hello\n    world\n\nD\u1ea5u ngo\u1eb7c \u0111\u01a1n v\u00e0 d\u1ea5u ngo\u1eb7c k\u00e9p:\n B\u1ea1n th\u1eed xem qua v\u00ed d\u1ee5 sau:<\/pre>\n<pre>#!\/usr\/bin\/perl\n\nprint \"Hello, world\\n\";\nprint 'Hello, world\\n';\n<\/pre>\n<p>K\u1ebft qu\u1ea3 nh\u01b0 sau:<\/p>\n<pre>Hello, world\nHello, world\\n\n<\/pre>\n<p>B\u1ea1n s\u1ebd th\u1ea5y s\u1ef1 kh\u00e1c nhau c\u01a1 b\u1ea3n gi\u1eefa hai d\u1ea5u n\u00e0y l\u00e0: d\u1ea5u ngo\u1eb7c k\u00e9p s\u1ebd hi\u1ec3n th\u1ecb \\n l\u00e0 xu\u1ed1ng d\u00f2ng, c\u00f2n d\u1ea5u ngo\u1eb7c \u0111\u01a1n s\u1ebd in ra nguy\u00ean g\u1ed1c \u0111o\u1ea1n code. T\u01b0\u01a1ng t\u1ef1 v\u1edbi v\u00ed d\u1ee5 sau:<\/p>\n<pre>#!\/usr\/bin\/perl\n\n$a = 10;\nprint \"Value of a = $a\\n\";\nprint 'Value of a = $a\\n';\nThis will produce the following result \u2212\n<\/pre>\n<p>Khi ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd c\u00f3 k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/p>\n<p>Value of a = 10<br \/>\nValue of a = $a\\n<\/p>\n<p>Khai b\u00e1o \u0111o\u1ea1n v\u0103n b\u1ea3n l\u00e0 m\u1ed9t bi\u1ebfn. B\u1ea1n hay xem v\u00ed d\u1ee5 sau:<\/p>\n<pre>#!\/usr\/bin\/perl\n\n$a = 10;\n$var = &lt;&lt;\"EOF\";\nThis is the syntax for here document and it will continue\nuntil it encounters a EOF in the first line.\nThis is case of double quote so variable value will be\ninterpolated. For example value of a = $a\nEOF\nprint \"$var\\n\";\n\n$var = &lt;&lt;'EOF';\nThis is case of single quote so variable value will not be\ninterpolated. For example value of a = $a\nEOF\nprint \"$var\\n\";\n<\/pre>\n<p>Trong ch\u01b0\u01a1ng tr\u00ecnh b\u1ea1n s\u1ebd th\u1ea5y \u0111o\u1ea1n v\u0103n b\u1ea3n s\u1ebd \u0111\u01b0\u1ee3c g\u00e1n nh\u01b0 m\u1ed9t bi\u1ebfn. V\u00e0 trong \u0111o\u1ea1n v\u0103n b\u1ea3n \u0111\u00f3 ta c\u00f3 th\u1ec3 g\u1ecdi m\u1ed9t bi\u1ebfn n\u00e0o \u0111\u00f3, v\u00ed d\u1ee5 trong \u0111o\u1ea1n m\u00e3 n\u00e0y l\u00e0 a.<\/p>\n<p>K\u1ebft qu\u1ea3 s\u1ebd in ra:<\/p>\n<pre>This is the syntax for here document and it will continue\nuntil it encounters a EOF in the first line.\nThis is case of double quote so variable value will be\ninterpolated. For example value of a = 10\n\nThis is case of single quote so variable value will be\ninterpolated. For example value of a = $a\n<\/pre>\n<p>K\u1ebft lu\u1eadn:<br \/>\nCh\u00fang ta v\u1eeba nghi\u00ean c\u1ee9u c\u00e1c c\u00fa ph\u00e1p c\u01a1 b\u1ea3n nh\u1ea5t c\u1ee7a Perl.<br \/>\nCh\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perl vay m\u01b0\u1ee3n kh\u00e1 nhi\u1ec1u c\u00fa ph\u00e1p v\u00e0 \u00fd t\u01b0\u1edfng t\u1eeb c\u00e1c ng\u00f4n ng\u1eef kh\u00e1c: awk, sed, C, Bourne Shell, &#8230; Tuy nhi\u00ean c\u00f3 m\u1ed9t v\u00e0i \u0111i\u1ec3m kh\u00e1c gi\u1eefa c\u00e1c ng\u00f4n ng\u1eef. Trong b\u00e0i n\u00e0y&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6],"tags":[],"class_list":["post-331","post","type-post","status-publish","format-standard","hentry","category-perl","category-programing"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/comments?post=331"}],"version-history":[{"count":0,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/331\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}