免费注册 查看新帖 |

Chinaunix

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

if (-d _) {............} 这个是什么意思,我打印了很多遍都找不到结果(谢谢) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-15 02:04 |只看该作者 |倒序浏览
if (-d _) {............}

论坛徽章:
0
2 [报告]
发表于 2010-08-15 02:06 |只看该作者
if (-d $_) 吧

论坛徽章:
0
3 [报告]
发表于 2010-08-15 02:20 |只看该作者
#!/usr/bin/perl -w
# symirror - build spectral forest of symlinks
use strict;
use File::Find;
use Cwd;

my ($srcdir, $dstdir);
my $cwd = getcwd();
die "usage: $0 realdir mirrordir" unless @ARGV == 2;

for (($srcdir, $dstdir) = @ARGV) {
    my $is_dir = -d;
    next if $is_dir;                        # cool
    if (defined ($is_dir)) {
        die "$0: $_ is not a directory\n";
    } else {                                # be forgiving
        mkdir($dstdir, 07777) or die "can't mkdir $dstdir: $!";
    }
} continue {
    s#^(?!/)#$cwd/#;                        # fix relative paths
}
                    
chdir $srcdir;
find(\&wanted, '.');

sub wanted {
    my($dev, $ino, $mode) = lstat($_);
    my $name = $File::Find::name;
    $mode &= 07777;                 # preserve directory permissions
    $name =~ s!^\./!!;              # correct name
    if (-d _) {                     # then make a real directory
        mkdir("$dstdir/andy$name", $mode)
            or die "can't mkdir $dstdir/$name: $!";
    } else {                        # shadow everything else
        symlink("$srcdir/$name", "$dstdir/$name")
            or die "can't symlink $srcdir/$name to $dstdir/$name: $!";
    }

论坛徽章:
0
4 [报告]
发表于 2010-08-15 02:20 |只看该作者
[img]#!/usr/bin/perl -w
# symirror - build spectral forest of symlinks
use strict;
use File::Find;
use Cwd;

my ($srcdir, $dstdir);
my $cwd = getcwd();
die "usage: $0 realdir mirrordir" unless @ARGV == 2;

for (($srcdir, $dstdir) = @ARGV) {
    my $is_dir = -d;
    next if $is_dir;                        # cool
    if (defined ($is_dir)) {
        die "$0: $_ is not a directory\n";
    } else {                                # be forgiving
        mkdir($dstdir, 07777) or die "can't mkdir $dstdir: $!";
    }
} continue {
    s#^(?!/)#$cwd/#;                        # fix relative paths
}
                    
chdir $srcdir;
find(\&wanted, '.');

sub wanted {
    my($dev, $ino, $mode) = lstat($_);
    my $name = $File::Find::name;
    $mode &= 07777;                 # preserve directory permissions
    $name =~ s!^\./!!;              # correct name
    if (-d _) {                     # then make a real directory
        mkdir("$dstdir/andy$name", $mode)
            or die "can't mkdir $dstdir/$name: $!";
    } else {                        # shadow everything else
        symlink("$srcdir/$name", "$dstdir/$name")
            or die "can't symlink $srcdir/$name to $dstdir/$name: $!";
    }
}[/img]

论坛徽章:
0
5 [报告]
发表于 2010-08-15 02:21 |只看该作者
  1. #!/usr/bin/perl -w
  2. # symirror - build spectral forest of symlinks
  3. use strict;
  4. use File::Find;
  5. use Cwd;

  6. my ($srcdir, $dstdir);
  7. my $cwd = getcwd();
  8. die "usage: $0 realdir mirrordir" unless @ARGV == 2;

  9. for (($srcdir, $dstdir) = @ARGV) {
  10.     my $is_dir = -d;
  11.     next if $is_dir;                        # cool
  12.     if (defined ($is_dir)) {
  13.         die "$0: $_ is not a directory\n";
  14.     } else {                                # be forgiving
  15.         mkdir($dstdir, 07777) or die "can't mkdir $dstdir: $!";
  16.     }
  17. } continue {
  18.     s#^(?!/)#$cwd/#;                        # fix relative paths
  19. }
  20.                     
  21. chdir $srcdir;
  22. find(\&wanted, '.');

  23. sub wanted {
  24.     my($dev, $ino, $mode) = lstat($_);
  25.     my $name = $File::Find::name;
  26.     $mode &= 07777;                 # preserve directory permissions
  27.     $name =~ s!^\./!!;              # correct name
  28.     if (-d _) {                     # then make a real directory
  29.         mkdir("$dstdir/andy$name", $mode)
  30.             or die "can't mkdir $dstdir/$name: $!";
  31.     } else {                        # shadow everything else
  32.         symlink("$srcdir/$name", "$dstdir/$name")
  33.             or die "can't symlink $srcdir/$name to $dstdir/$name: $!";
  34.     }
  35. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2010-08-15 02:31 |只看该作者
小骆驼上有写。。

论坛徽章:
0
7 [报告]
发表于 2010-08-15 06:32 |只看该作者
if (-d _) {............}
whitecell 发表于 2010-08-15 02:04



    对前一次访问的句柄的快速优化访问,可稍许提高性能。

$ perl -le 'open HD,"/etc/resolv.conf"; print 1 if -f HD; print 0 unless -d _'
1
0

论坛徽章:
0
8 [报告]
发表于 2010-08-15 07:43 |只看该作者
来自 perldoc 的一段解释:
      If any of the file tests (or either the "stat" or "lstat"
      operators) are given the special filehandle consisting of a
      solitary underline, then the stat structure of the previous file
      test (or stat operator) is used, saving a system call. (This
      doesn't work with "-t", and you need to remember that lstat()
      and "-l" will leave values in the stat structure for the
      symbolic link, not the real file.) (Also, if the stat buffer was
      filled by an "lstat" call, "-T" and "-B" will reset it with the
      results of "stat _"). Example:

          print "Can do.\n" if -r $a || -w _ || -x _;

          stat($filename);
          print "Readable\n" if -r _;
          print "Writable\n" if -w _;
          print "Executable\n" if -x _;
          print "Setuid\n" if -u _;
          print "Setgid\n" if -g _;
          print "Sticky\n" if -k _;
          print "Text\n" if -T _;
          print "Binary\n" if -B _;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP