{"id":101,"date":"2008-04-10T23:31:45","date_gmt":"2008-04-10T18:01:45","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/2008\/04\/perl-hello-script\/"},"modified":"2008-04-10T23:31:45","modified_gmt":"2008-04-10T18:01:45","slug":"perl-hello-script","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2008\/04\/perl-hello-script\/","title":{"rendered":"Perl Hello Script"},"content":{"rendered":"<p><img src='http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2008\/04\/perl.gif' alt='Perl Logo' class=\"intro\" align=\"right\" \/><\/p>\n<p class=\"intro\">This is the next installment of the <a href=\"http:\/\/www.bin-co.com\/blog\/2008\/03\/learning-a-new-programming-language-the-hello-world-method\/\">Hello Script series<\/a> &#8211; Hello Script for Perl. <strong class=\"highlight\">&#8216;Hello Script&#8217; is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet<\/strong> when working with that language.<\/p>\n<h2>Introduction to Perl<\/h2>\n<p>For those of who are unfamiliar with perl, here is the Wikipedia definition&#8230;<\/p>\n<blockquote><p>Perl is a dynamic programming language created by Larry Wall and first released in 1987. Perl borrows features from a variety of other languages including C, shell scripting (sh), AWK, sed and Lisp. Perl was widely adopted for its strengths in text processing and lack of the arbitrary limitations of many scripting languages at the time.<\/p><\/blockquote>\n<p>If you are interested in learning perl, I have written a <a href=\"http:\/\/www.bin-co.com\/perl\/tutorial\/\">Perl Tutorial<\/a>. And here are some more links if you are interested<\/p>\n<ul>\n<li><a href=\"http:\/\/www.perl.com\/\">Perl.com<\/a><\/li>\n<li><a href=\"http:\/\/www.perl.org\/\">The Perl Directory<\/a><\/li>\n<li><a href=\"http:\/\/www.perlmonks.org\/\">Perl Monks<\/a><\/li>\n<\/ul>\n<h2>Hello Script for Perl<\/h2>\n<pre><code class=\"perl\">\n#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\n\n# Printing(IO)\nprint \"Hello World!\\n\";\n\n# Variables, concatenation\nmy $name = 'Binny';\nmy $year = 2008;\nprint \"Hello, \" . $name . \" - welcome to \" . $year . \"\\n\";\n\n#If,else conditions\nif ($year &gt; 2008) {\n\tprint \"Welcome to the future - yes, we have flying cars!\";\n}\nelsif($year &lt; 2008) {\n\tprint \"The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!\";\n}\nelse {\n\tprint \"Anything wrong with your time machine? You have not gone anywhere, kiddo.\";\n}\nprint \"\\n\";\n\n# For loop\nfor(my $i=0; $i&lt;3; $i++) {\n\tprint \"$i) Hi there!\\n\";\n}\n\n#Numerical Array, foreach\nmy @rules = (\n\t'Do no harm',\n\t'Obey',\n\t'Continue Living'\n);\nmy $i = 1;\nforeach my $rule (@rules) {\n\tprint \"Rule \" . $i . \" : \" . $rule . \"\\n\";\n\t$i++;\n}\n\n# Associated array, while\nmy %associated = (\n\t'hello'\t=&gt;\t'world',\n\t'foo'\t=&gt;\t'bar',\n\t'lorem'\t=&gt;\t'ipsum'\n);\nwhile(my ($key, $value) = each(%associated)) {\n\tprint \"$key: $value\\n\";\n}\n\n# Using Join and Split\nmy @csv_values = split(',', \"hello,world,how,are,you\\n\");\nprint join(\":\", @csv_values);\n\n# Function, argument, return, call\nsub hello {\n\t$name = shift; #First argument.\n\treturn \"Hello \" . $name;\n}\nprint hello(\"Binny\");\n\n# File IO\n# File reading, easy method...\nopen(IN,'Hello.pl') or die(\"Cannot open file : $!\");\nmy @lines = &lt;IN&gt;;\nclose(IN);\nmy $contents = join('',@lines);\nprint \"Hello has \" . length($contents) . \" chars\\n\";\n# Writing to a file\nopen(OUT, '&gt;\/tmp\/hello.txt');\nprint OUT \"Hello World\";\nclose(OUT);\n \n# Command Executing\nprint `ls`; #Execute the command 'ls' and print its output\nprint \"\\n\";\n \n# Regular Expressions\nmy $string = \"Hello World\";\nprint \"Yup - its evil\\n\" if($string =~ \/^Hell\/);\n$string =~ s\/l([^l])\/$1\/g; #Remove an 'l' from both words. Should print 'Helo Word'\nprint $string;\n\n\nprint \"\\n\\n\";\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is the next installment of the Hello Script series &#8211; Hello Script for Perl. &#8216;Hello Script&#8217; is a file that contains the most commonly <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2008\/04\/perl-hello-script\/\" title=\"Perl Hello Script\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,25,28],"tags":[75,137,206,232,245,289],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-perl","category-scripts","category-tutorial","tag-cheatsheet","tag-hello","tag-perl","tag-reference","tag-script","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}