免费注册 查看新帖 |

Chinaunix

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

local具体用途 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-12 15:23 |只看该作者 |倒序浏览
本帖最后由 pony2001mx 于 2014-02-12 15:45 编辑

在DNA序列文件中每个序列的ID以>开头,见如下文件:
>seq1
ATGCTAGCTAGCTACGATCGATGCATCGATCGATCGATCG
>seq2
GCGCCGCGGCTATTCGCGTTGACTGCTGACTGCATGACTG

有些PERL脚本有
  1. local $/ = '>'
复制代码
的语句,即以>而不以\n做分割。我的问题是:local 的具体作用是什么? 如果我想在一个while循环以>做分割,转到下一个while循环(或foreach循环)又以\n做分割,如何实现?

非常谢谢!

论坛徽章:
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-02-12 15:39 |只看该作者
回复 1# pony2001mx

please refer the perldata and perlvar

$ perldoc perlvar
    perlvar - Perl predefined variables
    ...

    A few of these variables are considered "read-only". This means that if
    you try to assign to this variable, either directly or indirectly through
    a reference, you'll raise a run-time exception.

    You should be very careful when modifying the default values of most
    special variables described in this document. In most cases you want to
    localize these variables before changing them, since if you don't, the
    change may affect other modules which rely on the default values of the
    special variables that you have changed. This is one of the correct ways
    to read the whole file at once:

            open my $fh, "<", "foo" or die $!;
            local $/; # enable localized slurp mode
            my $content = <$fh>;
            close $fh;

    But the following code is quite bad:

            open my $fh, "<", "foo" or die $!;
            undef $/; # enable slurp mode
            my $content = <$fh>;
            close $fh;

    since some other module, may want to read data from some file in the
    default "line mode", so if the code we have just presented has been
    executed, the global value of $/ is now changed for any other code running
    inside the same Perl interpreter.

    Usually when a variable is localized you want to make sure that this
    change affects the shortest scope possible. So unless you are already
    inside some short "{}" block, you should create one yourself. For example:

            my $content = '';
            open my $fh, "<", "foo" or die $!;
            {
                    local $/;
                    $content = <$fh>;
            }
            close $fh;

    Here is an example of how your own code can go broken:

            for ( 1..3 ){
                   $\ = "\r\n";
                    nasty_break();
                    print "$_";
            }
            sub nasty_break {
            $\ = "\f";
            # do something with $_
            }

    You probably expect this code to print the equivalent of

        "1\r\n2\r\n3\r\n"

    but instead you get:

        "1\f2\f3\f"

    Why? Because "nasty_break()" modifies $\ without localizing it first. The
    value you set in "nasty_break()" is still there when you return. The fix
    is to add "local()" so the value doesn't leak out of "nasty_break()":

            local $\ = "\f";

    It's easy to notice the problem in such a short example, but in more
    complicated code you are looking for trouble if you don't localize changes
    to the special variables.


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP