b******e 发帖数: 432 | 1 是道测试题了,周五早上之前给出答案才有效。
希望有PERL经验的同学指点一下,多谢啦!
There are errors in the following code, both in logic
and in syntax. Find the errors and provide the correct
fixes.
# this function prints something to stdout, unless
# the option parameter is set to STDERR, and then it
# prints out to stderr
sub writeOut
{
my $message = shift;
my $option = shift; #optional
if( $option eq 'STDERR' )
{
print STDERR $message . "\n";
}
else
{
print $message . "\n";
}
}
# this function gets the system UUID, with or without
# dashes depending on the option
sub getUUID()
{
my $option = shift; #optional
my $uuid = "8f5de6fd-5f97-4b27-be35-861c3bbbb1f1";
if( $option eq 'nodash' )
{
$uuid =~ s/-//g;
}
return $uuid;
} | e****d 发帖数: 895 | 2 Check whether $option is defined or not before doing
the eq comparison.
【在 b******e 的大作中提到】 : 是道测试题了,周五早上之前给出答案才有效。 : 希望有PERL经验的同学指点一下,多谢啦! : There are errors in the following code, both in logic : and in syntax. Find the errors and provide the correct : fixes. : # this function prints something to stdout, unless : # the option parameter is set to STDERR, and then it : # prints out to stderr : sub writeOut : {
| b******e 发帖数: 432 | 3 I guess this doesn't matter.
Someone points out that should change eq to ne to get semantically correct. | b******e 发帖数: 432 | 4 多谢啦!!!!
这2道题我都搞明白了!
第一个是语义理解上的问题。
第二题是因为
# Functions with a prototype of () are potential candidates for inlining.
# So, it's supposed no arguments. | b******e 发帖数: 432 | 5 # For this function, I think the original one is semantically wrong.
# Based on the description, I think it always needs to print to STDERR.
# However, when the option is not STDERR, it needs to print to STDOUT as
well.
#
# Besides, I may improve it by add some the restrict to input or add more
match pattern to the option, Ex:
# 1. I may trim the $option before compare.
# 1. The input option might be 'stderr', I may modify it to .
# 2. There might no input option, or no input message.
sub my_writeOut
{
my $message = shift;
my $option = shift; #optional
exit 0 if (!$message); # exit if $message is null
if( &trim($option) ne 'STDERR' )
{
print $message . "\n";
}
print STDERR $message . "\n";
}
# Functions with a prototype of () are potential candidates for inlining.
# So, the original one is supposed no arguments.
# Also, I may change the function to get $uuid from input parameters.
# Further, I may use some functions to get the system UUID.
sub my_getUUID
{
my $uuid = shift;
my $option = shift; #optional
# my $uuid = "8f5de6fd-5f97-4b27-be35-861c3bbbb1f1";
exit 0 if (!$uuid); # exit if $uuid is null
if( &trim($option) eq 'nodash' )
{
$uuid =~ s/-//g;
}
return $uuid;
} |
|