{"id":10803,"date":"2020-03-29T11:59:28","date_gmt":"2020-03-29T11:59:28","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=10803"},"modified":"2020-03-29T14:21:46","modified_gmt":"2020-03-29T14:21:46","slug":"perl","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=10803","title":{"rendered":"Perl"},"content":{"rendered":"<p><a href=\"https:\/\/perldoc.perl.org\/perlintro.html\">perlintro<\/a> &#8211; a brief introduction and overview of Perl<br \/>\n<a href=\"https:\/\/perldoc.perl.org\/perlop.html\">perlop<\/a> &#8211; Perl operators and precedence<br \/>\n<a href=\"https:\/\/perldoc.perl.org\/perlobj.html\">perlobj<\/a> &#8211; Perl object reference<br \/>\n<a href=\"https:\/\/perldoc.perl.org\/perldebug.html\">perldebug<\/a> &#8211; Perl debugging<br \/>\n<a href=\"https:\/\/perldoc.perl.org\/perlrequick.html\">perlrequick<\/a> &#8211; Perl regular expressions quick start<\/p>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>Perl Environment Include\/Library Path<\/h3>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# perl -e &quot;print qq(@INC)&quot;\r\n\/usr\/local\/lib\/perl5\/site_perl\/mach\/5.20 \/usr\/local\/lib\/perl5\/site_perl \/usr\/local\/lib\/perl5\/5.20\/mach \/usr\/local\/lib\/perl5\/5.20 \/usr\/local\/lib\/perl5\/site_perl\/5.20 \/usr\/local\/lib\/perl5\/site_perl\/5.20\/mach\r\n\r\n# find \/usr\/local\/lib\/perl5 -name &quot;*.pm&quot;\r\n\/usr\/local\/lib\/perl5\/5.20\/AnyDBM_File.pm\r\n\/usr\/local\/lib\/perl5\/5.20\/App\/Cpan.pm\r\n...\r\n<\/pre>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>Variable Type<\/h3>\n<p><a href=\"https:\/\/www.tutorialspoint.com\/perl\/perl_variables.htm\">Perl &#8211; Variables<\/a><\/p>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>Enum<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/473666\/does-perl-have-an-enumeration-type\">Does Perl have an enumeration type?<\/a><\/p>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>RegEx<\/h3>\n<p><a href=\"http:\/\/modernperlbooks.com\/books\/modern_perl\/chapter_06.html\">Regular Expressions and Matching<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/28529592\/perl-strings-comparison-and-regex\">perl &#8211; strings comparison and regex<\/a><\/p>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>System Command-Line<\/h3>\n<p><a href=\"https:\/\/perldoc.perl.org\/functions\/system.html\">perldoc: system<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/3854651\/how-can-i-store-the-result-of-a-system-command-in-a-perl-variable\">How can I store the result of a system command in a Perl variable?<\/a><br \/>\n<a href=\"https:\/\/perldoc.perl.org\/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command%3f\">How can I capture STDERR from an external command?<\/a><\/p>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>Name of the Perl script<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/4600192\/how-to-get-the-name-of-perl-script-that-is-running\">How to get the name of Perl script that is running<\/a><\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\nprint $0;\r\n<\/pre>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>Command Line Arguments<\/h3>\n<ul>\n<li>Use <code>$ARGV[<span style=\"color: #ff0000;\">n<\/span>]<\/code> to display argument.<\/li>\n<li>Use <code><span style=\"color: #ff0000;\">$#ARGV<\/span><\/code> to get total number of passed argument to a perl script.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/perlmaven.com\/argv-in-perl\">Processing command line arguments &#8211; @ARGV in Perl<\/a><br \/>\n<a href=\"https:\/\/www.cyberciti.biz\/faq\/howto-pass-perl-command-line-arguments\/\">Perl Display And Pass Command Line Arguments With @argv<\/a><br \/>\n<a href=\"http:\/\/alvinalexander.com\/perl\/perl-command-line-arguments-read-args\/\">How to read Perl command-line arguments<\/a><\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\nmy $argc= $#ARGV + 1;\r\n\r\nmy $name   = $ARGV&#x5B;0];\r\nmy $number = $ARGV&#x5B;1];\r\n# or\r\nmy ($name, $number) = @ARGV;\r\n\r\n<\/pre>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>Loops (<code>for<\/code>, <code>foreach<\/code>, etc.)<\/h3>\n<p><a href=\"https:\/\/perlmaven.com\/for-loop-in-perl\">Perl for loop explained with examples<\/a><\/p>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>defined<\/h3>\n<p><a href=\"https:\/\/perldoc.perl.org\/functions\/defined.html\">perldoc: defined<\/a><\/p>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>Arrays<\/h3>\n<p><a href=\"https:\/\/perlmaven.com\/perl-arrays\">Perl Arrays<\/a><\/p>\n<p>Note, when accessing a single element of an array the leading sigil changes from @ to $. This might cause confusion to some people, but if you think about it, it is quite obvious why.<\/p>\n<p><code>@<\/code> marks plural and <code>$<\/code> marks singular. When accessing a single element of an array it behaves just as a regular scalar variable.<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\n# Declare an array\r\nmy @names;\r\n\r\n# Declare and assign values:\r\nmy @names = (&quot;Foo&quot;, &quot;Bar&quot;, &quot;Baz&quot;);\r\n\r\n# Debugging of an array\r\nuse Data::Dumper qw(Dumper);\r\nmy @names = (&quot;Foo&quot;, &quot;Bar&quot;, &quot;Baz&quot;);\r\nsay Dumper \\@names;\r\n# $VAR1 = &#x5B;\r\n#         'Foo',\r\n#         'Bar',\r\n#         'Baz'\r\n#       ];\r\n\r\n# foreach loop and perl arrays\r\nmy @names = (&quot;Foo&quot;, &quot;Bar&quot;, &quot;Baz&quot;);\r\nforeach my $n (@names) {\r\n  say $n;\r\n}\r\n# Foo\r\n# Bar\r\n# Baz\r\n\r\n# Accessing an element of an array\r\nmy @names = (&quot;Foo&quot;, &quot;Bar&quot;, &quot;Baz&quot;);\r\nsay $names&#x5B;0];\r\n<\/pre>\n<h4>Indexing array<\/h4>\n<p>The indexes of an array start from 0. The largest index is always in the variable called $#name_of_the_array. So<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\nmy @names = (&quot;Foo&quot;, &quot;Bar&quot;, &quot;Baz&quot;);\r\nsay $#names;\r\n<\/pre>\n<p>Will print 2 because the indexes are 0,1 and 2.<\/p>\n<h4>Length or size of an array<\/h4>\n<p>In Perl there is no special function to fetch the size of an array, but there are several ways to obtain that value. For one, the size of the array is one more than the largest index. In the above case <code>$#names+1<\/code> is the size or length of the array.<\/p>\n<p>In addition the <code>scalar<\/code> function can be used to to obtain the size of an array:<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\nmy @names = (&quot;Foo&quot;, &quot;Bar&quot;, &quot;Baz&quot;);\r\nsay scalar @names;\r\n<\/pre>\n<p>Will print 3.<\/p>\n<p>The scalar function is sort of a casting function that &#8211; among other things &#8211; converts an array to a scalar. Due to an arbitrary, but clever decision this conversion yields the size of the array.<\/p>\n<h4>Loop on the indexes of an array<\/h4>\n<p>There are cases when looping over the values of an array is not enough. We might need both the value and the index of that value. In that case we need to loop over the indexes, and obtain the values using the indexes:<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\nmy @names = (&quot;Foo&quot;, &quot;Bar&quot;, &quot;Baz&quot;);\r\nforeach my $i (0 .. $#names) {\r\n  say &quot;$i - $names&#x5B;$i]&quot;;\r\n}\r\n<\/pre>\n<p>prints:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n0 - Foo\r\n1 - Bar\r\n2 - Baz\r\n<\/pre>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>String<\/h3>\n<p><a href=\"https:\/\/alvinalexander.com\/perl\/edu\/articles\/pl010003.shtml\">Perl string concatenation &#8211; How to concatenate strings with Perl<\/a><\/p>\n<ul>\n<li>Perl string concatenation &#8211; Method #1 &#8211; Using ${name}<\/li>\n<li>Perl string concatenation &#8211; Method #2 &#8211; using Perl&#8217;s dot operator<\/li>\n<li>Perl string concatenation &#8211; Method #3 &#8211; using Perl&#8217;s join function<\/li>\n<\/ul>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\n$name = 'foo';\r\n$filename = &quot;\/tmp\/${name}.tmp&quot;;\r\n\r\n$name = checkbook'; \r\n$filename = '\/tmp\/' . $name . '.tmp'; \r\n# $filename now contains &quot;\/tmp\/checkbook.tmp&quot;\r\n\r\n$name = 'checkbook'; \r\n$filename = join '', '\/tmp\/', $name, '.tmp'; \r\n# $filename now contains &quot;\/tmp\/checkbook.tmp&quot;\r\n<\/pre>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h3>File I\/O<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/13659186\/how-to-tell-perl-to-print-to-a-file-handle-instead-of-printing-the-file-handle\">How to tell perl to print to a file handle instead of printing the file handle?<\/a> !!!<\/p>\n<p><a href=\"https:\/\/perlmaven.com\/writing-to-files-with-perl\">Writing to files with Perl<\/a><br \/>\n<a href=\"https:\/\/www.tutorialspoint.com\/perl\/perl_files.htm\">Perl &#8211; File I\/O<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/10166932\/how-to-remove-one-line-from-a-file-using-perl\">How to remove one line from a file using Perl?<\/a><br \/>\n<a href=\"https:\/\/superuser.com\/questions\/357996\/how-do-i-delete-a-certain-line-from-a-file-with-perl\">How do I delete a certain line from a file with Perl?<\/a><br \/>\n<a href=\"https:\/\/www.tek-tips.com\/faqs.cfm?fid=6549\">Insert a line at the beginning of a file<\/a><\/p>\n<ul>\n<li><code>\"&lt;file.txt\"<\/code>: read-only mode<\/li>\n<li><code>\"&gt;file.txt\"<\/code>: writing mode<\/li>\n<li><code>\"+&lt;file.txt\"<\/code>: updating without truncating<\/li>\n<li><code>\"+&gt;file.txt\"<\/code>: truncate the file first<\/li>\n<li><code>\"&gt;&gt;file.txt\"<\/code>: append mode<\/li>\n<li><code>\"+&gt;&gt;file.txt\"<\/code>: append mode with read<\/li>\n<\/ul>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\nuse strict;\r\nuse warnings;\r\n \r\nmy $filename = 'report.txt';\r\nopen(my $fh, '&gt;', $filename) or die &quot;Could not open file '$filename' $!&quot;;\r\nprint $fh &quot;My first report generated by perl\\n&quot;;\r\nclose $fh;\r\nprint &quot;done\\n&quot;;\r\n<\/pre>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h2>Process List<\/h2>\n<p><a href=\"http:\/\/blogs.perl.org\/users\/perlancar\/2014\/12\/day-17-checking-process-existence-and-listing-processes-procfind.html\">Day 17: Checking process existence and listing processes (Proc::Find)<\/a><br \/>\n<a href=\"https:\/\/www.perlmonks.org\/bare\/?node_id=659067\">ps in perl?<\/a><br \/>\n<a href=\"https:\/\/metacpan.org\/pod\/Proc::Find\">Proc::Find<\/a> &#8211; Find processes by name, PID, or some other attributes<br \/>\n<a href=\"https:\/\/metacpan.org\/pod\/Proc::ProcessTable\">Proc::ProcessTable<\/a> &#8211; Perl extension to access the unix process table<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# perl ...\r\nCan't locate Proc\/Find.pm in @INC (you may need to install the Proc::Find module)\r\n<\/pre>\n<p><!-- -------------------------------------------------------------------------------------- --><\/p>\n<h2>Perl 6<\/h2>\n<p><a href=\"http:\/\/perltuts.com\/tutorials\/types-in-perl-6\/enums\">Tutorials \/ Types in Perl 6 \/ Enums<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>perlintro &#8211; a brief introduction and overview of Perl perlop &#8211; Perl operators and precedence perlobj &#8211; Perl object reference perldebug &#8211; Perl debugging perlrequick &#8211; Perl regular expressions quick start Perl Environment Include\/Library Path # perl -e &quot;print qq(@INC)&quot; \/usr\/local\/lib\/perl5\/site_perl\/mach\/5.20 \/usr\/local\/lib\/perl5\/site_perl \/usr\/local\/lib\/perl5\/5.20\/mach \/usr\/local\/lib\/perl5\/5.20 \/usr\/local\/lib\/perl5\/site_perl\/5.20 \/usr\/local\/lib\/perl5\/site_perl\/5.20\/mach # find \/usr\/local\/lib\/perl5 -name &quot;*.pm&quot; \/usr\/local\/lib\/perl5\/5.20\/AnyDBM_File.pm \/usr\/local\/lib\/perl5\/5.20\/App\/Cpan.pm &#8230; Variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10803","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/10803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10803"}],"version-history":[{"count":26,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/10803\/revisions"}],"predecessor-version":[{"id":10829,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/10803\/revisions\/10829"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}