s0ttle has asked for the wisdom of the Perl Monks concerning the following question:

I've been messing around with something similar to this and I can't figure out why it won't work.
sub x{ local(*A)=$_[0]; if(opendir(R,"/usr")){x(*R); } }
and when I try to do something like:
sub x{ local(*A)=$_[0]; if (opendir(R,"/usr")){while(<R>){print;} } }
I get:
[s0ttle@ween]%perl subtest Read on closed filehandle <C> at a line 23. [s0ttle@ween]%fuq fuq: Command not found. [s0ttle@ween]%

Replies are listed 'Best First'.
(Ovid) Re: subs && typeglobs
by Ovid (Cardinal) on Aug 03, 2001 at 23:56 UTC

    What are you trying to do? I can't tell from your example code. If you're trying to pass a filehandle to a subroutine, do this:

    somefunc( *FH ); sub somefunc { my $fh = shift; # use it like a normal filehandle }

    If you're trying to read the files from a directory handle, you don't want the diamond operator, you're looking for readdir.

    opendir SOMEDIR, $directory or die "Can't open $directory: $!"; while ( defined ( my $file = readdir SOMEDIR ) ) { # do stuff with filename } closedir SOMEDIR;

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: subs && typeglobs
by VSarkiss (Monsignor) on Aug 03, 2001 at 23:53 UTC

    Well, your first attempt opens the same directory over and over, doing nothing with the contents. Your second attempt tries to read from R as though it's a filehandle, but it's a dirhandle, which is quite different.

    "This word, I do not think it means what you think it means." If you opendir something, use readdir to read it, not the diamond <> operator. More info at perlfunc:opendir.

    HTH

      #!/usr/bin/perl -w # ## use strict; my $Spath='/home/s0ttlen/'; opendir(H, "$Spath")|| die "Error:$!\n"; dir_search(*H,"$Spath"); sub dir_search{ local(*ROOT)=$_[0]; my $path=$_[1]; my $cont; foreach $cont (sort readdir(ROOT)){ next if $cont eq '.' or $cont eq '..'; next if -l "$path$cont"; if (-f "$path$cont"){ &log("$path$cont") if (-T "$path$cont" || -u "$path$cont"); } elsif (-d "$path$cont" && opendir(D,"$path$cont")) { dir_search(*D,"$path$cont"); } } } sub log{ my $file=$_[0]; my $log='/home/s0ttlen/log/slog'; open(LOG,">>$log")|| die "Error:$!\n"; print LOG "$file\n"; }
      doesn't do what I expected?!

        OK so you are hand rolling the old recurse the directory tree chestnut. Here is a script that will do that for you putting all the dirs in @dirs and all the files in @files. I presume you already know about File::Find so won't do the use a module mantra. There is an explanation of this code here

        #!/usr/bin/perl -w use strict; my $root = 'c:/cluster1/'; my @dirs = ($root); my @files; for my $path (@dirs){ opendir ( DIR, $path ) or next; # skip dirs we can't read while (my $file = readdir DIR) { # skip the dot files next if $file eq '.' or $file eq '..'; # skip symbolic links to avoid infinite loops next if -l $path.$file; if ( -d $path.$file ) { # add the dirs to our dir list (full path) push @dirs, $path.$file.'/'; } else { # add the files to file list push @files, $path.$file; } } closedir DIR; } print "Directory list\n\n"; print "$_\n" for sort @dirs; print "\n\nFile List\n\n"; print "$_\n" for sort @files;

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      I tried the x(*F); in a program and it doesn't work.
Re: subs && typeglobs
by tachyon (Chancellor) on Aug 03, 2001 at 23:53 UTC

    What are you trying to do? It is not clear to me from your code. If you could explain the purpose it would be easier to help.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print