Tuesday, October 14, 2008

perl ternary operator

Usually only one statement can be used. However, if one would like to use more than one statement, one will have to use the logical operator "and" combining with the open parathesis " ( )"instead of the semicolon ";"


Here is my example to create a new perl file:


#!/usr/bin/perl -w
#use strict


### create a perl script template.


$created_filename=$ARGV[0];


if(-e "$created_filename\.pl"){
print "The created file has already existed. To override, input Y(y) or N(n), then press Enter\n";

rep2: $_=; chomp;



/^n(o)?/i ? print "The same named file has already existed and file creation is given up.\n"
:/^ye+s?/i ? print "The file will be created\n"
: (print "input wrong, please input Ye(s) or N(o)!!" and print " here is a test\n" and goto rep2);

# if (/^n(o)?/i){
# print "The same named file has already existed and file creation is given up.\n";
# }elsif(/^ye+s?/){
# print "The file will be created\n";
# }else{
#
# print "input wrong, please input Yes or No!!\n";
# goto rep2;
# #redo;

}
local *TR; open TR, ">$created_filename\.pl";
print TR "\#\!\/usr\/bin\/perl \-w\n";
print TR "\#use strict\n";



$mode=0777;
chmod $mode, "$created_filename\.pl";

No comments: