$1, month => $2, day => $3, weekday => $4,  );  for (keys %hash) {  print "$_ => $hash{$_}\n";  }"> $1, month => $2, day => $3, weekday => $4,  );  for (keys %hash) {  print "$_ => $hash{$_}\n";  }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible.

Similar presentations


Presentation on theme: "Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible."— Presentation transcript:

1 Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible

2 Computer Center, CS, NCTU 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”

3 Computer Center, CS, NCTU 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";  }

4 Computer Center, CS, NCTU 4 One-Line Perl Execution

5 Computer Center, CS, NCTU 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 = ;

6 Computer Center, CS, NCTU 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 ( ) { print } Read from file handler FH

7 Computer Center, CS, NCTU 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

8 Computer Center, CS, NCTU 8 On the first use we should set up configuration

9 Computer Center, CS, NCTU 9

10 Computer Center, CS, NCTU 10 Keep …

11 Computer Center, CS, NCTU 11 Change PREFIX! PREFIX is where to install your modules. If you don’t understand, For non-root users, my setting is ~/perl/module/ Still keep until…

12 Computer Center, CS, NCTU 12 Select the nearest CPAN site 2

13 Computer Center, CS, NCTU 13

14 Computer Center, CS, NCTU 14 Install desired modules: After installation, type “quit” to leave cpan

15 Computer Center, CS, NCTU 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

16 Computer Center, CS, NCTU 16 Demo: Pdir, BBS like directory handler

17 Computer Center, CS, NCTU 17 Demo: Process Killer

18 Computer Center, CS, NCTU 18 Demo: wxPerl + OpenGL

19 Computer Center, CS, NCTU 19 Demo: Kephra using wxPerl on Windows


Download ppt "Perl Programming – Appendix There ’ s more than one way to do it Keep easy things easy and the hard possible."

Similar presentations


Ads by Google