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;
} |
g**********t 发帖数: 475 | 2 # 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 ne 'STDERR' )
{
print $message . "\n";
}
print STDERR $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;
} |
b******e 发帖数: 432 | 3 能请你解释一下么?
我实在是觉得这俩段代码没啥问题。
多谢了!
【在 g**********t 的大作中提到】 : # 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 ne 'STDERR' ) : { : print $message . "\n";
|
b******e 发帖数: 432 | |
DK 发帖数: 194 | 5 his fix is correct, just change ($option eq 'STDERR')
to
($option ne 'STDERR')
it should be semantically correct....
there is no syntax error here, it runs fine. even when I do "use strict;"
【在 b******e 的大作中提到】 : 能请你解释一下么? : 我实在是觉得这俩段代码没啥问题。 : 多谢了!
|
b******e 发帖数: 432 | 6 不好意思,我真是太愚笨了。我认为对第一段代码来说这2种写法是一样的啊,都没有错误。
第二个加不加括弧的,好像都可以运行。
strict;"
【在 DK 的大作中提到】 : his fix is correct, just change ($option eq 'STDERR') : to : ($option ne 'STDERR') : it should be semantically correct.... : there is no syntax error here, it runs fine. even when I do "use strict;"
|
b******e 发帖数: 432 | 7 I see. It's semantically correct! Thanks!
strict;"
【在 DK 的大作中提到】 : his fix is correct, just change ($option eq 'STDERR') : to : ($option ne 'STDERR') : it should be semantically correct.... : there is no syntax error here, it runs fine. even when I do "use strict;"
|
b******e 发帖数: 432 | 8 多谢啦!!!!
这2道题我都搞明白了!
第一个是语义理解上的问题。
第二题是因为
# Functions with a prototype of () are potential candidates for inlining.
# So, it's supposed no arguments.
我明天早上再发包子哈! |
b******e 发帖数: 432 | 9 # 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;
} |