
Encrypt.pm 的程式原始碼如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #!/usr/bin/perl use strict; use warnings; package Encrypt; use List::Util qw(shuffle); sub new { my $class = shift ; my $self = { alph => shift , code => shift , }; $self ->{alph} = join ( "" , ( "a" .. "z" )); $self ->{code} = join ( "" , shuffle(( "a" .. "z" ))); bless $self , $class ; return $self ; } sub getCode { my $self = shift ; return $self ->{code}; } sub setCode { my $self = shift ; my $code = shift ; $self ->{code} = $code ; } sub toEncode { my $self = shift ; my $str_input = shift ; my $str_result = "" ; my $i = 0; while ( $i < length ( $str_input )) { my $s = substr ( $str_input , $i , 1); if ( $s =~ /[a-z]/) { $str_result .= substr ( $self ->{code}, index ( $self ->{alph}, $s ), 1); } else { $str_result .= $s ; } $i ++; } return $str_result ; } sub toDecode { my $self = shift ; my $str_input = shift ; my $str_result = "" ; my $i = 0; while ( $i < length ( $str_input )) { my $s = substr ( $str_input , $i , 1); if ( $s =~ /[a-z]/) { $str_result .= substr ( $self ->{alph}, index ( $self ->{code}, $s ), 1); } else { $str_result .= $s ; } $i ++; } return $str_result ; } 1; # 《程式語言教學誌》的範例程式 # 檔名:Encrypt.pm # 功能:示範 Perl 程式 # 作者:張凱慶 # 時間:西元 2013 年 1 月 |
您可以繼續參考
範例程式碼
相關目錄
回 Perl 入門指南
回 Perl 教材
回首頁
參考資料
http://www.perl.org/
http://learn.perl.org/
http://perldoc.perl.org/index.html
http://www.tutorialspoint.com/perl/index.htm
http://perl-begin.org/
http://www.bin-co.com/perl/perl_tk_tutorial/
沒有留言:
張貼留言