Perl Programming Reference Man Page man perlintro man
![Perl Programming Perl Programming](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-1.jpg)
![Reference > Man Page – – – % man perlintro % man perlrun % Reference > Man Page – – – % man perlintro % man perlrun %](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-2.jpg)
![Slides Contents > > > Introduction Scalar data Arrays, List and Hash Control Structures Slides Contents > > > Introduction Scalar data Arrays, List and Hash Control Structures](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-3.jpg)
![Introduction Introduction](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-4.jpg)
![Introduction > Perl – Practical Extraction and Report Language • • • Text manipulation Introduction > Perl – Practical Extraction and Report Language • • • Text manipulation](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-5.jpg)
![The “Hello, World” (1) Perl indicator Optional arguments man perlrun Comment, to the end The “Hello, World” (1) Perl indicator Optional arguments man perlrun Comment, to the end](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-6.jpg)
![The “Hello, World” (2) Parentheses for built-in functions are never required Grab one line The “Hello, World” (2) Parentheses for built-in functions are never required Grab one line](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-7.jpg)
![The “Hello, World” (3) #!/usr/bin/perl print "What is your name? "; $name = <STDIN>; The “Hello, World” (3) #!/usr/bin/perl print "What is your name? "; $name = <STDIN>;](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-8.jpg)
![The “Hello, World” (4) - Array @ Initialization with qw operator man perldata #!/usr/bin/perl The “Hello, World” (4) - Array @ Initialization with qw operator man perldata #!/usr/bin/perl](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-9.jpg)
![Hash % Key value Key can be any scalar value man perldata The “Hello, Hash % Key value Key can be any scalar value man perldata The “Hello,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-10.jpg)
![The “Hello, World” (6) - Regular Expression RE match operator Regular expression man perlre The “Hello, World” (6) - Regular Expression RE match operator Regular expression man perlre](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-11.jpg)
![The “Hello, World” (7) - Subroutine #!/usr/bin/perl print ("Hello, world!n"); print ("Please enter first The “Hello, World” (7) - Subroutine #!/usr/bin/perl print ("Hello, world!n"); print ("Please enter first](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-12.jpg)
![The “Hello, World” (8) - Open file Open a file and assign a file The “Hello, World” (8) - Open file Open a file and assign a file](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-13.jpg)
![The “Hello, World” (9) - Open command #!/usr/bin/perl $subject = "Alert Mail from hello The “Hello, World” (9) - Open command #!/usr/bin/perl $subject = "Alert Mail from hello](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-14.jpg)
![The “Hello, World” (10) - format #!/usr/bin/perl open (FD 1, "data 2. txt") || The “Hello, World” (10) - format #!/usr/bin/perl open (FD 1, "data 2. txt") ||](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-15.jpg)
![Scalar Data Scalar Data](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-16.jpg)
![Scalar data > Number – Perl manipulates number as double-decision floating values – Float Scalar data > Number – Perl manipulates number as double-decision floating values – Float](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-17.jpg)
![Scalar Operators > Operators for Numbers – Arithmetic • +, -, *, / , Scalar Operators > Operators for Numbers – Arithmetic • +, -, *, / ,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-18.jpg)
![Scalar conversion > Number or String ? – Numeric operator • • • Automatically Scalar conversion > Number or String ? – Numeric operator • • • Automatically](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-19.jpg)
![Scalar Variable > Hold single scalar value – Ordinary Assignment • • $a = Scalar Variable > Hold single scalar value – Ordinary Assignment • • $a =](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-20.jpg)
![Array and List Data Array and List Data](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-21.jpg)
![List > List – An ordered scalar data – List literal representation • • List > List – An ordered scalar data – List literal representation • •](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-22.jpg)
![Array (1) > Array – A variable that holds list • @ary = (“a”, Array (1) > Array – A variable that holds list • @ary = (“a”,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-23.jpg)
![Array (2) > Access a list of elements – Slice of array (use @ Array (2) > Access a list of elements – Slice of array (use @](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-24.jpg)
![Array (3) > Related functions – push and pop • • Use array as Array (3) > Related functions – push and pop • • Use array as](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-25.jpg)
![Array (4) > <STDIN> to array – Return all remaining lines up to EOF Array (4) > <STDIN> to array – Return all remaining lines up to EOF](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-26.jpg)
![Hash (1) > Collection of scalar data – – <key, value> pairs Key is Hash (1) > Collection of scalar data – – <key, value> pairs Key is](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-27.jpg)
![Hash (2) > Related functions – keys • • Yield a list of all Hash (2) > Related functions – keys • • Yield a list of all](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-28.jpg)
![Hash (3) – Ex: $h{"tytsai"}= "Tsung-Yi Tsai"; $h{"csie"}="Best department of computer Science"; while (($k, Hash (3) – Ex: $h{"tytsai"}= "Tsung-Yi Tsai"; $h{"csie"}="Best department of computer Science"; while (($k,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-29.jpg)
![Control Structure Control Structure](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-30.jpg)
![if and unless (1) if (expression) { statements-of-if-parts; }else{ statements-of-else-part; } if (expression) { if and unless (1) if (expression) { statements-of-if-parts; }else{ statements-of-else-part; } if (expression) {](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-31.jpg)
![if and unless (2) if (expression) { statements-of-if-parts; } unless (expression) { statements-of-else-parts; } if and unless (2) if (expression) { statements-of-if-parts; } unless (expression) { statements-of-else-parts; }](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-32.jpg)
![while and until # while true, do body while (expression) { statements-of-while-body; } Ex: while and until # while true, do body while (expression) { statements-of-while-body; } Ex:](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-33.jpg)
![do while and do until do { statements-of-do-body; }while expression; do { statements-of-do-body; }until do while and do until do { statements-of-do-body; }while expression; do { statements-of-do-body; }until](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-34.jpg)
![for and foreach for (init; test; update) { statements-of-for-body; } foreach $i (@some_list) { for and foreach for (init; test; update) { statements-of-for-body; } foreach $i (@some_list) {](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-35.jpg)
![last and next statement > last – Like C “break; ” > next – last and next statement > last – Like C “break; ” > next –](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-36.jpg)
![Labeled Block (1) > Labeled block – Give name to block to achieve “goto” Labeled Block (1) > Labeled block – Give name to block to achieve “goto”](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-37.jpg)
![Labeled Block (2) LAB 1: for ($i = 1; $i <= 3; $i++){ LAB Labeled Block (2) LAB 1: for ($i = 1; $i <= 3; $i++){ LAB](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-38.jpg)
![Basic I/O Basic I/O](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-39.jpg)
![Input (1) > Using STDIN – In scalar context, return the next line or Input (1) > Using STDIN – In scalar context, return the next line or](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-40.jpg)
![Input (2) Ex: while ( defined( $line = <STDIN>)) { # process line } Input (2) Ex: while ( defined( $line = <STDIN>)) { # process line }](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-41.jpg)
![Output > Using print – Take a list of strings and send each string Output > Using print – Take a list of strings and send each string](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-42.jpg)
![Predefined variables > man perlvar – – – $_ # default input and pattern-searching Predefined variables > man perlvar – – – $_ # default input and pattern-searching](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-43.jpg)
![Regular Expression Regular Expression](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-44.jpg)
![Regular Expression > RE – A pattern to be matched against a string • Regular Expression > RE – A pattern to be matched against a string •](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-45.jpg)
![Regular Expression Pattern - Single-Character Pattern > Match single character – /a/ , /[abc]/, Regular Expression Pattern - Single-Character Pattern > Match single character – /a/ , /[abc]/,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-46.jpg)
![Regular Expression Pattern - Grouping Patterns (1) > Match more than one character – Regular Expression Pattern - Grouping Patterns (1) > Match more than one character –](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-47.jpg)
![Regular Expression Pattern - Grouping Patterns (2) – Parentheses as memory • • • Regular Expression Pattern - Grouping Patterns (2) – Parentheses as memory • • •](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-48.jpg)
![Interpolation in RE > Variable interpolation $sentence = "Every good bird does fly"; $what Interpolation in RE > Variable interpolation $sentence = "Every good bird does fly"; $what](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-49.jpg)
![Special variables in RE > $1, $2, $3 … – Set to the same Special variables in RE > $1, $2, $3 … – Set to the same](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-50.jpg)
![Operators before // - Substitution > Substitution – s/pattern/replacement/ – Ex: $_ = “foot Operators before // - Substitution > Substitution – s/pattern/replacement/ – Ex: $_ = “foot](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-51.jpg)
![Operators before // - Translation > Translation – tr/search-list/replacement-list/ – Ex: $message = “This Operators before // - Translation > Translation – tr/search-list/replacement-list/ – Ex: $message = “This](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-52.jpg)
![Related functions > split – You can specify the delimit as regular expression – Related functions > split – You can specify the delimit as regular expression –](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-53.jpg)
![Subroutines Subroutines](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-54.jpg)
![Subroutine > Definition – With “sub” keyword – Subroutine definition is global > Return Subroutine > Definition – With “sub” keyword – Subroutine definition is global > Return](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-55.jpg)
![Arguments > @_ – Contain the subroutine invocation arguments – @_ is private to Arguments > @_ – Contain the subroutine invocation arguments – @_ is private to](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-56.jpg)
![Variables in subroutine > Private variables – Use “my” operator to create a list Variables in subroutine > Private variables – Use “my” operator to create a list](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-57.jpg)
![File File](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-58.jpg)
![Open and close (1) > Automatically opened file handlers – STDIN, STDOUT, STDERR > Open and close (1) > Automatically opened file handlers – STDIN, STDOUT, STDERR >](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-59.jpg)
![Open and close (2) > Open with redirection – Ex: #!/usr/bin/perl open (FD, "ypcat Open and close (2) > Open with redirection – Ex: #!/usr/bin/perl open (FD, "ypcat](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-60.jpg)
![File test $name = "index. html"; if (-e $name) { print "file: $name existsn"; File test $name = "index. html"; if (-e $name) { print "file: $name existsn";](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-61.jpg)
![Directory > Use “chdir” function – Change current directory – Return successful or not Directory > Use “chdir” function – Change current directory – Return successful or not](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-62.jpg)
![File and Directory Manipulation > Removing file – unlink(filename-list); – Ex: unlink(“data 1. txt”, File and Directory Manipulation > Removing file – unlink(filename-list); – Ex: unlink(“data 1. txt”,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-63.jpg)
![Format Format](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-64.jpg)
![Format > Format – Report writing template – Define • • Constant part (headers, Format > Format – Report writing template – Define • • Constant part (headers,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-65.jpg)
![Define a format > Use “format” keyword – syntax – format name = fieldline Define a format > Use “format” keyword – syntax – format name = fieldline](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-66.jpg)
![Invoking a format > Through “write” function – write function will write stuff into Invoking a format > Through “write” function – write function will write stuff into](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-67.jpg)
![Fieldholders > @<<< – It means “ 5 character, left justified” > Text fields Fieldholders > @<<< – It means “ 5 character, left justified” > Text fields](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-68.jpg)
![The Top-of-page format > Let report to fit page-size printing device – Perl will The Top-of-page format > Let report to fit page-size printing device – Perl will](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-69.jpg)
![Changing Defaults > Change default file handler – Use select function – print without Changing Defaults > Change default file handler – Use select function – print without](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-70.jpg)
![Process Management Process Management](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-71.jpg)
![Using system() function > system function – system( ) will fork a /bin/sh shell Using system() function > system function – system( ) will fork a /bin/sh shell](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-72.jpg)
![Using Backquote > `` – Execute the command replace itself with execution – result Using Backquote > `` – Execute the command replace itself with execution – result](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-73.jpg)
![Using Process as Filehandler > We can either – Open and capture the output Using Process as Filehandler > We can either – Open and capture the output](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-74.jpg)
![Using fork() function > Just as fork(2) do – Create a clone of the Using fork() function > Just as fork(2) do – Create a clone of the](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-75.jpg)
![Sending and Receiving Signals (1) > Catch the signal in your program – Using Sending and Receiving Signals (1) > Catch the signal in your program – Using](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-76.jpg)
![Sending and Receiving Signals (2) > Ex: #!/usr/bin/perl $SIG{'TERM'} = 'my_TERM_catcher'; print "before sending Sending and Receiving Signals (2) > Ex: #!/usr/bin/perl $SIG{'TERM'} = 'my_TERM_catcher'; print "before sending](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-77.jpg)
![System Information Manipulation System Information Manipulation](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-78.jpg)
![User information (1) > Using getpwuid() or getpwnam() – Pass uid to getpwuid( ) User information (1) > Using getpwuid() or getpwnam() – Pass uid to getpwuid( )](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-79.jpg)
![User information (2) > Sequential access to passwd – Use setpwent( ), getpwent( ) User information (2) > Sequential access to passwd – Use setpwent( ), getpwent( )](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-80.jpg)
![String Manipulation String Manipulation](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-81.jpg)
![Related functions > Find a substring – index(original-str, sub-str) $where 1 $where 2 $where Related functions > Find a substring – index(original-str, sub-str) $where 1 $where 2 $where](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-82.jpg)
![Sort > Sort – Without any modification, sorting – – is based on ASCII Sort > Sort – Without any modification, sorting – – is based on ASCII](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-83.jpg)
![Built-in functions Built-in functions](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-84.jpg)
![Built-in functions > For Scalars – chomp, chop, index, length, sprintf, substr, … > Built-in functions > For Scalars – chomp, chop, index, length, sprintf, substr, … >](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-85.jpg)
- Slides: 85
![Perl Programming Perl Programming](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-1.jpg)
Perl Programming
![Reference Man Page man perlintro man perlrun Reference > Man Page – – – % man perlintro % man perlrun %](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-2.jpg)
Reference > Man Page – – – % man perlintro % man perlrun % man perldata % man perlop % man perlsub % man perlfunc % man perlvar % man perlsyn % man perlre % man perlopentut % man perlform (brief introduction and overview) (how to execute perl) (data structure) (operators and precedence) (subroutines) (built-in functions) (predefined variables) (syntax) (regular expression) (File I/O) (Format) 2
![Slides Contents Introduction Scalar data Arrays List and Hash Control Structures Slides Contents > > > Introduction Scalar data Arrays, List and Hash Control Structures](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-3.jpg)
Slides Contents > > > Introduction Scalar data Arrays, List and Hash Control Structures Basic I/O Regular Expression Subroutine File Format Process management System information manipulation String manipulation 3
![Introduction Introduction](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-4.jpg)
Introduction
![Introduction Perl Practical Extraction and Report Language Text manipulation Introduction > Perl – Practical Extraction and Report Language • • • Text manipulation](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-5.jpg)
Introduction > Perl – Practical Extraction and Report Language • • • Text manipulation Web development Network programming GUI development … > Easy to use – White space between tokens > Compiled and interpreted – Won’t get a syntax error once the program is started 5
![The Hello World 1 Perl indicator Optional arguments man perlrun Comment to the end The “Hello, World” (1) Perl indicator Optional arguments man perlrun Comment, to the end](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-6.jpg)
The “Hello, World” (1) Perl indicator Optional arguments man perlrun Comment, to the end of line #!/usr/bin/perl # My First Perl Program print ("Hello, World!n"); Built-in function man perlfunc Run Perl Program % perl hello. pl %. /hello. pl C-like "; " termination (even no +x mode or indicator) ( +x mode and perl indicator) 6
![The Hello World 2 Parentheses for builtin functions are never required Grab one line The “Hello, World” (2) Parentheses for built-in functions are never required Grab one line](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-7.jpg)
The “Hello, World” (2) Parentheses for built-in functions are never required Grab one line of input #!/usr/bin/perl print “What is your name ? ”; $name = <STDIN>; chomp ($name); print ("Hello, $name!n"); Scalar variable man perldata Script-like variable embedding Remove newline 7
![The Hello World 3 usrbinperl print What is your name name STDIN The “Hello, World” (3) #!/usr/bin/perl print "What is your name? "; $name = <STDIN>;](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-8.jpg)
The “Hello, World” (3) #!/usr/bin/perl print "What is your name? "; $name = <STDIN>; chomp $name; if ($name eq "tytsai") { print ("Hello, tytsai! NA slides !!n"); }else{ print ("Hello, $name!n"); } if-else man perlsyn Operator man perlop 8
![The Hello World 4 Array Initialization with qw operator man perldata usrbinperl The “Hello, World” (4) - Array @ Initialization with qw operator man perldata #!/usr/bin/perl](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-9.jpg)
The “Hello, World” (4) - Array @ Initialization with qw operator man perldata #!/usr/bin/perl @pre = ("廁所", "教室", "操場"); @post = qw(放屁 大喊我愛你 喔耶); $i = 0; $j = 0; for ($i = 0; $i < @pre; $i++){ for ($j = 0; $j < @post; $j++){ print ("I go to $pre[$i] to do $post[$j]!n"); } } Num of elements Subscript reference with $ 9
![Hash Key value Key can be any scalar value man perldata The Hello Hash % Key value Key can be any scalar value man perldata The “Hello,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-10.jpg)
Hash % Key value Key can be any scalar value man perldata The “Hello, World” (5) - Hash #!/usr/bin/perl %toy = qw( mom dad son dog 5 ); judy chiky freaky miky ordinary print "enter key: "; $mykey = <STDIN>; chomp ($mykey); print "$toy{$mykey} plays $mykeyn"; Subscript reference with $ Specify key with {} 10
![The Hello World 6 Regular Expression RE match operator Regular expression man perlre The “Hello, World” (6) - Regular Expression RE match operator Regular expression man perlre](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-11.jpg)
The “Hello, World” (6) - Regular Expression RE match operator Regular expression man perlre #!/usr/bin/perl $name 1 $name 2 $name 3 $result 1 $result 2 = = = "tytsai"; "Tytsa. I"; "Tytsasa. I"; $name 1 =~ /^tytsai/; $name 2 =~ /^tytsai/i; print ("Result 1 = $result 1, name 1 = $name 1n"); print ("Result 2 = $result 2, name 2 = $name 2n"); $result 3 = $name 1 =~ tr/a-z/A-Z/; $result 4 = $name 3 =~ s/sa/SASASA/g; print ("Result 3 = $result 3, name 1 = $name 1n"); print ("Result 4 = $result 4, name 3 = $name 3n"); Translation operator Substitution operator 11
![The Hello World 7 Subroutine usrbinperl print Hello worldn print Please enter first The “Hello, World” (7) - Subroutine #!/usr/bin/perl print ("Hello, world!n"); print ("Please enter first](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-12.jpg)
The “Hello, World” (7) - Subroutine #!/usr/bin/perl print ("Hello, world!n"); print ("Please enter first number: "); $n 1 = <STDIN>; chomp ($n 1); print ("Please enter second number: "); $n 2 = <STDIN>; chomp ($n 2); print add($n 1, $n 2) ; print "n"; Local variable within block sub add { my($sub_n 1, $sub_n 2) = @_; return $sub_n 1 + $sub_n 2; } Subroutine definition man perlsub Subroutine parameters array man perlvar 12
![The Hello World 8 Open file Open a file and assign a file The “Hello, World” (8) - Open file Open a file and assign a file](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-13.jpg)
The “Hello, World” (8) - Open file Open a file and assign a file descriptor Logical OR operator Built-in “die” function #!/usr/bin/perl openfile(); sub openfile { open (FD 1, "data. txt") || die "can't open file: $!"; } while( defined ($line = <FD 1>) ) { print ("$line"); } Read one line via file handler Use defined() to test whether undef Predefined variable System error message 13
![The Hello World 9 Open command usrbinperl subject Alert Mail from hello The “Hello, World” (9) - Open command #!/usr/bin/perl $subject = "Alert Mail from hello](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-14.jpg)
The “Hello, World” (9) - Open command #!/usr/bin/perl $subject = "Alert Mail from hello 9. pl"; $address = "tytsai@csie. nctu. edu. tw"; mailsub($subject, $address); sub mailsub { my ($sub, $add) = @_; } Print to different file descriptor open MAILFD, "| mail -s "$sub" $add"; print MAILFD "Nothing more than a wordn"; close MAILFD; Open a command via pipe symbol 14
![The Hello World 10 format usrbinperl open FD 1 data 2 txt The “Hello, World” (10) - format #!/usr/bin/perl open (FD 1, "data 2. txt") ||](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-15.jpg)
The “Hello, World” (10) - format #!/usr/bin/perl open (FD 1, "data 2. txt") || die "can't open file: $!"; while (defined($line = <FD 1>)){ ($name, $age, $school) = split(" ", $line); write; } Field definition line man perlform close (FD 1) || die "can't close file: $!"; format STDOUT = @<<<<<<< $name, $age, $school. format STDOUT_TOP = Name Age School =======. Field value line End of format definition Top-of-page format definition 15
![Scalar Data Scalar Data](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-16.jpg)
Scalar Data
![Scalar data Number Perl manipulates number as doubledecision floating values Float Scalar data > Number – Perl manipulates number as double-decision floating values – Float](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-17.jpg)
Scalar data > Number – Perl manipulates number as double-decision floating values – Float / Integer constants, such as: • 1. 25, -6. 8, 6. 23 e 23, 12, -8, 0377, 0 xff > String – Sequence of characters – Single-Quoted Strings • '$a is still $a', 'don't', 'hellon' – Double-Quoted Strings ( variable with interpolation) • • “$a will be replacedn” Escape characters > n, t, r, f, b, a 17
![Scalar Operators Operators for Numbers Arithmetic Scalar Operators > Operators for Numbers – Arithmetic • +, -, *, / ,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-18.jpg)
Scalar Operators > Operators for Numbers – Arithmetic • +, -, *, / , %, **, ++, -- – Logical comparison • <, <=, ==, >, != > Operator for Strings – Concatenation “. ” • “hello”. “ “. “world” – Repetition “x” • “abc” x 4 abcabc – Comparison • lt, le, eq, ge, gt, ne 18
![Scalar conversion Number or String Numeric operator Automatically Scalar conversion > Number or String ? – Numeric operator • • • Automatically](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-19.jpg)
Scalar conversion > Number or String ? – Numeric operator • • • Automatically convert to equivalent numeric value Trailing nonnumeric are ignored Ex: > “ 123. 45 abc” will be 123. 45 – String operator • • Automatically convert to equivalent string Ex: > “x”. (4*5) will be “x 20” 19
![Scalar Variable Hold single scalar value Ordinary Assignment a Scalar Variable > Hold single scalar value – Ordinary Assignment • • $a =](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-20.jpg)
Scalar Variable > Hold single scalar value – Ordinary Assignment • • $a = 17 $b = $a + 3 – Binary assignment operators • • $a += 5 is the same as $a = $a + 5 -=, *=, /=, %= , **=, . = > $str = $str. “. dat” – Autoincrement and autodecrement • ++$a, $a++ 20
![Array and List Data Array and List Data](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-21.jpg)
Array and List Data
![List List An ordered scalar data List literal representation List > List – An ordered scalar data – List literal representation • •](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-22.jpg)
List > List – An ordered scalar data – List literal representation • • Comma-separated values Ex: > (1, 2, 3) > (“abc”, 4. 8) > ($a, 8, 9, “hello”) – List constructor operator • Ex: > (1. . 5) > (1. 2. . 4. 2) > (2. . 5, 10, 12) > (1. 3. 1) > ($a. . $b) same as (1, 2, 3, 4, 5) same as (1. 2, 2. 2, 3. 2, 4. 2) same as (2, 3, 4, 5, 10, 12) same as (1. 3, 2, 3) depend on values of $a and $b 22
![Array 1 Array A variable that holds list ary a Array (1) > Array – A variable that holds list • @ary = (“a”,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-23.jpg)
Array (1) > Array – A variable that holds list • @ary = (“a”, “b”, “c”); @ary = qw(a b c); @ary 2 = @ary 3 = (4. 5, @ary 2, 6. 7) # (4. 5, “a”, “b”, “c”, 6. 7) • $count = @ary 3; # 5, length of @ary 3 • • ($a, ($d, ($e, # swap # $d = $a, @ary 4 = ($b, $c) # $e = $ary 4[0], others to @ary 5 • ($first) = @ary 3; # $first = $ary 3[0] • print $ary 3[-1] print $ary 3[$#ary 3] # print 6. 7, $#ary 3 is the last index • • • $b, $c) = (1, 2, 3) $b) = ($b, $a) @ary 4) = ($a, $b, $c) @ary 5) = @ary 4 23
![Array 2 Access a list of elements Slice of array use Array (2) > Access a list of elements – Slice of array (use @](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-24.jpg)
Array (2) > Access a list of elements – Slice of array (use @ prefix, not $) • • • @a[0, 1] = @[1, 0] @a[0, 1, 2] = @[1, 1, 1] @a[1, 2] = (9, 10) > Beyond the index – Access will get “undef” • • @ary = (3, 4, 5) $a = $ary[8]; – Assign will extend the array • • @ary= (3, 4, 5) $ary[5] = “hi” # (1, 2, 3, undef, “hi”) 24
![Array 3 Related functions push and pop Use array as Array (3) > Related functions – push and pop • • Use array as](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-25.jpg)
Array (3) > Related functions – push and pop • • Use array as a stack Ex: > push(@ary, $new); > push(@ary, $new, 2, $two); > $top = pop(@ary); # @ary = ($new, @ary) # multiple push – reverse • • – sort • • Reverse the order of the elements Ex: > @a = reverse(@a); > @a = reverse(@b); Sort elements as strings in ascending ASCII order Ex: > @a = (1, 2, 4, 8, 16, 32, 64) > @a = sort(@a); – chomp • • # gets 1, 16, 2, 32, 4, 64, 8 Do chomp to every elements of array Ex: > chomp(@ary); 25
![Array 4 STDIN to array Return all remaining lines up to EOF Array (4) > <STDIN> to array – Return all remaining lines up to EOF](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-26.jpg)
Array (4) > <STDIN> to array – Return all remaining lines up to EOF – Ex: • @a = <STDIN>; # press Ctrl + D > Interpolation of array – Elements are interpolated in sequence with “ ” – Ex: • • @ary = (“a”, “bb”, “ccc”, 1, 2, 3); $all = “Now for @ary here!”; > “Now for a bb ccc 1 2 3 here!” • $all = “Now for @ary[2, 3] here!”; > “Now for ccc 1 here!” 26
![Hash 1 Collection of scalar data key value pairs Key is Hash (1) > Collection of scalar data – – <key, value> pairs Key is](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-27.jpg)
Hash (1) > Collection of scalar data – – <key, value> pairs Key is the string index, value is any scalar data Defined by “%” symbol, accessed by $ with {} Ex: • • • $h{“aaa”} = “bbb” $h{234. 5} = 456. 7 print $h{“aaa”} # <“aaa”, “bbb”> # <“ 234. 5”, 456. 7> > Hash assignment – – – @a = %h # array a is (“aaa”, “bbb”, “ 234. 5”, “ 456. 7” %h 2 = @a # h 2 is like h %h 3 = %h # h 3 is like h %h 4 = (“aaa”, “bbb”, “ 234. 5”, “ 456. 7”); %h 5 = reverse %h 2 # construct hash with key and value swapped 27
![Hash 2 Related functions keys Yield a list of all Hash (2) > Related functions – keys • • Yield a list of all](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-28.jpg)
Hash (2) > Related functions – keys • • Yield a list of all the current keys in hash Ex: > @list = keys(%h); # @list = (“aaa”, “ 234. 5”) – values • • Yield a list of all the current values in hash Ex: > @vals = values(%h); # @vals = (“bbb”, 456. 7); – each • Return key-value pair until all elements have been accessed – delete • Remove hash elements 28
![Hash 3 Ex htytsai TsungYi Tsai hcsieBest department of computer Science while k Hash (3) – Ex: $h{"tytsai"}= "Tsung-Yi Tsai"; $h{"csie"}="Best department of computer Science"; while (($k,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-29.jpg)
Hash (3) – Ex: $h{"tytsai"}= "Tsung-Yi Tsai"; $h{"csie"}="Best department of computer Science"; while (($k, $v) = each (%h)) { print "$k is the key of $vn"; } delete $h{"tytsai"}; 29
![Control Structure Control Structure](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-30.jpg)
Control Structure
![if and unless 1 if expression statementsofifparts else statementsofelsepart if expression if and unless (1) if (expression) { statements-of-if-parts; }else{ statements-of-else-part; } if (expression) {](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-31.jpg)
if and unless (1) if (expression) { statements-of-if-parts; }else{ statements-of-else-part; } if (expression) { statements-of-if-parts; }elsif(expression 2){ statements-of-elsif-parts; }else{ statements-of-else-part; } Ex: print "how old are your? "; $age = <STDIN>; if ($age < 18) { print "Young lady!!n"; }else{ print "Such a nice dayn"; } 31
![if and unless 2 if expression statementsofifparts unless expression statementsofelseparts if and unless (2) if (expression) { statements-of-if-parts; } unless (expression) { statements-of-else-parts; }](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-32.jpg)
if and unless (2) if (expression) { statements-of-if-parts; } unless (expression) { statements-of-else-parts; } Ex: print "how old are your? "; $age = <STDIN>; if ($age < 18) { print "Young lady!!n"; } unless ($age < 18) { print "Such a nice dayn"; } Truth is based on string value in scalar context: "0" , "" or undef are false, others are true 0, "0", "" , undef are false 1, "1", "00", "0. 000" are true 32
![while and until while true do body while expression statementsofwhilebody Ex while and until # while true, do body while (expression) { statements-of-while-body; } Ex:](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-33.jpg)
while and until # while true, do body while (expression) { statements-of-while-body; } Ex: # while not true, do body until (expression) { statements-of-until-body; } while ($n > 0){ print "At one time, I were $n years old. n"; $n--; } print "how old are your? "; $n = <STDIN>; until ($n > 18){ print "I am $n++, I want to be man in future. n"; } 33
![do while and do until do statementsofdobody while expression do statementsofdobody until do while and do until do { statements-of-do-body; }while expression; do { statements-of-do-body; }until](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-34.jpg)
do while and do until do { statements-of-do-body; }while expression; do { statements-of-do-body; }until expression; Ex: $a = 10; do { print "now is $an"; $a--; }while $a > 0; $a = 0; do{ print "now is $an"; $a++; }until $a > 10; 34
![for and foreach for init test update statementsofforbody foreach i somelist for and foreach for (init; test; update) { statements-of-for-body; } foreach $i (@some_list) {](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-35.jpg)
for and foreach for (init; test; update) { statements-of-for-body; } foreach $i (@some_list) { statements-of-foreach; } Ex: for ($i = 1; $i <= 10; $i++){ print "$i "; } @a = (1, 2, 3, 4, 5); foreach $b (reverse @a){ print $b; } 35
![last and next statement last Like C break next last and next statement > last – Like C “break; ” > next –](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-36.jpg)
last and next statement > last – Like C “break; ” > next – Like C “continue”; > redo – Jump to the beginning of the current block without revaluating the control expression $n = 6; while($n > 0){ print "first, $nn"; $n--; if($n == 3){ print "second, $nn"; redo; } print "third, $nn"; } first, 6 third, 5 first, 5 third, 4 first, 4 second, 3 first, 3 third, 2 first, 2 third, 1 first, 1 third, 0 36
![Labeled Block 1 Labeled block Give name to block to achieve goto Labeled Block (1) > Labeled block – Give name to block to achieve “goto”](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-37.jpg)
Labeled Block (1) > Labeled block – Give name to block to achieve “goto” purpose – Use “last”, “next”, “redo” to goto any labeled block • • • last: immediately exist the loop in question next: skip the rest of the current iteration of loop redo: restart the loop without evaluating 37
![Labeled Block 2 LAB 1 for i 1 i 3 i LAB Labeled Block (2) LAB 1: for ($i = 1; $i <= 3; $i++){ LAB](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-38.jpg)
Labeled Block (2) LAB 1: for ($i = 1; $i <= 3; $i++){ LAB 2: for($j = 1; $j <= 3; $j++){ LAB 3: for($k = 1; $k <= 3; $k++){ print "i = $i, j = $j, k = $kn"; if(($i == 1)&&($j == 2)&&($k == 3)){ last LAB 2; } if(($i == 2)&&($j == 3)&&($k == 1)){ next LAB 1; } if(($i == 3)&&($j == 1)&&($k == 2)){ next LAB 2; } } Result: i = 1, j = 1, k = 1 i = 1, j = 1, k = 2 i = 1, j = 1, k = 3 i = 1, j = 2, k = 1 i = 1, j = 2, k = 2 i = 1, j = 2, k = 3 i = 2, j = 1, k = 1 i = 2, j = 1, k = 2 i = 2, j = 1, k = 3 i = 2, j = 2, k = 1 i = 2, j = 2, k = 2 i = 2, j = 2, k = 3 i = 2, j = 3, k = 1 i = 3, j = 1, k = 2 i = 3, j = 2, k = 1 i = 3, j = 2, k = 2 i = 3, j = 2, k = 3 i = 3, j = 3, k = 1 i = 3, j = 3, k = 2 i = 3, j = 3, k = 3 38
![Basic IO Basic I/O](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-39.jpg)
Basic I/O
![Input 1 Using STDIN In scalar context return the next line or Input (1) > Using STDIN – In scalar context, return the next line or](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-40.jpg)
Input (1) > Using STDIN – In scalar context, return the next line or undef – In list context, return all remaining lines as a list > Using diamond operator “<>” – Like STDIN, but diamond operator gets data from the files specified on the command line • Command line arguments will go to @ARGV and diamond operator looks @ARGV 40
![Input 2 Ex while defined line STDIN process line Input (2) Ex: while ( defined( $line = <STDIN>)) { # process line }](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-41.jpg)
Input (2) Ex: while ( defined( $line = <STDIN>)) { # process line } while ( <STDIN> ){ # process $_ } @ARGV = (“aaa. txt”, “bbb. txt”, “ccc. txt”); while (<>){ # this loop will gets lines from these three files # process $_ } 41
![Output Using print Take a list of strings and send each string Output > Using print – Take a list of strings and send each string](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-42.jpg)
Output > Using print – Take a list of strings and send each string to stdout in turn • Ex: print (“hello”, $abc, “ worldn”); > Using printf – C-like printf • Ex: printf(“%15 s, %5 d, %20. 2 fn”, $s, $n, $r); 42
![Predefined variables man perlvar default input and patternsearching Predefined variables > man perlvar – – – $_ # default input and pattern-searching](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-43.jpg)
Predefined variables > man perlvar – – – $_ # default input and pattern-searching space $, # output field separator for print $/ # input record separator (newline) $$ # pid $<, $> # uid and euid $0 # program name %ENV # Current environment variables %SIG # signal handlers for various signals @ARGV # command line arguments @_ # parameter list $ARGV # current filename when reading from <> STDIN, STDOUT, STDERR 43
![Regular Expression Regular Expression](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-44.jpg)
Regular Expression
![Regular Expression RE A pattern to be matched against a string Regular Expression > RE – A pattern to be matched against a string •](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-45.jpg)
Regular Expression > RE – A pattern to be matched against a string • • Sometimes you just want to know the result Sometimes you want to find and replace it Ex: # match the pattern “^tytsai” against $_ while (<>) { if ( /^tytsai/ ){ print $_; } } 45
![Regular Expression Pattern SingleCharacter Pattern Match single character a abc Regular Expression Pattern - Single-Character Pattern > Match single character – /a/ , /[abc]/,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-46.jpg)
Regular Expression Pattern - Single-Character Pattern > Match single character – /a/ , /[abc]/, /[abc]]/ , /[0 -9]/, /[a-z. A-Z 0 -9]/, /[^0 -9]/ – Predefined Character Class Abbreviations • digit > d means [0 -9] > D means [^0 -9] • word > w means [a-z. A-Z 0 -9_] > W means [^a-z. A-Z 0 -9_] • # digit # non-digit # word char # non word space > s means [ rtnf] > S means [^ rtnf] # space char # non-space Programming Perl, P. 161 or man perlre 46
![Regular Expression Pattern Grouping Patterns 1 Match more than one character Regular Expression Pattern - Grouping Patterns (1) > Match more than one character –](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-47.jpg)
Regular Expression Pattern - Grouping Patterns (1) > Match more than one character – Sequence • • Match a sequence of characters Ex: /abc/ # match an a followed by b , by c – Multipliers • • • * + ? {a, b} {a, } {a} /fo+ba? r/ /a. {5}b/ # # # >= 0, {0, } >= 1, {1, } 0 or 1, {0, 1} a ~ b, inclusive >= 5 =5 # f, one or more o, b, optional a, r # a, any five non-newline char, b 47
![Regular Expression Pattern Grouping Patterns 2 Parentheses as memory Regular Expression Pattern - Grouping Patterns (2) – Parentheses as memory • • •](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-48.jpg)
Regular Expression Pattern - Grouping Patterns (2) – Parentheses as memory • • • Still match the pattern, but remember the matched string for future reference Use and number to reference the memorized part Ex: > /a(. *)b1 c/ • # match a. TYb. TYc or abc, not a. Eb. EEc Use (? : . . ) instead (. . ) to not memorize – Alternation • • • Match exactly one of the alternatives Use | to specify alternatives Ex: > /red|blue|green/ 48
![Interpolation in RE Variable interpolation sentence Every good bird does fly what Interpolation in RE > Variable interpolation $sentence = "Every good bird does fly"; $what](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-49.jpg)
Interpolation in RE > Variable interpolation $sentence = "Every good bird does fly"; $what = "bird"; $what 2 = "[bw]ird"; if ($sentence =~ /$what/) { print "I saw $what n"; } if ($sentence =~ /$what 2/) { print "I saw $what n"; } – Use U quoting escape to deal with non-aphanumeric char $sentence = "Every good bird does fly"; $what 2 = "[bw]ird"; if ($sentence =~ /Q$what 2E/) { print "I saw $what n"; } 49
![Special variables in RE 1 2 3 Set to the same Special variables in RE > $1, $2, $3 … – Set to the same](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-50.jpg)
Special variables in RE > $1, $2, $3 … – Set to the same value as 1, 2, 3 … when memorizing – Ex: $_ = “this is a test”; /(w+)W+(w+)/; # match first two words, # now, $1 = “this”, $2 = “is” ($first, $second) = /(w+)W+(w+)/; > $`, $&, $’ – Store before-matched, after-matched strings – Ex: $_ = “this is a sample string”; /sa. *le/; # now, $`= “this is a ”, # $& = “sample” # $’ = “ string” 50
![Operators before Substitution Substitution spatternreplacement Ex foot Operators before // - Substitution > Substitution – s/pattern/replacement/ – Ex: $_ = “foot](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-51.jpg)
Operators before // - Substitution > Substitution – s/pattern/replacement/ – Ex: $_ = “foot fool buffoon”; s/foo/bar/g; #now, $_ = “bart barl bufbarn” $sc = “this is a test”; $sc =~ s/(w+)/<$1>/g; # now, $sc = “<this> <a> <test>” $war 3 = “WAR War war”; $war 3 =~ s/war/peace/gi”; # now $war 3 = “peace”; 51
![Operators before Translation Translation trsearchlistreplacementlist Ex message This Operators before // - Translation > Translation – tr/search-list/replacement-list/ – Ex: $message = “This](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-52.jpg)
Operators before // - Translation > Translation – tr/search-list/replacement-list/ – Ex: $message = “This is a secret”; $message =~ tr/A-Za-z/N-ZA-Mn-za-m/; # rotate right 13 encrypt $word = “bookkeeper”; $word =~ tr/a-z. A-Z//s; # squash duplicate, $word = “bokeper” $me = “TThi. SS a TTTest”; $me =~ tr/TS/#!/s; # $me = “#hi! i! a #est” $he = “"abc@$%"; $he =~ tr/@$%//d; $it =“ 0123456789”; $it =~ tr/0 -9/987654/d; # delete found but not given a replacement # now, $he = “abc” # now, $it = “ 987654” 52
![Related functions split You can specify the delimit as regular expression Related functions > split – You can specify the delimit as regular expression –](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-53.jpg)
Related functions > split – You can specify the delimit as regular expression – Unmatched string will form a list – Ex: $message = sshd: *: 22: Secure Shell Daemon: /var/empty: /usr/sbin/nologin @fields = split(": ", $message); > join – Take a glue and list to form a string – Ex: $original = join(“: ”, @fields); 53
![Subroutines Subroutines](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-54.jpg)
Subroutines
![Subroutine Definition With sub keyword Subroutine definition is global Return Subroutine > Definition – With “sub” keyword – Subroutine definition is global > Return](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-55.jpg)
Subroutine > Definition – With “sub” keyword – Subroutine definition is global > Return value – Either single scalar data or a list Ex: $a = 5; $b = 10; $c = ADD($a, $b); @d = LIST_TWO($a, $b); sub ADD{ my($n 1, $n 2) = @_; return $n 1 + $n 2; } sub LIST_TWO{ my($n 1, $n 2) = @_; return ($n 1, $n 2); } 55
![Arguments Contain the subroutine invocation arguments is private to Arguments > @_ – Contain the subroutine invocation arguments – @_ is private to](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-56.jpg)
Arguments > @_ – Contain the subroutine invocation arguments – @_ is private to the subroutine • Nested subroutine invocation gets its own @_ – $_[0], $_[1], …, $_[$#_] to access individual arguments 56
![Variables in subroutine Private variables Use my operator to create a list Variables in subroutine > Private variables – Use “my” operator to create a list](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-57.jpg)
Variables in subroutine > Private variables – Use “my” operator to create a list of private variables > Semiprivate – Private, but visible within any subroutines calls in the same block – Use “local” to create a list of semi-private variables $value = “orignial” tellme( ); spoof( ); tellme( ); # original temporary original tellme( ); spoof( ); tellme( ); # original sub spoof{ local ($value) = "temporary"; tellme(); } sub spoof{ my ($value) = "temporary"; tellme(); } sub tellme { print "$value"; } 57
![File File](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-58.jpg)
File
![Open and close 1 Automatically opened file handlers STDIN STDOUT STDERR Open and close (1) > Automatically opened file handlers – STDIN, STDOUT, STDERR >](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-59.jpg)
Open and close (1) > Automatically opened file handlers – STDIN, STDOUT, STDERR > Open – open(FILEHD, "filename") # open for read – open(FILEHD, ">filename") # open for write – open(FILEHD, ">>filename") # open for append > Open with status checked – open(FILEHD, "filename") || die "error-message"; > Close – close(FILEHD) 59
![Open and close 2 Open with redirection Ex usrbinperl open FD ypcat Open and close (2) > Open with redirection – Ex: #!/usr/bin/perl open (FD, "ypcat](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-60.jpg)
Open and close (2) > Open with redirection – Ex: #!/usr/bin/perl open (FD, "ypcat passwd | grep /tytsai |"); while(<FD>){ chomp; print "$_n"; } open (FD 2, "|/usr/bin/mail -s "Mail from perl" tytsai@csie. nctu. edu. tw"); print FD 2 "this is testn"; 60
![File test name index html if e name print file name existsn File test $name = "index. html"; if (-e $name) { print "file: $name existsn";](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-61.jpg)
File test $name = "index. html"; if (-e $name) { print "file: $name existsn"; } 61
![Directory Use chdir function Change current directory Return successful or not Directory > Use “chdir” function – Change current directory – Return successful or not](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-62.jpg)
Directory > Use “chdir” function – Change current directory – Return successful or not – Ex: chdir(“/etc”) || die “cannot cd to /etc ($i)”; > Globbing – Expansion of path that contains * into list – Globbing can be done through • • <path> glob function @a = </etc/host*>; @b = glob("/etc/host*"); print "a = @an"; print "b = @bn"; # /etc/host. conf /etc/hosts. allow /etc/hosts. equiv /etc/hosts. lpd 0 62
![File and Directory Manipulation Removing file unlinkfilenamelist Ex unlinkdata 1 txt File and Directory Manipulation > Removing file – unlink(filename-list); – Ex: unlink(“data 1. txt”,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-63.jpg)
File and Directory Manipulation > Removing file – unlink(filename-list); – Ex: unlink(“data 1. txt”, “hello. pl”); unlink <*. o>; > Renaming a file – rename(file, new-name); > Create link – link (original, link-file) – symlink(original, link-file) # ln origninal link-file # ln –s original link-file – mkdir(directory-name, mode) – rmdir(directory-name) # mkdir(“test”, 0777) – chmod(mode, file) # chmod(0666, “hello. pl”) – chown(UID, GID, file) # chown(1234, 35, “hello. pl”) > Making and removing directory > Modify permission > Change ownership 63
![Format Format](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-64.jpg)
Format
![Format Format Report writing template Define Constant part headers Format > Format – Report writing template – Define • • Constant part (headers,](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-65.jpg)
Format > Format – Report writing template – Define • • Constant part (headers, labels, fixed text) Variable part (reporting data) – Using format • • Defining a format Invoking the format 65
![Define a format Use format keyword syntax format name fieldline Define a format > Use “format” keyword – syntax – format name = fieldline](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-66.jpg)
Define a format > Use “format” keyword – syntax – format name = fieldline value 1, value 2, value 3, … fieldline value 4, value 5, …. fieldline • format ADDRESS = =============== | @<<<<<<<<<<<<<| $name | @<<<<<<<<<<<<<| $address | @<<<<<<, @<<<<<<| $city, $state, $zip =============== can be either fixed text or “fieldholders” for variable > White space is important in fieldline > White space is ignored in value line • If there is any fieldholders in fieldline, there must be a series of scalar variable in the following line 66
![Invoking a format Through write function write function will write stuff into Invoking a format > Through “write” function – write function will write stuff into](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-67.jpg)
Invoking a format > Through “write” function – write function will write stuff into current file handler – using “current” format Default current format is the same name with file handler #!/usr/bin/perl open (FD 1, "data 2. txt") || die "can't open file: $!"; while (defined($line = <FD 1>)){ ($name, $age, $school) = split(" ", $line); write; } close (FD 1) || die "can't close file: $!"; format STDOUT = @<<<<<<< $name, $age, $school. 67
![Fieldholders It means 5 character left justified Text fields Fieldholders > @<<< – It means “ 5 character, left justified” > Text fields](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-68.jpg)
Fieldholders > @<<< – It means “ 5 character, left justified” > Text fields – Use @ to mean text fields – Use <, >, | to mean left, right and center -justified > Numeric Fields – Use @ to mean numeric fields, but use “#” to represent digit – Ex: • Assets: @#####. ## > Multiline Fields – Use @* to place multiple lines in single fieldholders 68
![The Topofpage format Let report to fit pagesize printing device Perl will The Top-of-page format > Let report to fit page-size printing device – Perl will](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-69.jpg)
The Top-of-page format > Let report to fit page-size printing device – Perl will call top-of-page format if • • In the very beginning of write When the output cannot fit in current page – Default page length • • 60 lines Set $= to 30 can change page length to 30 lines – Default top-of-page format name • filehandlername_TOP – Variables used in top-of-page format • $% > Will be replaced with current page number 69
![Changing Defaults Change default file handler Use select function print without Changing Defaults > Change default file handler – Use select function – print without](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-70.jpg)
Changing Defaults > Change default file handler – Use select function – print without file handler will write stuff to default handler – Ex: print “hello worldn”; # print STDOUT “hello worldn”; $old. FD = select (LOGFILE); print “Error happenedn”; # print LOGFILE “Error happenedn”; select ($old. FD); # restore to saved file handler > Change default format name – Set $~ variable – Ex: $~ = ADDRESS write; # it will use the ADDRESS format # other than STDOUT 70
![Process Management Process Management](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-71.jpg)
Process Management
![Using system function system function system will fork a binsh shell Using system() function > system function – system( ) will fork a /bin/sh shell](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-72.jpg)
Using system() function > system function – system( ) will fork a /bin/sh shell to execute the command – – specified in arguments STDIN, STDOUT and STDERR are inherited from the Perl process Ex: system(“date”); system(“(date; who) > $gohere”); 72
![Using Backquote Execute the command replace itself with execution result Using Backquote > `` – Execute the command replace itself with execution – result](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-73.jpg)
Using Backquote > `` – Execute the command replace itself with execution – result Ex: foreach $_ (`who`){ ($who, $where, $when) = /(S+)s+(. *)/; print “$who on $where at $whenn”; } tytsai@tybsd: ~/Perl> who tytsai ttyv 0 Mar 28 14: 05 tytsai ttyp 0 Mar 30 08: 27 (ccamd) tytsai ttyp 1 Mar 28 14: 12 (ccamd: S. 0) tytsai ttyp 2 Mar 28 14: 12 (ccamd: S. 1) tytsai ttyp 3 Mar 28 14: 12 (ccamd: S. 2) tytsai@tybsd: ~/Perl> perl process. pl tytsai on ttyv 0 at Mar 28 14: 05 tytsai on ttyp 0 at Mar 30 08: 27 (ccamd) tytsai on ttyp 1 at Mar 28 14: 12 (ccamd: S. 0) tytsai on ttyp 2 at Mar 28 14: 12 (ccamd: S. 1) tytsai on ttyp 3 at Mar 28 14: 12 (ccamd: S. 2) 73
![Using Process as Filehandler We can either Open and capture the output Using Process as Filehandler > We can either – Open and capture the output](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-74.jpg)
Using Process as Filehandler > We can either – Open and capture the output from process or – Open and provide input to process > Ex: open(WHOFD, “who |”); open(MAILFD, “| mail tytsai@csie. nctu. edu. tw”) open(MULTI, “who | grep : S. *|”); while(<MULTI>){ print $_; } 74
![Using fork function Just as fork2 do Create a clone of the Using fork() function > Just as fork(2) do – Create a clone of the](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-75.jpg)
Using fork() function > Just as fork(2) do – Create a clone of the current perl process – Use return PID to distinguish parent and child • Zero for child and nonzero for parent if (!defined($child_pid = fork())){ # fork failed die "cannot fork: $!"; }elsif ($child_pid){ exec("date"); die “can’t not exec data: $!"; }else{ waitpid($child_pid, 0); print("child has finishedn"); } 75
![Sending and Receiving Signals 1 Catch the signal in your program Using Sending and Receiving Signals (1) > Catch the signal in your program – Using](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-76.jpg)
Sending and Receiving Signals (1) > Catch the signal in your program – Using %SIG predefined hash – Using signal name in ‘man signal’ without prefix “SIG” as the key • Ex: > $SIG{‘INT’}, $SIG{‘TERM’} – Set the hash value to your subroutine to catch the signal • • Use “DEFAULT” to restore default action Use “IGNORE” to ignore this signal (no action) > Sending the signal – Use kill( ) function • kill(signal, pid-list) > kill(2, 234, 235); or kill(‘INT’, 234, 235); 76
![Sending and Receiving Signals 2 Ex usrbinperl SIGTERM myTERMcatcher print before sending Sending and Receiving Signals (2) > Ex: #!/usr/bin/perl $SIG{'TERM'} = 'my_TERM_catcher'; print "before sending](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-77.jpg)
Sending and Receiving Signals (2) > Ex: #!/usr/bin/perl $SIG{'TERM'} = 'my_TERM_catcher'; print "before sending signal. . n"; kill(15, $PID); print "after sending signal. . n"; sub my_TERM_catcher{ print "I catch you!! Do cleanup worksn"; } 77
![System Information Manipulation System Information Manipulation](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-78.jpg)
System Information Manipulation
![User information 1 Using getpwuid or getpwnam Pass uid to getpwuid User information (1) > Using getpwuid() or getpwnam() – Pass uid to getpwuid( )](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-79.jpg)
User information (1) > Using getpwuid() or getpwnam() – Pass uid to getpwuid( ) and login-name to getpwnam( ) – Both return the list: ($name, $passwd, $uid, $gid, $pw_change, $pw_class, $gcos, $dir, $shell, $pw_expire) @a = getpwnam("tytsai"); @b = getpwuid(1001); print "@an"; print "@bn"; # tytsai * 1001 0 Tsung-Yi Tsai /home/tytsai /bin/tcsh 0 79
![User information 2 Sequential access to passwd Use setpwent getpwent User information (2) > Sequential access to passwd – Use setpwent( ), getpwent( )](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-80.jpg)
User information (2) > Sequential access to passwd – Use setpwent( ), getpwent( ) and endpwent( ) > Sequential access to group – Use setgrent( ), getgrent( ) and endgrent( ) setpwent(); while(@list = getpwent()){ print ("@listn"); } endpwent(); setgrent(); while(@list = getgrent()){ print ("@listn"); } endgrent(); 80
![String Manipulation String Manipulation](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-81.jpg)
String Manipulation
![Related functions Find a substring indexoriginalstr substr where 1 where 2 where Related functions > Find a substring – index(original-str, sub-str) $where 1 $where 2 $where](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-82.jpg)
Related functions > Find a substring – index(original-str, sub-str) $where 1 $where 2 $where 3 $where 4 = = index("a very long string", "long"); index("a very long string", "lame"); index(“hello world”, “o”, 5); index(“hello world”, “o”, 8); # # 7 -1 > Sub-string – substring(string, start, length); $str = substr(“a very long string”, 3, 2) $str = substr(“a very long string”, -3, 3) # “er” # “ing” > Formatting data – sprintf(format, argument-list); $result = sprintf(“%05 d”, $y); 82
![Sort Sort Without any modification sorting is based on ASCII Sort > Sort – Without any modification, sorting – – is based on ASCII](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-83.jpg)
Sort > Sort – Without any modification, sorting – – is based on ASCII code You can sort by specifying your “comparison method” Ex: @somelist = (1, 2, 4, 8, 16, 32, 64, 128, 256); @a = sort @somelist; @b = sort by_number @somelist; print "a = @an"; print "b = @bn"; sub by_number{ if($a < $b){ return -1; }elsif ($a == $b){ return 0; }elsif ($a > b){ return 1; } } 83
![Builtin functions Built-in functions](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-84.jpg)
Built-in functions
![Builtin functions For Scalars chomp chop index length sprintf substr Built-in functions > For Scalars – chomp, chop, index, length, sprintf, substr, … >](https://slidetodoc.com/presentation_image_h/36949912eec3a3bd4be91d294ff9f2a0/image-85.jpg)
Built-in functions > For Scalars – chomp, chop, index, length, sprintf, substr, … > Numeric – abs, exp, log, hex, int, oct, rand, sin, cos, sqrt, … > For @ or % – push/pop, shift, sort, keys, values, delete > I/O – open, close, read, write, print/printf, … > Time-related – gmtime, localtime, times > Network – bind, socket, accept, connect, listen, getsockopt/setsockopt, … > User and group info – Getpwent/setpwent, getpwuid, getpwnam, getgrent/setgrent, … 85
Man perlrun
External conflict man vs society
Perl cgi tutorial
Cgi linkage in perl
Perl programming for biologists
What is apa format
Resume reference page
Earth science part d
Earth science reference table page 16
Harvard citation page number
Reference node and non reference node
Reference node and non reference node
Perbedaan linear programming dan integer programming
Greedy algorithm vs dynamic programming
System programming
Integer programming vs linear programming
Programing adalah
Perl logger
Regex
Perl tricks
Perl tk tutorial
Perl soap::lite
Obfuscated perl
Perl not equal operator
Perl web framework
Perl shell scripting
Beginning perl for bioinformatics
Perl n
Perl data types
Perl hash table
Four perfect pebbles chapter 2
Perl bioinformatics
Perl yaml example
Open perl ide
Perl random number generator
Perl log analysis
Slidetodoc.com
Intro to perl
Perl getting started
Language
Perl conditionals
Perl paradigma
Perl round
Cgi linkage in perl
Swig perl
Perl camel
Php custom exception
Elseif perl
Perl text manipulation
Dan perl
Esb9999
Types of values
Perl tutorial javatpoint
Perl println
Perl bioinformatics
Schengenlyzeum perl
Cgi perl
"commercial" perl or tcl or python
Piper perl
Tall man lettering canada
By one man sin
You live and learn
Det man vinner i kraft förlorar man i väg
Hungry man is a angry man
Old man and new man
Mensen trening
Man vs man conflict in the cask of amontillado
Literary conflict definition
Internal struggle definition
Carnal man
Gott liebt jeden menschen
Rich man poor man bible
The white man's burden cartoon
Cover page essay
What is editorial article
Bib.com citation
Citation purpose
In text citation with multiple authors
Where the red fern grows vocabulary words and page numbers
Insurance declaration page example
Qrz page
How to properly make a works cited page
Change page size word
Website is a collection of *
Frost wedging drawing
Multi level page table