perl intro 2 first program
DESCRIPTION
TRANSCRIPT
![Page 1: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/1.jpg)
04/10/23 1ATI Confidential
DataData MiningMining
Perl Brown Bag
Shaun Griffith
March 20, 2006
![Page 2: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/2.jpg)
04/10/23 2
Agenda
•Program Structure
•Slinging Strings
![Page 3: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/3.jpg)
04/10/23 3
First Program
Read a GDF and grab some data
![Page 4: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/4.jpg)
04/10/23 4
Program Structure
Shebang•#!/path/to/perl
•Unix shell runs this program on the script
•Other choices
•DOS:
#!/this/doesn’t/do/much
•Unix – find Perl in your patheval '(exit $?0)' && eval 'exec perl -w -S $0
${1+"$@"}‘ && eval 'exec perl -w -S $0
$argv:q' if 0;# The above invocation finds Perl in the
path, # wherever it may be
![Page 5: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/5.jpg)
04/10/23 5
Program So Far#!/your/perl/here
![Page 6: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/6.jpg)
04/10/23 6
Scriptures
Also known as “strictures”
use strict;
use warnings;
Strict
•Declare all variables before use (“vars”)
•No symbolic references (“refs”)
•Declare all subroutines before use (“subs”)
![Page 7: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/7.jpg)
04/10/23 7
Scriptures…
Warnings•isn’t numeric
•undefined value
Warnings print to STDERR by default
Warnings usually mean program errors or bad data!
![Page 8: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/8.jpg)
04/10/23 8
Program So Far#!/your/perl/here
use strict;
use warnings;
![Page 9: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/9.jpg)
04/10/23 9
Variables
Scalars•Hold a single value
•String
•Number
•Reference (like a pointer)
•Start with $
•$name = “Fred”;
•$age = 17;
•$current_name = \$name;
![Page 10: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/10.jpg)
04/10/23 10
Variables…
Arrays•Hold more than one value (scalar)
•Order is important
•Start with @ or $
•@list = (1,2,3,4,5);
•$list[3] = “Markam”;
•@list[11,28] = (“Red”,”Green”);
•Size: $size = @list;
•Last index: $last = $#list;
![Page 11: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/11.jpg)
04/10/23 11
Variables…
Hashes• Pairs of data: key and value
• No order – No duplicate keys
• Start with % or @ or $
%cost = ( “apples” => 0.45,
“bananas” => 0.55 );
@cost{@fruit} = ( 0.45, 0.55 );
$cost{apples} = 0.45;
• List of keys: @keys = keys %cost;
• Size: $fruit = scalar keys %cost;
![Page 12: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/12.jpg)
04/10/23 12
Reading Files
Perl DWIM (Do What I Mean)• To read files listed on the command line:
while (<>) { do_something_here; }
• “<>” is the “diamond” operator
• If empty, reads from STDIN (a file “handle”)…
• …which defaults to @ARGV
• “<>” automatically opens and closes files
![Page 13: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/13.jpg)
04/10/23 13
Program So Far#!/your/perl/here
use strict;
use warnings;
while (<>)
{
}
![Page 14: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/14.jpg)
04/10/23 14
Matching
To match barcodes:
m/barcode=(\d+)/i;
• m// is the match operator
• barcode= is literal text to match
•\d+ matches one or more digits (0-9)
• () captures matches into $1, $2, etc.
•/i ignores case
To print it out:
print “$1\n”; # \n is end of line
Putting them together:
if ( m/barcode=(\d+)/i )
{ print “$1\n”; }
![Page 15: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/15.jpg)
04/10/23 15
Program So Far#!/your/perl/here
use strict;
use warnings;
while (<>)
{
if ( m/barcode=(\d+)/i )
{ print “$1\n”; }
}
![Page 16: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/16.jpg)
04/10/23 16
More Stuff
Pass/Fail is on the same line:
m/(PASS|FAIL)/i;
•Vertical bar is “or”
Print this out too:
if ( m/(PASS|FAIL)/i )
{ print “$1\n”; }
But let’s print all of that on one line:
if ( m/barcode=(\d+)/i )
{ print “$1\t”;
if ( m/(pass|fail)/i )
{ print "$1”; }
print "\n";
}
![Page 17: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/17.jpg)
04/10/23 17
Program So Far#!/your/perl/here
use strict;
use warnings;
while (<>)
{
if ( m/barcode=(\d+)/i )
{ print “$1\t”;
if ( m/(pass|fail)/i )
{ print "$1”; }
print "\n";
}
}
![Page 18: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/18.jpg)
04/10/23 18
Printing HeadersYou could do this:
print “Barcode\tPF\n”; # \t is tab
…but if spacing is important:
printf “%10s\t%4s\n”, “Barcode”, “ PF ”;
This is the same printf as C.
• %10s
• % starts a field
• 10 gives the width
• s is for strings
• d is for integers
• e/f/g are for real (floats)
Do the same for the other prints if you want…
![Page 19: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/19.jpg)
04/10/23 19
Ta-Da!!!#!/your/perl/here
use strict;use warnings;
# headerprintf “%10s\t%4s\n”, “Barcode”, “ PF ”;
while (<>){
if ( m/barcode=(\d+)/i ){ printf “%10s\t”, $1;
if ( m/(pass|fail)/i ){ printf “%4s”, $1; }print "\n";
}}exit; # redundant, but good for debugger
![Page 20: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/20.jpg)
04/10/23 20
Questions?
Questions on this material?• Reading files• Variables• Matching• Printing
Questions on anything else?• Reading from more than 1 file?• Substitutions?• Loops?• Subroutines?
![Page 21: Perl Intro 2 First Program](https://reader035.vdocument.in/reader035/viewer/2022062616/5499d3bfb479593d4d8b57a9/html5/thumbnails/21.jpg)
04/10/23 21
Next Time
Running Perl
Perl Debugger