boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 50伪币:请教perl代码差错的问题!多谢啦!
相关主题
一个 perl 的 print 的初级问题
A question related to pipe
问一个vc++ 2008的问题
how can I get external program's result in C
菜鸟请教个hadoop streaming job 的问题 (转载)
请高人解释一下为啥这个输出总是"HELLO-ERR"
Python有什么好的方法建two-way pipe?
node.js multer: Recursive process.nextTick detected
node.js child process: 怎样保证1个命令执行完了再执行下一个?
请教一个python下面popen的问题
相关话题的讨论汇总
话题: uuid话题: stderr话题: option话题: message话题: shift
进入Programming版参与讨论
1 (共1页)
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;
}
1 (共1页)
进入Programming版参与讨论
相关主题
请教一个python下面popen的问题
PyCharm里的Python启动的Process在等待按键,如何继续
简单的perl问题
想实现一个简单的script language,用perl好做么?
弱问perl写网页buffer问题
perl cgi中调用C可执行程序为什么在html中不显示? (转载)
又一个GDB的问题:关于显示数据
这个 perl 输出的数字为什么自动加了换行?谢谢!
perl question
请 教 : 关 于 writing to a file 用 Perl for CGI
相关话题的讨论汇总
话题: uuid话题: stderr话题: option话题: message话题: shift