{"id":331,"date":"2021-01-18T12:00:00","date_gmt":"2021-01-18T12:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=331"},"modified":"2024-02-08T08:23:09","modified_gmt":"2024-02-08T13:23:09","slug":"perl-_-vs-_","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/perl-_-vs-_\/","title":{"rendered":"Perl $_ vs. $_[]"},"content":{"rendered":"\n<p>For a beginner, Perl can be a frightening language. This is largely due the numerous special variables used in perl. Once a programmer understands the special variables, he\/she begins to appreciate the beauty and strength of Perl.<\/p>\n\n\n\n<p><strong>The special variable $_<\/strong><\/p>\n\n\n\n<p>$_ functions as a default variable. $_ needs to be initialized implicitly or explicitly before it can used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$_ = \"default value\";\nprint;  # this will print the value of $_<\/code><\/pre>\n\n\n\n<p>Note that I did not specify which variable to print. When no variable is provided where a variable is expected, the $_ special variable is used. Here&#8217;s another example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while() \n  print $_;<\/code><\/pre>\n\n\n\n<p>This program will reprint whatever the user types.&nbsp;takes the input from the an external device such as a keyboard. This data must be assigned to a variable before it can be used. Since I did not specify a variable, the input is automatically stored in $_. If I remove the $_ from the second line, this program will still function properly since print; will print the value of $_ by default as seen in the previous example.<\/p>\n\n\n\n<p><strong>$_[], a subscript of @_ special variable<\/strong><\/p>\n\n\n\n<p>It is common for Perl beginners to confuse $_ with $_[]. In fact, $_[] is a subscript of @_ and it has nothing to do with the scalar variable $_.<\/p>\n\n\n\n<p>@_ special variable contains a list of all the arguments passed to a subroutine. $_[argument number] is a way of accessing an individual argument.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sub printarguments \n  print join('-',@_);\n  print \"\\nThe second argument was $_&#91;1]\";\n\nprintarguments('one','two','three');<\/code><\/pre>\n\n\n\n<p>Perl indexes begin from 0. Therefore $_[0] accesses the first argument, $_[1] accesses the second argument, and so on.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a beginner, Perl can be a frightening language. This is largely due the numerous special variables used in perl. Once a programmer understands the special variables, he\/she begins to appreciate the beauty and strength of Perl. The special variable $_ $_ functions as a default variable. $_ needs to be initialized implicitly or explicitly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[512],"tags":[79],"class_list":["post-331","post","type-post","status-publish","format-standard","hentry","category-misc","tag-perl"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=331"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/331\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/331\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}