免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3620 | 回复: 4
打印 上一主题 下一主题

请教一段Perl代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-01-21 13:48 |只看该作者 |倒序浏览
我从一个正常运行的脚本里,看到有如下一段Perl代码, 实在不太理解红字的部分, 还请帮忙, 谢谢。

my ($in,$str,$hashref);
my %node_cfg;

$in = new FileHandle "</tmp/file";

$str = <$in>;
close $in;
$hashref;                    #实在搞不懂这里, 这是什么意思, 这样能执行吗     
eval $str;                    # 我认为$str是一段不能个执行的代码, 那放在eval里有什么用呢, 请高手点解? 谢了。

%node_cfg = %$hashref;


##############

/tmp/file 内容如下:



$hashref = {
             'server1.test.com' => {
                                            'STATUS' => 'Running',
                                            'CPU' => 'Intel',
                                            'SYSTEM_ID' => '001',
                                            'OS' => 'Windows 2008 Enterprise R2'
                                          },

             'server2.test.com' => {
                                            'STATUS' => 'Running',
                                            'CPU' => 'Intel',
                                            'SYSTEM_ID' => '002',
                                            'OS' => 'Redhat Enterprise Linux 6'
                                          }
           }



论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2014-01-21 15:10 |只看该作者
本帖最后由 jason680 于 2014-01-21 15:11 编辑

回复 1# unixu


$ perldoc -f eval

    eval EXPR
    eval BLOCK
    eval    In the first form, the return value of EXPR is parsed and executed
            as if it were a little Perl program. The value of the expression
            (which is itself determined within scalar context) is first
            parsed, and if there were no errors, executed in the lexical
            context of the current Perl program, so that any variable settings
            or subroutine and format definitions remain afterwards. Note that
            the value is parsed every time the "eval" executes. If EXPR is
            omitted, evaluates $_. This form is typically used to delay
            parsing and subsequent execution of the text of EXPR until run
            time.

            ...

   

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
3 [报告]
发表于 2014-01-21 15:27 |只看该作者

{:2_172:}  大神 ! 帮我看看代码, 它是否有问题 ?
http://bbs.chinaunix.net/forum.p ... ;extra=#pid24106017
19 楼

回复 2# jason680


   

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
4 [报告]
发表于 2014-01-22 01:43 |只看该作者
本帖最后由 rubyish 于 2014-01-22 04:07 编辑
unixu 发表于 2014-01-21 09:48
我从一个正常运行的脚本里,看到有如下一段Perl代码, 实在不太理解红字的部分, 还请帮忙, 谢谢。
  1. $hashref;                    #实在搞不懂这里, 这是什么意思, 这样能执行吗     
复制代码
my  ...

这条是没用的。
delete it is ok.
try
  1. my ($S,$H);
  2. my %C;

  3. $S = <>;        # input: $H = { a => 1, b => 2 }
  4. #$H;            #实在搞不懂这里, 这是什么意思, 这样能执行吗     
  5. eval $S;
  6. %C = %$H;       # %C now ( a => 1, b => 2 )
复制代码

论坛徽章:
0
5 [报告]
发表于 2014-01-22 11:34 |只看该作者
eval 是為了避免上面的 file handle 產生錯誤導致 crash
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP