본문 바로가기

창고/Backup_2013_0121

Day 4 (last)

반응형
1 #!/usr/bin/perl 2 use strict; 3 use warnings; 4 5 $_ = "fish"; 6 /((\w)(\w))/; 7 8 print "$1 $2 $3"; #fi f i 9 10 $_ = "1234567890"; 11 12 /(\d)+/; 13 14 print "\n $1"; #0 15 16 /(\d+)/; 17 18 print "\n $1"; #1234567890 19 20 print "\n"; 21 22 $_ = "Our server is training.perltraining.com.au."; 23 my ($full, $host, $domain) = /(([\w-]+)\.([\w.-]+))/; 24 print "$1\n"; # prints "training.perltraining.com.au." 25 print "$full\n"; # prints "training.perltraining.com.au." 26 print "$2 : $3\n"; # prints "training : perltraining.com.au." 27 print "$host : $domain\n" # prints "training : perltraining.com.au." 28 29 # Extended Regular Expression 30 # Parse a line from ’ls -l’ 31 32 m{ 33 ^ # Start of line. 34 ([\w-]+)\s+ # $1 - File permissions. 35 (\d+)\s+ # $2 - Hard links. 36 (\w+)\s+ # $3 - User 37 (\w+)\s+ # $4 - Group 38 (\d+)\s+ # $5 - File size 39 (\w+\s+\d+\s+[\d:]+)\s+ # $6 - Date and time. 40 (.*) # $7 - Filename. 41 $ # End of line. 42 }x; 43 44 45 __END__

파일을 변수에 담고 정규식만 표현하면 매칭하는 값을 찾을 수 있다.
끝에 있는 확장 정규식으로 좀더 보기 쉽게 정규식 표현이 가능하다

1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 while(<>) 7 { 8 print "$_"; 9 } 10 11 __END__

파일 읽는 예제전에 볼 에코 프로그램 예제
특히나 stadard in 은 <> 로 단축 가능 



1
#!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 my $filehandler; 7 my $fh_write; 8 my $fh_append; 9 10 open( $filehandler, '<', 'fileopen.pl') 11 or die "Fail to open File: $!"; 12 13 open( $fh_write,'>', 'filewrite.dat') 14 or die "Fail to write File: $!"; 15 16 open( $fh_append,'>>', 'fileappend.dat') 17 or die "Fail to append File: $!"; 18 19 while( <$filehandler> ) 20 { 21 print "$_"; 22 print $fh_write "write $_"; 23 print $fh_append "append $_"; 24 } 25 26 27 close( $filehandler ); 28 close( $fh_write ); 29 close( $fh_append ); 30 31 __END__


매우 쉽게 파일을 읽고 쓸수 있는 펄~! 당신도 펄의 세계로=ㅁ=

이걸로 펄 기초 익히기 예제가 끝입니다.

이제 웹 CGI 와 PERL 을 익혀서 지식을 확장해 봐야겠습니다~

 
반응형