Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

use FileHandle, readdir???

by dbs (Sexton)
on May 25, 2011 at 20:57 UTC ( [id://906707]=perlquestion: print w/replies, xml ) Need Help??

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

Hi! Why isn't this working:
my $NuserDir = new FileHandle "+>> /home/$key"; while (my $fobj = readdir $NuserDir) { next if $fobj eq '.' or $fobj eq '..' or $fobj eq '.profile'; print ("chown -R $key:$euser_group $fobj"); } Can't use an undefined value as a symbol reference at usercfgforfacs.t +est.plx line 256.
thx!

Replies are listed 'Best First'.
Re: use FileHandle, readdir???
by toolic (Bishop) on May 25, 2011 at 21:07 UTC
    Maybe you really want to use DirHandle (instead of FileHandle) since you are opening a directory.
    use warnings; use strict; use DirHandle; my $d = DirHandle->new('/tmp'); # <-- your directory here if (defined $d) { while (defined(my $fobj = $d->read)) { next if $fobj eq '.' or $fobj eq '..' or $fobj eq '.profile'; print "$fobj\n"; # <-- chown here } undef $d; }
Re: use FileHandle, readdir???
by GrandFather (Saint) on May 25, 2011 at 21:04 UTC

    Because readdir requires a directory handle created with opendir perhaps?

    True laziness is hard work
Re: use FileHandle, readdir???
by kennethk (Abbot) on May 25, 2011 at 21:07 UTC
    Consider your error: there is only one variable on that line, so it must be undefined. A better question you should be asking is "Why didn't my open work?" I would suggest testing your open:

    my $NuserDir = new FileHandle "+>> /home/$key" or die "Open failed: $!";

    I strongly suspect that you either don't have permissions for the requested operation or you are trying to clobber a directory.

    On a side note, after you fix that, you still won't function since readdir needs a directory handle, not a file handle.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://906707]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found