Redian新闻
>
50伪币:请教perl代码差错的问题!多谢啦!
avatar
50伪币:请教perl代码差错的问题!多谢啦!# Unix - 噫吁兮,危乎高哉
b*e
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;
}
avatar
b*e
2
# 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;
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。