
perl如何实现C语言中宏的功能?
2个回答
展开全部
可以直接使用,例:
print "FILE: ", __FILE__, " Line: ", __LINE__, "\n";
参见perldoc perldata
The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program.
print "FILE: ", __FILE__, " Line: ", __LINE__, "\n";
参见perldoc perldata
The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program.
追问
首先谢谢你的解答,这种方案我知道,不过比较繁琐,在每次打印日志输出的时候每次都要输入这些内容print __FILE__,__LINE__,$msginfo,"\n"; 我是想实现像C语言宏一样的功能:eg
#define LOG __FILE__,__LINE__
然后在代码需要打印日志的地方就使用 print LOG, $msginfo,"\n";这样的方式,不知道是否可行?
追答
可以考虑自定义一个函数,在里面使用caller关键字。
caller Returns the context of the current subroutine call.
# 0 1 2
($package, $filename, $line) = caller;
#!/usr/bin/perl
use strict;
use warnings;
my $Debug_Flag = 1;
sub logmsg {
return if (!$Debug_Flag);
my $msg = shift;
my ($package, $file, $line) = (caller)[0,1,2];
my $timestring = ""; # 生成当前时间
print "PACKAGE: $package, FILE: $file, LINE: $line\n";
print "TIME; $timestring\n";
print "$msg\n";
}
&logmsg("I'm line 19 in file test.pl");
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询