免费注册 查看新帖 |

Chinaunix

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

关于perl文件句柄赋值给hash变量的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-21 23:58 |只看该作者 |倒序浏览
大家好,
我在打开一个文件的时候,将文件句柄赋值给hash变量,如下:
1 #!/usr/bin/perl -w
  2 use Data:umper;                                                                                                                                       
  3
  4 my %FH;
  5 open ($FH{nt}, ">>mod.log";
  6
  7 my $fh;
  8 $fh =   $FH{nt};
  9 print $FH{nt} "hello";


用上边的方法写文件的时候提示如下错误:
String found where operator expected at tt.pl line 9, near "} "hello""
        (Missing operator before  "hello"?)
syntax error at tt.pl line 9, near "} "hello""
Execution of tt.pl aborted due to compilation errors.


但是将hash变量在赋值给另外一个变量再写文件的时候就是成功的:
  1 #!/usr/bin/perl -w                                                                                                                                       
  2 use Data:umper;
  3
  4 my %FH;
  5 open ($FH{nt}, ">>mod.log";
  6
  7 my $fh;
  8 $fh =   $FH{nt};
  9 print $fh "hello";


不知这个是啥原理呢?求解释~

论坛徽章:
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 [报告]
发表于 2012-06-22 00:12 |只看该作者
回复 1# dolinux

# you can do this one
print {$FH{nt}} "hello";

论坛徽章:
0
3 [报告]
发表于 2012-06-22 12:18 |只看该作者
回复 2# jason680
多谢喽~~
为啥直接pint就不行呢?句柄赋值给hash变量是有啥特殊的呢?


   

论坛徽章:
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
4 [报告]
发表于 2012-06-22 15:48 |只看该作者
本帖最后由 jason680 于 2012-06-22 15:49 编辑

回复 3# dolinux

$ perldoc -f print
       print FILEHANDLE LIST
       print LIST
       print   Prints a string or a list of strings. ...               
               ...
               Note that if you're storing FILEHANDLEs in an array, or if
               you're using any other expression more complex than a scalar
               variable
to retrieve it, you will have to use a block returning
               the filehandle value instead:


                   print { $files[$i] } "stuff\n";
                   print { $OK ? STDOUT : STDERR } "stuff\n";

               Printing to a closed pipe or socket will generate a SIGPIPE
               signal.  See perlipc for more on signal handling.

论坛徽章:
0
5 [报告]
发表于 2012-06-23 17:03 |只看该作者
回复 4# jason680


    非常感谢,学习了~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP