{"id":1073,"date":"2009-08-21T06:05:15","date_gmt":"2009-08-21T11:05:15","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=1073"},"modified":"2014-07-19T07:58:09","modified_gmt":"2014-07-19T12:58:09","slug":"revisit-perl","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=1073","title":{"rendered":"Perl"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Introduction\">Introduction<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Basic_Concepts\">Basic Concepts<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Finding_Help\">Finding Help<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Running_Perl\">Running Perl<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Install_Perl_Modules\">Install Perl Modules<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Perl_Operators\">Perl Operators<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Comments\">Comments<\/a>\n\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Hello_World\">Hello, World<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Here_Doc\">Here Doc<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Date_Time_String\">Date Time String<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Arrays\">Arrays<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Hash\">Hash<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Manipulate_Strings_with_Regular_Expression\">Manipulate Strings with Regular Expression<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Subroutines\">Subroutines<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Modules\">Modules<\/a>\n\t\t\t\t<ol class='toc-even level-2'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Two_Kinds\">Two Kinds<\/a>\n\t\t\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Functional_Modules\">Functional Modules<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Pragmatic_Modules\">Pragmatic Modules<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t<\/ol>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#How_to_include_modules\">How to include modules<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Include_Path\">Include Path<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#List_All_Installed_Modules\">List All Installed Modules<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#BEGIN_and_END_blocks\">BEGIN and END blocks<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Use_Exporter\">Use Exporter<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Automatically_Create_Module_Template\">Automatically Create Module Template<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Object_Oriented_Perl\">Object Oriented Perl<\/a>\n\t\t\t\t<ol class='toc-even level-2'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Object_References\">Object References<\/a>\n\t\t\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Hard_Reference\">Hard Reference<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Symbolic_Reference\">Symbolic Reference<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t<\/ol>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Object_Basics\">Object Basics<\/a>\n\t\t\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Define_a_Class\">Define a Class<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Inheritance\">Inheritance<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t<\/ol>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Useful_Stuff\">Useful Stuff<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#DBI::Oracle\">DBI::Oracle<\/a>\n\t\t\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Install\">Install<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#Connect_to_Oracle\">Connect to Oracle<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t<li>\n\t\t\t\t\t\t\t\t<a href=\"#CRUD\">CRUD<\/a>\n\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t<\/ol>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Useful_Stuff_1\">Useful Stuff<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#References\">References<\/a>\n\t\t\t<\/li>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Introduction\"><h1>Introduction<\/h1><\/span>\n<span id=\"Basic_Concepts\"><h2>Basic Concepts<\/h2><\/span>\n<p>Perl program is a bunch of Perl statements and definitions thrown into a file. It incorporated object oriented support since Perl 5.<\/p>\n<p>Perl is both a compiler and an interpreter. Perl program is compiled into an internal format and then executed.<\/p>\n<span id=\"Finding_Help\"><h2>Finding Help<\/h2><\/span>\n<pre>\r\nperldoc perl                 # list of the many standard Perl manual pages\r\nperldoc perlrun             # Perl's command-line options\r\nperldoc perlapi             # C-based interfaces\r\n\r\nperldoc -f funcname       # look up a Perl function\r\nperldoc -q pattern         # search the questions in the Perl FAQ\r\nperldoc -r pattern          # search documentation recursively\r\nperldoc -f split\r\nperldoc -q '(mail|address)'\r\nperldoc -q -r '(un)pack'\r\n\r\nperldoc IO::File\r\nperldoc perldoc\r\nperldoc -m IO::File\r\nperldoc -l IO::File\r\nperldoc -i exutils::makemaker\r\nperldoc -i -q CGI\r\n<\/pre>\n<span id=\"Running_Perl\"><h2>Running Perl<\/h2><\/span>\n<pre lang=\"bash\">\r\nperl myscript.pl              # run perl\r\nperl -w myscript.pl         # with warning\r\nperl -V:startperl            # ask how to start perl (startperl='#!\/usr\/bin\/perl5.10.0';)\r\nperl -h                         # print command options\r\n<\/pre>\n<span id=\"Install_Perl_Modules\"><h2>Install Perl Modules<\/h2><\/span>\n<pre lang=\"bash\">\r\n# For Unixes, use CPAN \r\nperl -MCPAN -e shell\r\ncpan> install Apache::Registry   # install \r\ncpan> i CGI                            # info on CGI module\r\ncpan> i \/CGI\/                          # info on modules containing CGI\r\ncpan> i \/^CGI\/                        # info on modules start with CGI\r\ncpan> i \/^CGI::\/                      # info on CGI child modules\r\ncpan> m \/XML\/                        # info on XML module\r\ncpan> a DOUGM                       # info on author\r\ncpan> d \/-1.\\d\/                       # info on version 1.x\r\ncpan> b                                 # list all available bundles\r\ncpan> r                                  # list out of date modules\r\ncpan> u \/^XML::\/                    # list available *not* installed modules start with XML\r\ncpan> readme Apache::Session  # reading document without installing\r\n\r\n# For Windows, use ActiveState Perl\r\nppm install Math::Matrix    \r\n<\/pre>\n<span id=\"Perl_Operators\"><h2>Perl Operators<\/h2><\/span>\n<p><a href=\"http:\/\/www.kichwa.com\/quik_ref\/operators.html\">Perl Operators<\/a><\/p>\n<span id=\"Comments\"><h2>Comments<\/h2><\/span>\n<pre lang=\"perl\">\r\n# This is a commented out single line\r\n\r\n# Use POD\r\n=For comment\r\nThis is a multiple line\r\ncomments\r\n=Cut\r\n\r\n# Use Acme::Comment\r\nuse Acme::Comment type => 'C++';\r\n\/*\r\n    This is a comment ...\r\n    ... C++ style!\r\n*\/\r\n<\/pre>\n<p>More on <a href=\"http:\/\/www.perl.com\/pub\/a\/2002\/08\/13\/comment.html?page=1\">here<\/a>.<\/p>\n<span id=\"Hello_World\"><h3>Hello, World<\/h3><\/span>\n<pre lang=\"perl\">\r\n#!\/usr\/bin\/perl -w # -w: produce extra warnings\r\n$secretword=\"llama\";\r\nprint(\"What is your name?\"); # writes to screen\r\n$name = <STDIN>; # reads from keyboard\r\nchomp($name); # removes trailing newline character\r\nif ($name eq \"Jimmy\") { # if statement\r\n  print(\"Hello, Jimmy! How good of you to be here!\\n\");\r\n} else {\r\n  print(\"Hello, $name!\\n\");\r\n  print(\"What's the secret word? \");\r\n  $guess = <STDIN>;\r\n  chomp($guess);\r\n  while ($guess ne $secretword) { # while loop\r\n    print(\"Wrong, try again. What is the secret word?\");\r\n    $guess = <STDIN>;\r\n    chomp($guess);\r\n  }\r\n  print(\"Welcome again, $name\");\r\n}\r\n<\/pre>\n<span id=\"Here_Doc\"><h3>Here Doc<\/h3><\/span>\n<pre lang=\"perl\">\r\nmy $heredoc = <<END;\r\nHello,\r\nHere Doc.\r\nEND\r\n<\/pre>\n<p>* Reference: <a href=\"http:\/\/www.stonehenge.com\/merlyn\/UnixReview\/col12.html\">http:\/\/www.stonehenge.com\/merlyn\/UnixReview\/col12.html<\/a><\/p>\n<span id=\"Date_Time_String\"><h3>Date Time String<\/h3><\/span>\n<p>* localtime()<br \/>\nConverts a time as returned by the time function to a 9-element list with the time analyzed for the local time zone:<br \/>\n0: $sec, 1: $min, 2: $hour, 3: $mday, 4: $mon, 5: $year, 6: $wday, 7: $yday, 8: $isdst<\/p>\n<pre lang=\"perl\">\r\nmy ($current_min, $current_hour, $current_day, $current_mon,$current_year) \r\n\t= (localtime)[1,2,3,4,5];\r\nmy $curretn_date_string = sprintf '%02d\/%02d\/%d %02d:%02d', \r\n\t$current_mon+1, $current_day, $current_year+1900, $current_hour, $current_min;\r\n<\/pre>\n<p>* Reference: <a href=\"http:\/\/perldoc.perl.org\/functions\/localtime.html\">http:\/\/perldoc.perl.org\/functions\/localtime.html<\/a><\/p>\n<span id=\"Arrays\"><h3>Arrays<\/h3><\/span>\n<p>* Array variable names begin with '@', for example, '@words'.<br \/>\n* Array element is accessed with zero based subscript reference. For example, '$words[0]', '$words[1]'.<\/p>\n<pre lang=\"perl\">\r\n@pets = (\"dog\", \"cat\", \"fish\", \"hermit crab\");\r\n$dog = $pets[0];\r\n\r\n# Anonymous array\r\n$array = [ 'One', 'Two', 'Three' ];\r\n@arrayarray = ( 1, 2, [1, 2, 3]);\r\n<\/pre>\n<span id=\"Hash\"><h3>Hash<\/h3><\/span>\n<p>* Hash variable names begine with '%', for example, '%words'.<br \/>\n* Hash element is accessed with a key reference which can be any scalar value, for example, '$words{\"fred\"}'.<\/p>\n<pre lang=\"perl\">\r\n%words = (\r\n\"fred\", \"camel\",\r\n\"barney\", \"llama\",\r\n\"betty\", \"fish\"\r\n);\r\n\r\n$word1 = $words{\"fred\"};\r\n\r\n# Anonymous hash\r\n$hash = { 'Man' => 'Bill',\r\n          'Woman' => 'Mary,\r\n          'Dog' => 'Ben'\r\n};\r\n<\/pre>\n<span id=\"Manipulate_Strings_with_Regular_Expression\"><h3>Manipulate Strings with Regular Expression<\/h3><\/span>\n<p>* =~ # Match<br \/>\n* != # Not match<\/p>\n<span id=\"Match_Operator\"><h4>Match Operator<\/h4><\/span>\n<p>* m\/PATTERN\/ or \/PATTERN\/ # Match all occurrance<br \/>\n* ?PATTERN? # Match only once<\/p>\n<pre lang=\"perl\">\r\nmy $str1 = 'Hello world!';\r\nif ($str1 =~ m\/world\/){\r\n  print \"\\$str1 contains 'world'\\n\";\r\n}\r\n\r\nif ($str1 =~ \/Hello\/){\r\n  print \"\\$str1 contains 'Hello'\\n\";\r\n}\r\n\r\nmy $str2 = '10:10:10';\r\nmy ($hours, $minutes, $seconds) = ($str2 =~ m\/(\\d+):(\\d+):(\\d+)\/);\r\nprint \"hour: $hours, minute: $minutes, second: $seconds\";\r\n<\/pre>\n<span id=\"Match_Operator_Modifiers\"><h4>Match Operator Modifiers<\/h4><\/span>\n<pre lang=\"perl\">\r\n\/PATTERN\/i # case insensitive\r\n\/PATTERN\/m # ^ $ match begin and end of line char\r\n\/PATTERN\/g # global find all matches\r\n<\/pre>\n<span id=\"Substitue_Operator\"><h4>Substitue Operator<\/h4><\/span>\n<p>* s\/PATTERN\/REPLACEMENT\/;<br \/>\n* Substitue Operator Modifiers<\/p>\n<pre lang=\"perl\">\r\ns\/PATTERN\/REPLACEMENT\/i # case insensitive\r\ns\/PATTERN\/REPLACEMENT\/m # ^ $ match begin and end of line char\r\ns\/PATTERN\/REPLACEMENT\/g # global find all matches\r\n<\/pre>\n<span id=\"Translate_Operator\"><h4>Translate Operator<\/h4><\/span>\n<p>* tr\/SEARCHLIST\/REPLACEMENTLIST\/<br \/>\n* y\/SEARCHLIST\/REPLACEMENTLIST\/<br \/>\n* Translate operator modifiers<\/p>\n<pre lang=\"perl\">\r\ntr\/SEARCHLIST\/REPLACEMENTLIST\/c # Complement search list, i.e. all non-matched will be replaced\r\ntr\/SEARCHLIST\/REPLACEMENTLIST\/d # Delete found but unreplaced characters\r\ntr\/SEARCHLIST\/REPLACEMENTLIST\/s # Squash duplicately replaced characters\r\n<\/pre>\n<pre lang=\"perl\">\r\n# Match first word is jimmy, case insensitive:\r\n$name =~ \/^jimmy\\b\/i\r\n\r\n# Remove everything after the first word:\r\n$name =~ s\/\\W.*\/\/;\r\n\r\n# Convert all letters to lower case:\r\n$name =~ tr\/A-Z\/a-z\/;\r\n\r\nif ($name =~ \/^jimmy\\b\/i) {\r\n  print(\"Hi Jimmy!\");\r\n}else{\r\n  print(\"Hi, not Jimmy.\");\r\n}\r\n<\/pre>\n<span id=\"Subroutines\"><h3>Subroutines<\/h3><\/span>\n<pre lang=\"perl\">\r\n# Call subroutine\r\ngood_word('john', 'myguess');\r\n\r\nsub good_word {\r\n  # Access parameters\r\n  #my $somename = shift;\r\n  #my $someguess = shit;\r\n  \r\n  # Or\r\n  my($somename, $someguess) = @_; # my() defines variables as private\r\n\r\n  $somename =~ s\/\\W.*\/\/; # want want only the first word\r\n  $somename =~ tr\/A-Z\/a-z\/; # convert to lower cases\r\n  if ($somename eq \"jimmy\") {\r\n    return 1;\r\n  } elsif (($words{$somename} || \"groucho\") eq $someguess) {\r\n    return 1;\r\n  } else {\r\n    return 0;\r\n  }\r\n}\r\n<\/pre>\n<p>* Reference: <a href=\"http:\/\/www.perl.com\/doc\/manual\/html\/pod\/perlsub.html\">http:\/\/www.perl.com\/doc\/manual\/html\/pod\/perlsub.html<\/a><\/p>\n<span id=\"Modules\"><h1>Modules<\/h1><\/span>\n<span id=\"Two_Kinds\"><h2>Two Kinds<\/h2><\/span>\n<span id=\"Functional_Modules\"><h3>Functional Modules<\/h3><\/span>\n<p>* User modules<br \/>\n* Usually upper cases and more than one word long separated by double colons.<\/p>\n<span id=\"Pragmatic_Modules\"><h3>Pragmatic Modules<\/h3><\/span>\n<p>* Implements pragmas to modify Perl behavior at compile time.<br \/>\n* Uses lower case and usually one word long.<\/p>\n<span id=\"How_to_include_modules\"><h2>How to include modules<\/h2><\/span>\n<pre lang=\"perl\">\r\n# include a perl file\r\ndo '\/home\/perl\/loadme.pl';\r\n\r\n# include the old-style (and obsolete) getopts library\r\nrequire 'getopts.pl';\r\n   \r\n# include the newer Getopt::Std library\r\n# (i.e. PATH\/Getopt\/Std.pm)\r\nrequire Getopt::Std;\r\n\r\n# include Getopt::Std at compile time \r\nuse Getopt::Std;\r\n\r\n# importing a list of symbols with a comma-separated list:\r\nuse Module qw(sub1 sub2 $scalar @list :tagname);\r\n\r\nuse Module; # import default symbols \r\nuse Module(); # suppress all imports\r\n\r\n# if pragma\r\nuse if $ENV{USE_XML_PARSER},\"XML::Parser\"; \r\nuse if !$ENV{USE_XML_PARSER},\"XML::SAX\";\r\n\r\n# require Perl version 5.6.0 or higher\r\nrequire 5.6.0;\r\nuse 5.6.0;\r\n\r\n# require CGI.pm version 2.36 or higher\r\nuse CGI 2.36 qw(:standard);\r\n<\/pre>\n<span id=\"Include_Path\"><h2>Include Path<\/h2><\/span>\n<pre lang=\"perl\">\r\n# Print out @INC\r\nprint \"$_\\n\" foreach @INC;\r\nforeach (@INC) { print \"$_\\n\"; }\r\n\r\n# Print out %INC which contains modules requested\r\nprint \"\\%INC contains: \\n\";\r\n     foreach (keys %INC) {\r\n     print \"  $INC{$_}\\n\";\r\n}\r\n\r\n# Add to start of @INC externally\r\nperl -I\/home\/httpd\/perl\/lib,\/usr\/local\/extra\/lib\/modules perl\r\nPERL5OPT=\"I\/home\/httpd\/perl\/lib,\/usr\/local\/p5lib\"\r\nPERL5LIB=\"\/home\/httpd\/perl\/lib:\/usr\/local\/p5lib\"\r\n\r\n# add directory to end of @INC\r\npush @INC, \"\/home\/httpd\/perl\/lib\";\r\nuse lib '\/home\/httpd\/perl\/lib';\r\n\r\n# include libraries relative to script\r\nuse FindBin qw($RealDir); # or $Bin, $Dir, or $RealBin ... \r\nuse lib \"$RealDir\/..\/lib\";\r\n\r\n# remove paths from @INC with the no directive:\r\nno lib 'home\/httpd\/perl\/lib';\r\n\r\n# lazy include a module\r\nuse autouse 'Module' => qw(sub1 sub2 Module::sub3);\r\n\r\n# Checking for module availability\r\nwarn \"GD module not available\" unless eval {require GD; 1};\r\nBEGIN {\r\n    foreach ('GD', 'CGI', 'Apache::Session') {\r\n       warn \"$_ not available\" unless eval \"use $_; 1\";\r\n    }\r\n}\r\n<\/pre>\n<span id=\"List_All_Installed_Modules\"><h2>List All Installed Modules<\/h2><\/span>\n<pre lang=\"perl\">\r\n#!\/usr\/bin\/perl\r\n# installedfiles.pl\r\nuse warnings;\r\nuse strict;\r\n   \r\nuse ExtUtils::Installed;\r\n   \r\nmy $inst = new ExtUtils::Installed;\r\n   \r\nforeach my $package ($inst->modules) {\r\n    my $valid = $inst->validate($package)?\"Failed\":\"OK\";\r\n    my $version = $inst->version($package);\r\n    $version = 'UNDEFINED' unless defined $version;\r\n   \r\n    print \"\\n\\n--- $package v$version [$valid] ---\\n\\n\";\r\n    if (my @source = $inst->files($package, 'prog')) {\r\n        print \"\\t\", join \"\\n\\t\", @source;\r\n    }\r\n    if (my @docs = $inst->files($package, 'doc')) {\r\n        print \"\\n\\n\\t\", join \"\\n\\t\", @docs;\r\n    }\r\n}\r\n<\/pre>\n<span id=\"BEGIN_and_END_blocks\"><h2>BEGIN and END blocks<\/h2><\/span>\n<pre lang=\"perl\">\r\nBEGIN \r\n(compile phase) \r\nCHECK\r\nINIT\r\n(run phase)\r\nEND\r\n<\/pre>\n<span id=\"Use_Exporter\"><h2>Use Exporter<\/h2><\/span>\n<p>* Use Exporter:<\/p>\n<pre lang=\"perl\">\r\npackage My::Module;\r\n\r\nuse strict;\r\nuse Exporter;\r\n\r\n# Extends Exporter\r\n@My::Module::ISA = qw(Exporter);\r\n\r\n# Export hello subroutine\r\n@My::Module::EXPORT = qw(hello);\r\n\r\n# exported only on request\r\n@EXPORT_OK = qw(helloAgain);\r\n\r\nsub hello {\r\n    return \"Hello World\\n\";\r\n}\r\n\r\nsub helloAgain {\r\n    return \"Hello World\\n\";\r\n}\r\n\r\n1;<\/pre>\n<p>* Test<\/p>\n<pre lang=\"perl\">\r\n#!\/usr\/bin\/perl\r\nuse warnings;\r\nuse strict;\r\n   \r\nuse lib '.'; #look in current directory for My\/Module.pm\r\nuse My::Module; # or use My::Module qw(hello);\r\n   \r\nprint hello;<\/pre>\n<span id=\"Automatically_Create_Module_Template\"><h2>Automatically Create Module Template<\/h2><\/span>\n<pre lang=\"perl\">\r\nh2xs -AXn MyModule\r\n<\/pre>\n<span id=\"Object_Oriented_Perl\"><h1>Object Oriented Perl<\/h1><\/span>\n<span id=\"Object_References\"><h2>Object References<\/h2><\/span>\n<p>* A reference is a pointer to another object<\/p>\n<span id=\"Hard_Reference\"><h3>Hard Reference<\/h3><\/span>\n<p>* Hard reference refers to the *actual data* contained in data structure.<\/p>\n<pre lang=\"perl\">\r\n$foo = 'foo';\r\n$fooref = \\$foo;\r\n\r\n$arrayref = \\@ARGV;\r\n$hashref = \\%ENV;\r\n$globref = \\*STDOUT;\r\n\r\nsub foo { print \"foo\" };\r\n$foosub = \\&foo;\r\n<\/pre>\n<p>* Dereference a reference by prepending with corresponding data type characters<\/p>\n<pre lang=\"perl\">\r\n$$fooref;\r\n\r\ntestRef();\r\nsub testRef {\r\n\tmy $foo = 'foo';\r\n\tprint \"\\$foo: $foo\";\r\n\tprint \"\\n\";\r\n\tmy $fooref = \\$foo;\r\n\tprint \"\\$fooref: $fooref\";\r\n\tprint \"\\n\";\r\n\t$$fooref = 'bar';\r\n\tprint \"\\$foo: $foo\";\r\n\tprint \"\\n\";\r\n}\r\n\r\n@$arrayref;\r\n\r\n%$hashref;\r\n\r\n&$foosub;\r\n<\/pre>\n<span id=\"Symbolic_Reference\"><h3>Symbolic Reference<\/h3><\/span>\n<p>* Symbolic reference uses another variable to point to the actual variable.<br \/>\n* E.g. $x = 10; $foo = \"x\"; $$foo = 20; print $x will produces 20 instead of 10.<br \/>\n* Symbolic references can be disallowed by use strict 'refs';<br \/>\n* Symbolic references are only allow for global variables, not variables declared by 'my'.<\/p>\n<span id=\"Object_Basics\"><h2>Object Basics<\/h2><\/span>\n<p>* A Perl class is a Perl package that contains methods to manipulate class objects.<br \/>\n* A Perl method is a Perl subroutine defined within the class whose first argument is <strong>always<\/strong> an object reference or package name.<\/p>\n<span id=\"Define_a_Class\"><h3>Define a Class<\/h3><\/span>\n<pre lang=\"perl\">\r\n# Class name\r\npackage Person;\r\n\r\n# Imports\r\nuse strict;\r\nuse Exporter;\r\n\r\n# Export\r\n@Person::ISA = qw(Exporter);\r\n@Person::EXPORT = qw(new getFirstName setFirstName);\r\n\r\n# Constructor\r\nsub new\r\n{\r\n    my $class = shift;\r\n    my $self = {\r\n        # These are class variables\r\n        _firstName => shift,\r\n        _lastName  => shift,\r\n        _ssn       => shift,\r\n    };\r\n    # Return a reference to this object\r\n    bless $self, $class;\r\n    return $self;\r\n}\r\n\r\n# Destructor\r\nsub DESTROY {\r\n\tprint \"Employee::DESTROY called\\n\";\r\n}\r\n\r\n# Called whenever undefined subroutines are called\r\nsub AUTOLOAD\r\n{\r\n   print \"Employee::AUTOLOAD called.\\n\";\r\n   return();\r\n}\r\n<\/pre>\n<span id=\"Inheritance\"><h3>Inheritance<\/h3><\/span>\n<pre lang=\"perl\">\r\n# Subclass name\r\npackage Employee;\r\n\r\nuse strict;\r\nuse Person;\r\nuse Exporter;\r\n\r\n# Inherits Person\r\n@Employee::ISA = qw(Person);\r\n\r\n# Overrides constructor\r\nsub new {\r\n\tmy $class = $_[0];\r\n\tmy $self = $class->SUPER::new($_[1], $_[2], $_[3]);\r\n        \r\n       # Defines new class variable\r\n\t$self->{_title} = $_[4];\r\n\tbless $self, $class;\r\n\treturn $self;\r\n}\r\n<\/pre>\n<span id=\"Useful_Stuff\"><h2>Useful Stuff<\/h2><\/span>\n<pre lang=\"perl\">\r\n# trim string\r\n$blank =~ s\/^\\s+|\\s+$\/\/g;\r\n<\/pre>\n<span id=\"DBI::Oracle\"><h2>DBI::Oracle<\/h2><\/span>\n<span id=\"Install\"><h3>Install<\/h3><\/span>\n<p>* Install Oracle client or copy an existing Oracle client installation over. CPAN needs libraries from Oracle client in order to compile DBI and DBD::Oracle.<br \/>\n* Set ORACLE_HOME env var.<br \/>\n* Set Path to include Perl\\bin and %ORACLE_HOME%\\bin (Path=c:\\perl\\bin;%ORACLE_HOME%\\bin;%PATH%)<br \/>\n* Set ORACLE_SID env var. Make sure user 'scott' is unlock and password set to 'tiger'. DBD::Oracle test script uses scott\/tiger for testing.<br \/>\n* Install<\/p>\n<pre lang=\"perl\">\r\nc:\\perl -MCAPN -e shell\r\ncpan>install DBI\r\ncpan>install DBD::Oracle\r\n<\/pre>\n<span id=\"Connect_to_Oracle\"><h3>Connect to Oracle<\/h3><\/span>\n<pre lang=\"perl\">\r\n# Connect to Oracle\r\nmy $host = \"192.168.1.4\";\r\nmy $sid = \"orcl\";\r\nmy $user = \"scott\";\r\nmy $passwd = \"tiger\";\r\nmy $dbh = DBI->connect(\"dbi:Oracle:host=$host;sid=$sid\", $user, $passwd, {AutoCommit => 0})\r\n\t\tor die $DBI::errstr;\r\n# Default to AutoCommit on\r\n#my $dbh = DBI->connect(\"dbi:Oracle:host=$host;sid=$sid\", $user, $passwd) or die $DBI::errstr;\r\n\r\n<\/pre>\n<span id=\"CRUD\"><h3>CRUD<\/h3><\/span>\n<pre lang=\"perl\">\r\nuse DBI;\r\n\r\nmy ($dbh);\r\n\r\nopenDB();\r\ntestCreate();\r\ntestRetrieve();\r\ntestUpdate();\r\ntestRetrieve();\r\ntestDelete();\r\ncloseDB();\r\nsub openDB {\r\n\tprint \"Open DB...\";\r\n\tprint \"\\n\";\r\n    my $host = \"192.168.0.105\";\r\n    my $sid = \"orcl\";\r\n    my $user = \"scott\";\r\n    my $passwd = \"tiger\";\r\n    unless (defined $dbh){\r\n\t\t$dbh = DBI->connect(\"dbi:Oracle:host=$host;sid=$sid\", $user, $passwd, {AutoCommit => 0})\r\n\t\t\tor die $DBI::errstr;\r\n    }\r\n    print \"DB opened\";\r\n\tprint \"\\n\";\r\n}\r\n\r\nsub closeDB {\r\n\tprint \"Closing DB...\";\r\n\tprint \"\\n\";\r\n\t$dbh->disconnect() or warn $dbh->errstr();\r\n\tprint \"DB closed\";\r\n\tprint \"\\n\";\r\n}\r\n\r\nsub testCreate {\r\n\tprint \"Creating...\\n\";\r\n\tmy $sth = $dbh->prepare(\"insert into test_table values ('John', 'Doe')\");\r\n\t$sth->execute() or die $DBI::errstr;\r\n\t$dbh->commit() or die $DBI::errstr;\r\n\t$sth->finish();\r\n\t\r\n\tmy $sth2 = $dbh->prepare(\"insert into test_table values (?,?)\");\r\n\t$sth2->execute('Jane', 'Doe') or die $DBI::errstr;\r\n\t$dbh->commit() or die $DBI::errstr;\r\n\t$sth2->finish();\r\n\tprint \"Done Creating...\\n\";\r\n}\r\n\r\nsub testRetrieve {\r\n\tprint \"Retrieving...\\n\";\r\n\tmy $sth3 = $dbh->prepare(\"select * from test_table\");\r\n\t$sth3->execute() or die $DBI::errstr;\r\n\twhile (my @row = $sth3->fetchrow_array()){\r\n\t\tmy ($firstName, $lastName) = @row;\r\n\t\tprint \"Fist name: $firstName; Last name: $lastName\\n\";\r\n\t}\r\n\t$sth3->finish;\r\n\tprint \"Done Retrieving...\\n\";\r\n}\r\n\r\nsub testUpdate {\r\n\tprint \"Updating...\";\r\n\tprint \"\\n\";\r\n\tmy $updateSth = $dbh->prepare(\"update test_table set first_name= ? where first_name = ?\");\r\n\t$updateSth->execute('Jack', 'Jane') or die $DBI::errstr;\r\n\tprint \"Number of rows update: \", $updateSth->rows;\r\n\tprint \"\\n\";\r\n\t$dbh->commit() or die $DBI::errstr;\r\n\t$updateSth->finish();\r\n\tprint \"Done updating\";\r\n\tprint \"\\n\";\r\n}\r\n\r\nsub testDelete {\r\n\tprint \"Deleting...\\n\";\r\n\tmy $deleteSth = $dbh->prepare(\"delete from test_table\");\r\n\t$deleteSth->execute() or die $DBI::errstr;\r\n\t$deleteSth->finish();\r\n\t$dbh->commit() or die $DBI::errstr;\r\n\t$deleteSth->finish();\t\r\n\tprint \"Done Deleting...\\n\";\r\n}\r\n<\/pre>\n<span id=\"Useful_Stuff_1\"><h2>Useful Stuff<\/h2><\/span>\n<pre lang=\"perl\">\r\n# trim string\r\n$blank =~ s\/^\\s+|\\s+$\/\/g;\r\n\r\n# Find and replace string in files\r\n# e.g. find all .txt files and replace string two with ten\r\nperl -e \"s\/two\/ten\/g;\" -pi $(find . -name \"*.txt\")\r\n\r\n# Same as above but save found files with .ori extenstion\r\nperl -e \"s\/two\/ten\/g;\" -pi.ori $(find . -name \"*.txt\")\r\n\r\n<\/pre>\n<span id=\"References\"><h1>References<\/h1><\/span>\n<p>Learning Perl by RandalL. Schwartz etc.<br \/>\n<a href=\"http:\/\/www.steve.gb.com\/perl\/lesson08.html\">http:\/\/www.steve.gb.com\/perl\/lesson08.html<\/a><br \/>\nPro Perl by Peter Wainwright<br \/>\n<a href=\"http:\/\/www.tutorialspoint.com\/perl\/index.htm\">http:\/\/www.tutorialspoint.com\/perl\/index.htm<\/a><br \/>\n<a href=\"http:\/\/docstore.mik.ua\/orelly\/perl\/advprog\/index.htm\">Perl Books<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Basic Concepts Perl program is a bunch of Perl statements and definitions thrown into a file. It incorporated object oriented support since Perl 5. Perl is both a compiler and an interpreter. Perl program is compiled into an internal &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=1073\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[59],"tags":[579],"class_list":["post-1073","post","type-post","status-publish","format-standard","hentry","category-perl","tag-perl"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-hj","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1073"}],"version-history":[{"count":39,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1073\/revisions"}],"predecessor-version":[{"id":10143,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1073\/revisions\/10143"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}