SlideShare a Scribd company logo
Plunging Into Perl  While Avoiding the Deep End (mostly)
Some Perl nomenclature ,[object Object]
Some Perl nomenclature ,[object Object],[object Object]
Some Perl nomenclature ,[object Object],[object Object],[object Object]
Some Perl attributes ,[object Object],[object Object],[object Object]
Some Perl attributes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Your first program #!/usr/local/bin/perl print "Hello, World";
“Protecting” your program (Unix) By default, your program is not executable. chmod 744 your_program You can execute it as owner of the file, anyone else can only read it.
Variables $name can be text or number:  a character, a whole page of text, or any kind of number context determines type can go “both” ways
Variables, array of @employee Array of $employee variables $employee[0] $employee[1] etc.
Variables, hash of $lib{‘thisone’} = “2 days”; $lib{‘thatone’} = “5 days”; Thus can use $grace_period = $lib{$libname} when $libname is thatone, $grace_period is 5 days
Variables, list of ($var1, $var2, $var3) = function_that_does_something; This function returns a list of elements. A list is always inside parentheses ().
Variables, assigning a value to $var = value or expression $array[n] = something; @array = ();  # empty array %hash = ();  # empty hash Can be done almost anywhere, anytime.
Variable scope, and good practices use strict; Requires that you declare all variables like this: my $var; my $var = something; my @array = (); Also makes Perl check your code. Best Practices!
Variable scope, and good practices use strict; my $var; my $var = something; my @array = (); A variable declared like this is visible throughout your program. Best Practices!
Variable scope, and good practices use strict; my $var; my $var = something; my @array = (); A “my” declaration within code grouped within { and } is visible only in that section of code; it does not exist elsewhere. Best Practices! Scope: where in a program a variable exists.
File input and output (I/O)
Using command line arguments Usage:  program.pl infile outfile   $ARGV[0] $ARGV[1]
String manipulation & other stuff substring function
String manipulation & other stuff a better substring example
String manipulation & other stuff index function, find the location of a string in a string
String manipulation & other stuff The split function. Here we split string $l into pieces at every space character. Less common usage: take only 1 st  2 pieces.
String manipulation & other stuff Actually, the 2 nd  statement should be:  $l =~ s/^ +//; The ^ means start looking at the start of the line. find “db ratio” anywhere in $l
String manipulation & other stuff Instead of using $inline[n], $inline[n+1], etc., to refer to elements of array  @inline , here we can refer to  @inline’s  elements via $l in this example. Often makes for clearer and simpler code.
String manipulation & other stuff An often convenient way of populating an array.
String manipulation & other stuff Given $stuff = “this is me”; These are not equivalent: “ print $stuff” ‘ print $stuff’ `print $stuff`
String manipulation & other stuff Given $stuff = “this is me”; These are not equivalent: “ print $stuff” is “print this is me” ‘ print $stuff’ `print $stuff`
String manipulation & other stuff Given $stuff = “this is me”; These are not equivalent: “ print $stuff” is “print this is me” ‘ print $stuff’ is ‘print $stuff’ `print $stuff`
String manipulation & other stuff Given $stuff = “this is me”; `print $stuff`  would have the  operating system try to execute the command <print this is me>
String manipulation & other stuff This form should be used as $something = `O.S. command` Example:  $listing = ‘ls *.pl`; The output of this ls command is placed, as possibly a large string, into the variable $listing. This syntax allows powerful processing capabilities within a program.
printf, sprintf printf(“%s lines here”, $counter) if $counter is 42, we get 42 lines here for the output
printf, sprintf printf(“%c lines here”, $counter) if $counter is 42, we get * lines here for the output, since 42 is the ASCII value for “*”, and we’re printing a  c haracter
printf, sprintf Some additional string formatting… %s – output length is length($var) %10s – output length is absolutely 10  (right justified) %10.20s – output length is min 10,  max 20 %-10.10s – output length is absolutely 10  (left justified) Any padding is with space characters.
printf, sprintf Some additional number formatting… %d – output length is length($var) %10d – output length is absolutely 10   (leading space padded) %-10d – left justified, absolutely 10  (trailing space padded) %-10.10d – right justified, absolutely 10   (leading zero padded)
printf, sprintf Still more number formatting… %f – output length is length($var) %10.10f – guarantees 10 positions to the right of the decimal  (zero padded)
printf, sprintf printf  whatever  outputs to the screen
printf, sprintf printf  whatever  outputs to the screen printf  file   whatever  outputs to that file Ex:  printf file (“this is %s fun”, $much); ( print  functions just like the above, as to  output destination.)
printf, sprintf printf  whatever  outputs to the screen printf  file   whatever  outputs to that file Ex:  printf file (“this is %s fun”, $much); ( print  functions just like the above, as to  output destination.) sprintf is just like any printf, except that its output always goes to a string variable. Ex:  $var = sprintf(“this is %s fun”, $much);
ratiocheck.pl, what it does When the ratio of sizes of certain files related to a database exceeds a threshold, it’s probably time to do an index regen on that database.
ratiocheck.pl, what it does When the ratio of sizes of certain files related to a database exceeds a threshold, it’s probably time to do an index regen on that database. This program computes these ratios for several databases, each with its own threshold, and flags those that are candidates for index regeneration.
program dissection – ratiocheck.pl set up some variables two of these are templates for printing
program dissection – ratiocheck.pl In line 3 above, a file is  slurped,   i.e., the entire file is read into an array  via the <> mechanism.
program dissection – ratiocheck.pl This is a more typical use of the split function. Here, $item is separated into two pieces at the “|” character.
program dissection – ratiocheck.pl We want to check every database in alphabetical order. We are then calling the  checkit  subroutine for each database.
program dissection – ratiocheck.pl The system function executes its string as an O.S. command. Here we are mailing a file to two different people.
program dissection – ratiocheck.pl This subroutine takes 1 argument.
program dissection – ratiocheck.pl Remember our generic templates? Here they are used as a format string for the sprintf function. $generic_path = &quot;/m1/voyager/%s/data/&quot;;
program dissection – ratiocheck.pl The –s test returns a file’s size. (There are several dozen different –x file tests.)
program dissection – ratiocheck.pl Compute the files’ size ratio with sufficient decimal places.
program dissection – ratiocheck.pl  means  new line , loosely equivalent to a CR, or carriage return. Since we want to print the “%” character, we have to escape it with the “ backslash.
program dissection – ratiocheck.pl Here we have a hash reference…  we are checking if the ratio is greater than the threshold for the current database.
program dissection – ratiocheck.pl This is a busy printf statement… the  alert  text gets a string, a character, and a string embedded in it .
program dissection – ratiocheck.pl The  first  argument is a string, which is the output of the  sprintf  statement, which outputs the threshold value for this database.
program dissection – ratiocheck.pl The  second  argument is a character. We print the “%” character, whose ASCII value is 37.
program dissection – ratiocheck.pl The  third  argument is a string. In this case, the string consists of 35 asterisks. A string followed by “xN” will occur N times.
ratiocheck.pl, output Here’s what the output looks like:
DBI stuff What is it and why might I want it? DBI is the DataBase Interface module for Perl. You will also need the specific DBD (DataBase Driver) module for Oracle. This enables Perl to perform queries against your Voyager database. Both of these should already be on your Voyager box.
DBI stuff, how to You need four things to connect to Voyager: machine name your.machine.here.edu username your_username password your_password SID  VGER (or LIBR)
DBI stuff, how to $dbh is the handle for the database $sth is the handle for the query Create a query…then execute it. NOTE: SQL from Access will most      likely NOT work here!
DBI stuff, how to Get the data coming from your query.
DBI stuff, how to Get the data coming from your query. You’ll need a Perl variable for each column returned in the query. Commonly a list of variables is used; you could also use an array.
DBI stuff, how to Get the data coming from your query. You’ll need a Perl variable for each column returned in the query. Commonly a list of variables is used; you could also use an array. Typically, you get your data in a while loop, but you could have $var = $sth->fetchrow_array; when you know you’re getting a single value.
DBI stuff, how to When you’re done with a query, you should finish it. This becomes important when you have multiple queries in succession. You can have multiple queries open at the same time. In that case, make the statement handles unique…$sth2, or $sth_patron. Finally, you can close your database connection.
CPAN Comprehensive Perl Archive Network http://cpan.org You name it and somebody has probably written a Perl module for it, and you’ll find it here. There are also good Perl links here; look for the Perl Bookmarks link.
CPAN Installing modules You need to be  root  for systemwide installation on Unix systems. On Windows machines, you’ll need to be administrator. You can install them “just for yourself” with a bit of tweaking, and without needing root access.  If you’re not a techie, you’ll probably want to find someone who is, to install modules. Installing modules is beyond the scope of this presentation.
Perl on your PC You can get Perl for your PC from ActiveState. They typically have two versions available; I recommend the newer one. Get the MSI version. Installation is easy and painless, but it may take some time to complete. A lot of modules are included with this distribution; many additional modules are available. Module installation is made easy via the Perl Package Manager (PPM). Modules not found this way will require manual installation, details of which are beyond the scope of this presentation.
Date and Time in Perl, basic ### &quot;create&quot; today's date my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime; This gets the date and time information from the system.
Date and Time in Perl, basic ### &quot;create&quot; today's date my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime; my $today = sprintf (&quot;%4.4d.%2.2d.%2.2d&quot;, $year+1900, $month+1, $day); This puts today’s date in “Voyager” format, 2006.04.26
Date and Time in Perl The program, datemath.pl, is part of your handout. The screenshot below shows its output.
Regular expressions, matching m/PATTERN/gi If the  m  for matching is not there, it is assumed. The  g  modifier means to find globally, all occurrences. The  i  modifier means matching case insensitive. Modifiers are optional; others are available.
Regular expressions, substituting s/PATTERN/REPLACEWITH/gi The  s  says that substitution is the intent. The  g  modifier means to substitute globally, all occurrences. The  i  modifier means matching case insensitive. Modifiers are optional; others are available.
Regular expressions, translating tr/SEARCHFOR/REPLACEWITH/cd The  tr  says that translation is the intent. The  c  modifier means translate whatever is  not  in SEARCHFOR. The  d  modifier means to delete found but unreplaced characters. Modifiers are optional; others are available.
Regular expressions Look in the Perl book (see Resources) for an explanation on how to use regular expressions. You can look around elsewhere, at Perl sites, and in other books, for more information and examples. Looking at explained examples can be very helpful in learning how to use regular expressions. (I’ve enclosed some I’ve found useful; see Resources.)
Regular expressions Very powerful mechanism. Often hard to understand at first glance. Can be rather obtuse and frustrating! If one way doesn’t work, keep at it. Most likely there is a way that works!
Resources Advanced Perl Programming Learning Perl Perl in a Nutshell Programming Perl Perl Cookbook Perl Best Practices I use these two  a  lot Highly recommended once you’re experienced. These are all O’Reilly books.
Resources CPAN http://cpan.org Active State Perl http://activestate.com/Products/Download/Download.plex?id=ActivePerl The files listed below are available at http://homepages.wmich.edu/~zimmer/files/eugm2006 datemath.pl some program code for math with dates snippet.grep various regular expressions I’ve found useful Plunging Into Perl.ppt this presentation
Thanks for listening. Questions? [email_address] 269.387.3885 Picture © 2005 by Roy Zimmer

More Related Content

What's hot

Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
Marcos Rebelo
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Sopan Shewale
 
Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013
Prof. Wim Van Criekinge
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
Dave Cross
 
Introduction to Perl and BioPerl
Introduction to Perl and BioPerlIntroduction to Perl and BioPerl
Introduction to Perl and BioPerl
Bioinformatics and Computational Biosciences Branch
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
Manav Prasad
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
Varadharajan Mukundan
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
Ricardo Signes
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
Dave Cross
 
Perl
PerlPerl
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Subroutines
SubroutinesSubroutines
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern Perl
Dave Cross
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
Haim Michael
 
05php
05php05php
Hashes
HashesHashes
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Scala parsers Error Recovery in Production
Scala parsers Error Recovery in ProductionScala parsers Error Recovery in Production
Scala parsers Error Recovery in Production
Alexander Azarov
 

What's hot (20)

Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Introduction to Perl and BioPerl
Introduction to Perl and BioPerlIntroduction to Perl and BioPerl
Introduction to Perl and BioPerl
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
 
Perl
PerlPerl
Perl
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern Perl
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
 
05php
05php05php
05php
 
Hashes
HashesHashes
Hashes
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Scala parsers Error Recovery in Production
Scala parsers Error Recovery in ProductionScala parsers Error Recovery in Production
Scala parsers Error Recovery in Production
 

Viewers also liked

Another Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and PerlAnother Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Roy Zimmer
 
My angelito's total makeover
My angelito's total makeoverMy angelito's total makeover
My angelito's total makeover
Aritazul
 
Batchhow
BatchhowBatchhow
Batchhow
Roy Zimmer
 
Marcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping UpMarcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping Up
Roy Zimmer
 
A Strand of Perls: Some Home Grown Utilities
A Strand of Perls: Some Home Grown UtilitiesA Strand of Perls: Some Home Grown Utilities
A Strand of Perls: Some Home Grown Utilities
Roy Zimmer
 
The Outer Planets Of Our Solar System
The Outer Planets Of Our Solar SystemThe Outer Planets Of Our Solar System
The Outer Planets Of Our Solar System
Mark L.
 
The Physics of God and the Quantum Gravity Theory of Everything
The Physics of God and the Quantum Gravity Theory of EverythingThe Physics of God and the Quantum Gravity Theory of Everything
The Physics of God and the Quantum Gravity Theory of Everything
JamesRedford
 
The Outer Planets Of Our Solar System
The Outer Planets Of Our Solar SystemThe Outer Planets Of Our Solar System
The Outer Planets Of Our Solar System
Mark L.
 
Double page stages
Double page stagesDouble page stages
Double page stages
MitchElsey
 
Voyager Meets MeLCat: MC'ing the Introductions
Voyager Meets MeLCat: MC'ing the IntroductionsVoyager Meets MeLCat: MC'ing the Introductions
Voyager Meets MeLCat: MC'ing the Introductions
Roy Zimmer
 
Ray Owens 04.12.11
Ray Owens 04.12.11Ray Owens 04.12.11
Ray Owens 04.12.11
Ccasati
 
Orientation Session for (New) Presenters and Moderators
Orientation Session for (New) Presenters and ModeratorsOrientation Session for (New) Presenters and Moderators
Orientation Session for (New) Presenters and Moderators
Roy Zimmer
 

Viewers also liked (12)

Another Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and PerlAnother Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
 
My angelito's total makeover
My angelito's total makeoverMy angelito's total makeover
My angelito's total makeover
 
Batchhow
BatchhowBatchhow
Batchhow
 
Marcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping UpMarcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping Up
 
A Strand of Perls: Some Home Grown Utilities
A Strand of Perls: Some Home Grown UtilitiesA Strand of Perls: Some Home Grown Utilities
A Strand of Perls: Some Home Grown Utilities
 
The Outer Planets Of Our Solar System
The Outer Planets Of Our Solar SystemThe Outer Planets Of Our Solar System
The Outer Planets Of Our Solar System
 
The Physics of God and the Quantum Gravity Theory of Everything
The Physics of God and the Quantum Gravity Theory of EverythingThe Physics of God and the Quantum Gravity Theory of Everything
The Physics of God and the Quantum Gravity Theory of Everything
 
The Outer Planets Of Our Solar System
The Outer Planets Of Our Solar SystemThe Outer Planets Of Our Solar System
The Outer Planets Of Our Solar System
 
Double page stages
Double page stagesDouble page stages
Double page stages
 
Voyager Meets MeLCat: MC'ing the Introductions
Voyager Meets MeLCat: MC'ing the IntroductionsVoyager Meets MeLCat: MC'ing the Introductions
Voyager Meets MeLCat: MC'ing the Introductions
 
Ray Owens 04.12.11
Ray Owens 04.12.11Ray Owens 04.12.11
Ray Owens 04.12.11
 
Orientation Session for (New) Presenters and Moderators
Orientation Session for (New) Presenters and ModeratorsOrientation Session for (New) Presenters and Moderators
Orientation Session for (New) Presenters and Moderators
 

Similar to Plunging Into Perl While Avoiding the Deep End (mostly)

Training on php by cyber security infotech (csi)
Training on  php by cyber security infotech (csi)Training on  php by cyber security infotech (csi)
Training on php by cyber security infotech (csi)
Cyber Security Infotech Pvt. Ltd.
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
Dave Cross
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 
lab4_php
lab4_phplab4_php
lab4_php
tutorialsruby
 
lab4_php
lab4_phplab4_php
lab4_php
tutorialsruby
 
Ruby quick ref
Ruby quick refRuby quick ref
Ruby quick ref
Tharcius Silva
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
NBACriteria2SICET
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
NB Veeresh
 
php fundamental
php fundamentalphp fundamental
php fundamental
zalatarunk
 
php
phpphp
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
pooja bhandari
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
 
perl course-in-mumbai
 perl course-in-mumbai perl course-in-mumbai
perl course-in-mumbai
vibrantuser
 
perl course-in-mumbai
perl course-in-mumbaiperl course-in-mumbai
perl course-in-mumbai
vibrantuser
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
Kushaal Singla
 
Synapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on phpSynapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on php
SynapseindiaComplaints
 
Synapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on phpSynapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on php
SynapseindiaComplaints
 
Perl slid
Perl slidPerl slid
Perl slid
pacatarpit
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
phelios
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
Vibrant Technologies & Computers
 

Similar to Plunging Into Perl While Avoiding the Deep End (mostly) (20)

Training on php by cyber security infotech (csi)
Training on  php by cyber security infotech (csi)Training on  php by cyber security infotech (csi)
Training on php by cyber security infotech (csi)
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Ruby quick ref
Ruby quick refRuby quick ref
Ruby quick ref
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
php
phpphp
php
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
 
perl course-in-mumbai
 perl course-in-mumbai perl course-in-mumbai
perl course-in-mumbai
 
perl course-in-mumbai
perl course-in-mumbaiperl course-in-mumbai
perl course-in-mumbai
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Synapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on phpSynapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on php
 
Synapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on phpSynapseindia reviews sharing intro on php
Synapseindia reviews sharing intro on php
 
Perl slid
Perl slidPerl slid
Perl slid
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 

Recently uploaded

20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 

Recently uploaded (20)

20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 

Plunging Into Perl While Avoiding the Deep End (mostly)

  • 1. Plunging Into Perl While Avoiding the Deep End (mostly)
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. Your first program #!/usr/local/bin/perl print &quot;Hello, World&quot;;
  • 8. “Protecting” your program (Unix) By default, your program is not executable. chmod 744 your_program You can execute it as owner of the file, anyone else can only read it.
  • 9. Variables $name can be text or number: a character, a whole page of text, or any kind of number context determines type can go “both” ways
  • 10. Variables, array of @employee Array of $employee variables $employee[0] $employee[1] etc.
  • 11. Variables, hash of $lib{‘thisone’} = “2 days”; $lib{‘thatone’} = “5 days”; Thus can use $grace_period = $lib{$libname} when $libname is thatone, $grace_period is 5 days
  • 12. Variables, list of ($var1, $var2, $var3) = function_that_does_something; This function returns a list of elements. A list is always inside parentheses ().
  • 13. Variables, assigning a value to $var = value or expression $array[n] = something; @array = (); # empty array %hash = (); # empty hash Can be done almost anywhere, anytime.
  • 14. Variable scope, and good practices use strict; Requires that you declare all variables like this: my $var; my $var = something; my @array = (); Also makes Perl check your code. Best Practices!
  • 15. Variable scope, and good practices use strict; my $var; my $var = something; my @array = (); A variable declared like this is visible throughout your program. Best Practices!
  • 16. Variable scope, and good practices use strict; my $var; my $var = something; my @array = (); A “my” declaration within code grouped within { and } is visible only in that section of code; it does not exist elsewhere. Best Practices! Scope: where in a program a variable exists.
  • 17. File input and output (I/O)
  • 18. Using command line arguments Usage: program.pl infile outfile $ARGV[0] $ARGV[1]
  • 19. String manipulation & other stuff substring function
  • 20. String manipulation & other stuff a better substring example
  • 21. String manipulation & other stuff index function, find the location of a string in a string
  • 22. String manipulation & other stuff The split function. Here we split string $l into pieces at every space character. Less common usage: take only 1 st 2 pieces.
  • 23. String manipulation & other stuff Actually, the 2 nd statement should be: $l =~ s/^ +//; The ^ means start looking at the start of the line. find “db ratio” anywhere in $l
  • 24. String manipulation & other stuff Instead of using $inline[n], $inline[n+1], etc., to refer to elements of array @inline , here we can refer to @inline’s elements via $l in this example. Often makes for clearer and simpler code.
  • 25. String manipulation & other stuff An often convenient way of populating an array.
  • 26. String manipulation & other stuff Given $stuff = “this is me”; These are not equivalent: “ print $stuff” ‘ print $stuff’ `print $stuff`
  • 27. String manipulation & other stuff Given $stuff = “this is me”; These are not equivalent: “ print $stuff” is “print this is me” ‘ print $stuff’ `print $stuff`
  • 28. String manipulation & other stuff Given $stuff = “this is me”; These are not equivalent: “ print $stuff” is “print this is me” ‘ print $stuff’ is ‘print $stuff’ `print $stuff`
  • 29. String manipulation & other stuff Given $stuff = “this is me”; `print $stuff` would have the operating system try to execute the command <print this is me>
  • 30. String manipulation & other stuff This form should be used as $something = `O.S. command` Example: $listing = ‘ls *.pl`; The output of this ls command is placed, as possibly a large string, into the variable $listing. This syntax allows powerful processing capabilities within a program.
  • 31. printf, sprintf printf(“%s lines here”, $counter) if $counter is 42, we get 42 lines here for the output
  • 32. printf, sprintf printf(“%c lines here”, $counter) if $counter is 42, we get * lines here for the output, since 42 is the ASCII value for “*”, and we’re printing a c haracter
  • 33. printf, sprintf Some additional string formatting… %s – output length is length($var) %10s – output length is absolutely 10 (right justified) %10.20s – output length is min 10, max 20 %-10.10s – output length is absolutely 10 (left justified) Any padding is with space characters.
  • 34. printf, sprintf Some additional number formatting… %d – output length is length($var) %10d – output length is absolutely 10 (leading space padded) %-10d – left justified, absolutely 10 (trailing space padded) %-10.10d – right justified, absolutely 10 (leading zero padded)
  • 35. printf, sprintf Still more number formatting… %f – output length is length($var) %10.10f – guarantees 10 positions to the right of the decimal (zero padded)
  • 36. printf, sprintf printf whatever outputs to the screen
  • 37. printf, sprintf printf whatever outputs to the screen printf file whatever outputs to that file Ex: printf file (“this is %s fun”, $much); ( print functions just like the above, as to output destination.)
  • 38. printf, sprintf printf whatever outputs to the screen printf file whatever outputs to that file Ex: printf file (“this is %s fun”, $much); ( print functions just like the above, as to output destination.) sprintf is just like any printf, except that its output always goes to a string variable. Ex: $var = sprintf(“this is %s fun”, $much);
  • 39. ratiocheck.pl, what it does When the ratio of sizes of certain files related to a database exceeds a threshold, it’s probably time to do an index regen on that database.
  • 40. ratiocheck.pl, what it does When the ratio of sizes of certain files related to a database exceeds a threshold, it’s probably time to do an index regen on that database. This program computes these ratios for several databases, each with its own threshold, and flags those that are candidates for index regeneration.
  • 41. program dissection – ratiocheck.pl set up some variables two of these are templates for printing
  • 42. program dissection – ratiocheck.pl In line 3 above, a file is slurped, i.e., the entire file is read into an array via the <> mechanism.
  • 43. program dissection – ratiocheck.pl This is a more typical use of the split function. Here, $item is separated into two pieces at the “|” character.
  • 44. program dissection – ratiocheck.pl We want to check every database in alphabetical order. We are then calling the checkit subroutine for each database.
  • 45. program dissection – ratiocheck.pl The system function executes its string as an O.S. command. Here we are mailing a file to two different people.
  • 46. program dissection – ratiocheck.pl This subroutine takes 1 argument.
  • 47. program dissection – ratiocheck.pl Remember our generic templates? Here they are used as a format string for the sprintf function. $generic_path = &quot;/m1/voyager/%s/data/&quot;;
  • 48. program dissection – ratiocheck.pl The –s test returns a file’s size. (There are several dozen different –x file tests.)
  • 49. program dissection – ratiocheck.pl Compute the files’ size ratio with sufficient decimal places.
  • 50. program dissection – ratiocheck.pl means new line , loosely equivalent to a CR, or carriage return. Since we want to print the “%” character, we have to escape it with the “ backslash.
  • 51. program dissection – ratiocheck.pl Here we have a hash reference… we are checking if the ratio is greater than the threshold for the current database.
  • 52. program dissection – ratiocheck.pl This is a busy printf statement… the alert text gets a string, a character, and a string embedded in it .
  • 53. program dissection – ratiocheck.pl The first argument is a string, which is the output of the sprintf statement, which outputs the threshold value for this database.
  • 54. program dissection – ratiocheck.pl The second argument is a character. We print the “%” character, whose ASCII value is 37.
  • 55. program dissection – ratiocheck.pl The third argument is a string. In this case, the string consists of 35 asterisks. A string followed by “xN” will occur N times.
  • 56. ratiocheck.pl, output Here’s what the output looks like:
  • 57. DBI stuff What is it and why might I want it? DBI is the DataBase Interface module for Perl. You will also need the specific DBD (DataBase Driver) module for Oracle. This enables Perl to perform queries against your Voyager database. Both of these should already be on your Voyager box.
  • 58. DBI stuff, how to You need four things to connect to Voyager: machine name your.machine.here.edu username your_username password your_password SID VGER (or LIBR)
  • 59. DBI stuff, how to $dbh is the handle for the database $sth is the handle for the query Create a query…then execute it. NOTE: SQL from Access will most likely NOT work here!
  • 60. DBI stuff, how to Get the data coming from your query.
  • 61. DBI stuff, how to Get the data coming from your query. You’ll need a Perl variable for each column returned in the query. Commonly a list of variables is used; you could also use an array.
  • 62. DBI stuff, how to Get the data coming from your query. You’ll need a Perl variable for each column returned in the query. Commonly a list of variables is used; you could also use an array. Typically, you get your data in a while loop, but you could have $var = $sth->fetchrow_array; when you know you’re getting a single value.
  • 63. DBI stuff, how to When you’re done with a query, you should finish it. This becomes important when you have multiple queries in succession. You can have multiple queries open at the same time. In that case, make the statement handles unique…$sth2, or $sth_patron. Finally, you can close your database connection.
  • 64. CPAN Comprehensive Perl Archive Network http://cpan.org You name it and somebody has probably written a Perl module for it, and you’ll find it here. There are also good Perl links here; look for the Perl Bookmarks link.
  • 65. CPAN Installing modules You need to be root for systemwide installation on Unix systems. On Windows machines, you’ll need to be administrator. You can install them “just for yourself” with a bit of tweaking, and without needing root access. If you’re not a techie, you’ll probably want to find someone who is, to install modules. Installing modules is beyond the scope of this presentation.
  • 66. Perl on your PC You can get Perl for your PC from ActiveState. They typically have two versions available; I recommend the newer one. Get the MSI version. Installation is easy and painless, but it may take some time to complete. A lot of modules are included with this distribution; many additional modules are available. Module installation is made easy via the Perl Package Manager (PPM). Modules not found this way will require manual installation, details of which are beyond the scope of this presentation.
  • 67. Date and Time in Perl, basic ### &quot;create&quot; today's date my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime; This gets the date and time information from the system.
  • 68. Date and Time in Perl, basic ### &quot;create&quot; today's date my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime; my $today = sprintf (&quot;%4.4d.%2.2d.%2.2d&quot;, $year+1900, $month+1, $day); This puts today’s date in “Voyager” format, 2006.04.26
  • 69. Date and Time in Perl The program, datemath.pl, is part of your handout. The screenshot below shows its output.
  • 70. Regular expressions, matching m/PATTERN/gi If the m for matching is not there, it is assumed. The g modifier means to find globally, all occurrences. The i modifier means matching case insensitive. Modifiers are optional; others are available.
  • 71. Regular expressions, substituting s/PATTERN/REPLACEWITH/gi The s says that substitution is the intent. The g modifier means to substitute globally, all occurrences. The i modifier means matching case insensitive. Modifiers are optional; others are available.
  • 72. Regular expressions, translating tr/SEARCHFOR/REPLACEWITH/cd The tr says that translation is the intent. The c modifier means translate whatever is not in SEARCHFOR. The d modifier means to delete found but unreplaced characters. Modifiers are optional; others are available.
  • 73. Regular expressions Look in the Perl book (see Resources) for an explanation on how to use regular expressions. You can look around elsewhere, at Perl sites, and in other books, for more information and examples. Looking at explained examples can be very helpful in learning how to use regular expressions. (I’ve enclosed some I’ve found useful; see Resources.)
  • 74. Regular expressions Very powerful mechanism. Often hard to understand at first glance. Can be rather obtuse and frustrating! If one way doesn’t work, keep at it. Most likely there is a way that works!
  • 75. Resources Advanced Perl Programming Learning Perl Perl in a Nutshell Programming Perl Perl Cookbook Perl Best Practices I use these two a lot Highly recommended once you’re experienced. These are all O’Reilly books.
  • 76. Resources CPAN http://cpan.org Active State Perl http://activestate.com/Products/Download/Download.plex?id=ActivePerl The files listed below are available at http://homepages.wmich.edu/~zimmer/files/eugm2006 datemath.pl some program code for math with dates snippet.grep various regular expressions I’ve found useful Plunging Into Perl.ppt this presentation
  • 77. Thanks for listening. Questions? [email_address] 269.387.3885 Picture © 2005 by Roy Zimmer