perl programming – appendix there ’ s more than one way to do it keep easy things easy and the...
Post on 19-Dec-2015
217 views
TRANSCRIPT
![Page 1: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/1.jpg)
Perl Programming – AppendixPerl Programming – Appendix
There’s more than one way to do it
Keep easy things easy and the hard possible
![Page 2: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/2.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
2
Problem 1: Regex Example
$_ = “this is a test”;/(\w+)\W+(\w+)/; # match first two words,
# $1 = “this”, $2 = “is”print “$1, $2\n”;
$_ = “this is a sample string”;/sa.*le/; # $` = “this is a ”
# $& = “sample”# $’ = “ string”
![Page 3: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/3.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
3
Regex Example: Date Extraction
#!/usr/bin/perl $date = `date`; print "$date"; # 2008 年 2 月 29 日 周五 11 時 27 分 16 秒
CST $date =~ /^(\d+)\D+(\d+)\D+(\d+)\S+\s+ 周 (\S+)/; %hash = (
year => $1, month => $2, day => $3, weekday => $4, ); for (keys %hash) { print "$_ => $hash{$_}\n"; }
![Page 4: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/4.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
4
One-Line Perl Execution
![Page 5: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/5.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
5
Problem 2: File Handler
An interface to file access
File handler in C
File handler in Perl
File HandlerUser
File Name
Access Mode
Flags
Read, write
FILE *fp = fopen("count.dat","r");fgets(numb, 8, fp);
open FH, “count.dat” or die “open file failure: $!”;$numb = <FH>;
![Page 6: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/6.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
6
Problem 3: <>
while (<>) { print }
Using diamond operator <>• Get data from files specified on the command line
$ARGV records the current filename@ARGV shifts left to remove the current filename
• Otherwise read from STDIN
while (<FH>) { print }• Read from file handler FH
![Page 7: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/7.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
7
CPAN
If you see similar errors on execution of perl…
• Your @INC is incorrect, or
• You need to install the required modules
We can install modules from command line tool• cpan
Useful tool to install modules in CYGWIN
![Page 8: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/8.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
8
On the first use we should set up configuration
<Enter>
![Page 9: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/9.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
9
<Enter>
<Enter>
![Page 10: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/10.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
10
Keep <ENTER>…
![Page 11: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/11.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
11
Change PREFIX!
PREFIX is where to install your modules.If you don’t understand, <ENTER>For non-root users, my setting is ~/perl/module/
Still keep <ENTER> until…
![Page 12: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/12.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
12
Select the nearest CPAN site
2
![Page 13: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/13.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
13
![Page 14: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/14.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
14
Install desired modules:
After installation, type “quit” to leave cpan
![Page 15: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/15.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
15
Some Useful Built-in
system "tail -20 $ENV{HOME}/mail/procmail.log"; exec “rm -f file”;
sleep 3; select undef, undef, undef, 3;
$char = getc ; perl -e 'system("stty raw");getc‘
exit 1; die “error msg: $!”;
# sleep 3 seconds
# need to wait ENTER
# immediate return on keystroke
![Page 16: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/16.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
16
Demo: Pdir, BBS like directory handler
![Page 17: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/17.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
17
Demo: Process Killer
![Page 18: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/18.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
18
Demo: wxPerl + OpenGL
![Page 19: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/19.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
19
Demo: Kephra using wxPerl on Windows
![Page 20: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/20.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
20
Perl Module
We have two files in the same directory• main.pl The main script, will be run as ./main.pl
• Animal/Mouse.pm Definition of perl module
In main.pl,
#!/usr/bin/perl –w
use lib ‘.’; # search path for your modules, see @INC
use Animal::Mouse;
# means module in Animal/Mouse.pm
$mouse = new Animal::Mouse ("Mickey"); In Mouse.pm,
package Animal::Mouse; # must be the same as file name
1;
![Page 21: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/21.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
21
POSIX
You can use C functions in Perl. perldoc POSIX for list of usable functions.
use POSIX qw(acos setgid uname);• acos(0.5)
• setgid(4)
• if( uname() =~ /linux/i ){…}
![Page 22: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/22.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
22
IO::Handle
OO interface for file handler use IO::Handle; open SCR, ">/dev/tty";
SCR -> autoflush(1); Flush on every write
STDIN -> blocking(0); Non-blocking read
$line = <STDIN>;
Normal write: Buffer data until “\n” written Auto flush write: Write without waiting Normal read: block and wait until “\n” received Non-Blocking read: return immediately without waiting
![Page 23: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/23.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
23
IO::Select -1
OO interface to the power tool select
Assume we are using non-blocking reads. Read by Polling
• Write a loop to query data on your own
Read by select• Process incoming data when available
Data GeneratorProgram Query
Data GeneratorProgram Notify
![Page 24: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/24.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
24
IO::Select -2
$rd_h = new IO::Select($telnet, \*STDIN); while (@ready = $rd_h -> can_read){
for $fh (@ready){
if ($fh == $telnet) { # process telnet socket reads }
else { # process STDIN reads }
}
}
![Page 25: Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible](https://reader035.vdocuments.net/reader035/viewer/2022081519/56649d385503460f94a10f96/html5/thumbnails/25.jpg)
Com
pu
ter C
en
ter, C
S, N
CTU
25
IO::Select -3
use IO::Select; use IO::Socket;
$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080); $sel = new IO::Select( $lsn );
while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $sel->add($new); } else { # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } } }