免费注册 查看新帖 |

Chinaunix

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

求解释!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-09-05 14:22 |只看该作者 |倒序浏览
本帖最后由 上官神速 于 2014-09-05 14:28 编辑

my $DRIVE_RX = '[a-zA-Z]:';
my $UNC_RX = '(?:\\\\\\\\|//)[^\\\\/]+[\\\\/][^\\\\/]+';
my $VOL_RX = "(?:$DRIVE_RX|$UNC_RX)";


sub catfile {
    shift;

    # Legacy / compatibility support
    #
    shift, return _canon_cat( "/", @_ )
        if $_[0] eq "";

    # Compatibility with File::Spec <= 3.26:
    #     catfile('A:', 'foo') should return 'A:\foo'.
    return _canon_cat( ($_[0].'\\'), @_[1..$#_] )
        if $_[0] =~ m{^$DRIVE_RX\z}o;

    return _canon_cat( @_ );
}





sub _canon_cat                                # @path -> path
{
    my ($first, @rest) = @_;

    my $volume = $first =~ s{ \A ([A-Za-z]:) ([\\/]?) }{}x        # drive letter
                   ? ucfirst( $1 ).( $2 ? "\\" : "" )
               : $first =~ s{ \A (?:\\\\|//) ([^\\/]+)
                                 (?: [\\/] ([^\\/]+) )?
                                        [\\/]? }{}xs                        # UNC volume
               ? "\\\\$1".( defined $2 ? "\\$2" : "" )."\\"
               : $first =~ s{ \A [\\/] }{}x                        # root dir
               ? "\\"
               : "";
    my $path   = join "\\", $first, @rest;

    $path =~ tr#\\/#\\\\#s;                # xx/yy --> xx\yy & xx\\yy --> xx\yy

                                            # xx/././yy --> xx/yy
    $path =~ s{(?:
                (?:\A|\\)                # at begin or after a slash
                \.
                (?:\\\.)*                # and more
                (?:\\|\z)                 # at end or followed by slash
               )+                        # performance boost -- I do not know why
             }{\\}gx;

    # XXX I do not know whether more dots are supported by the OS supporting
    #     this ... annotation (NetWare or symbian but not MSWin32).
    #     Then .... could easily become ../../.. etc:
    # Replace \.\.\. by (\.\.\.+)  and substitute with
    # { $1 . ".." . "\\.." x (length($2)-2) }gex
                                             # ... --> ../..
    $path =~ s{ (\A|\\)                        # at begin or after a slash
                    \.\.\.
                (?=\\|\z)                 # at end or followed by slash
             }{$1..\\..}gx;
                                            # xx\yy\..\zz --> xx\zz
    while ( $path =~ s{(?:
                (?:\A|\\)                # at begin or after a slash
                [^\\]+                        # rip this 'yy' off
                \\\.\.
                (?<!\A\.\.\\\.\.)        # do *not* replace ^..\..
                (?<!\\\.\.\\\.\.)        # do *not* replace \..\..
                (?:\\|\z)                 # at end or followed by slash
               )+                        # performance boost -- I do not know why
             }{\\}sx ) {}

    $path =~ s#\A\\##;                        # \xx --> xx  NOTE: this is *not* root
    $path =~ s#\\\z##;                        # xx\ --> xx

    if ( $volume =~ m#\\\z# )
    {                                        # <vol>\.. --> <vol>\
        $path =~ s{ \A                        # at begin
                    \.\.
                    (?:\\\.\.)*                # and more
                    (?:\\|\z)                 # at end or followed by slash
                 }{}x;

        return $1                        # \\HOST\SHARE\ --> \\HOST\SHARE
            if    $path eq ""
              and $volume =~ m#\A(\\\\.*)\\\z#s;
    }
    return $path ne "" || $volume ? $volume.$path : ".";
}


其中的shift, return _canon_cat( "/", @_ )
        if $_[0] eq "";能解释下吗 my $volume = $first =~ s{ \A ([A-Za-z]:) ([\\/]?) }{}x        # drive letter
                   ? ucfirst( $1 ).( $2 ? "\\" : "" )
               : $first =~ s{ \A (?:\\\\|//) ([^\\/]+)
                                 (?: [\\/] ([^\\/]+) )?
                                        [\\/]? }{}xs                        # UNC volume
               ? "\\\\$1".( defined $2 ? "\\$2" : "" )."\\"
               : $first =~ s{ \A [\\/] }{}x                        # root dir
               ? "\\"
               : "";ss{ \A ([A-Za-z]:) ([\\/]?) }{}x咋和s///不是同类的格式s{{{;

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
2 [报告]
发表于 2014-09-05 15:42 |只看该作者
  1. s{ \A ([A-Za-z]:) ([\\/]?) }{}x
  2. s/ \A ([A-Za-z]:) ([\\\/]?) //x
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP