close

﹝學習記錄﹞- Perl - Sub

下面是經過其他人幫忙修改過的程式內容

入門階段 呵~! 相當冗長 >…<

#!/usr/bin/perl -w

#======================================================================

#Modification Record

#======================================================================

#20110525  Modify subrountine function.

#======================================================================

#Global Scope

#======================================================================

use strict;

#======================================================================

#Subroutine Program

#======================================================================

#20110525-------------------------------------------------------------------

sub ck_ans{

  my $ans=shift;

  my $answer=shift;

  my $price=shift;

 

  print '='x50,"\n";

  if ($ans eq 'Y' or $ans eq 'Yes' or $ans eq 'y' or $ans eq 'yes'){  #還沒學正規算式前的用法 :P 

    print "That'll be \$", $price,",please.\n";

  }elsif ($ans eq 'N' or $ans eq 'No' or $ans eq 'n' or $ans eq 'no'){

    print "Okay- but you're missing a great movie. Have a nice day!\n";

  }else{

    print "I'am sorry, I didn't understand that.\n";

  }

}

#---------------------------------------------------------------------------

#======================================================================

#Main Program

#======================================================================

print '-'x50,"\n";

print "Which movie would you like to see?\n (1) Movie A  (2) Movie B  (3) Movie C  (4) Movie D  (5) Movie E\n";

print "Please choose one (1/2/3/4/5): ";

my $answer = <>;

chomp($answer);

my $price;

if ($answer == 1){

  $price='8.50';

  print "Are you sure you want to see the 'Movie A'? (Yes/No): ";

  my $ans=<>;

  chomp $ans;

  &ck_ans($ans,$answer,$price);

}elsif ($answer == 2){

  $price='9.20';

  print "Are you sure you want to see the 'Movie B'? (Yes/No): ";

  my $ans=<>;

  chomp $ans;

  &ck_ans($ans,$answer,$price);

}elsif ($answer == 3){

  $price='6.25';

  print "Are you sure you want to see the 'Movie C'? (Yes/No): ";

  my $ans=<>;

  chomp $ans;

  &ck_ans($ans,$answer,$price);

}elsif ($answer == 4){

  $price='7.55';

  print "Are you sure you want to see the 'Movie D'? (Yes/No): ";

  my $ans=<>;

  chomp $ans;

  &ck_ans($ans,$answer,$price);

}elsif ($answer == 5){

  $price='8.00';

  print "Are you sure you want to see the 'Movie E'? (Yes/No): ";

  my $ans=<>;

  chomp $ans;

  &ck_ans($ans,$answer,$price);

}else{

  print "Please enter number 1~5. \n";

}

print '-'x50,"\n";

 

執行程式範例:

 

 

 

arrow
arrow
    文章標籤
    Perl sub
    全站熱搜

    yunc 發表在 痞客邦 留言(0) 人氣()