in reply to Opening Unknown Filenames

Okay, lets start at the start.
1. Open Directory /var/log/cr_user
Good idea, but your code doesn't follow it. Instead of opening and reading the directory, you run ls and read it's output. I recommend something like:
my $dir = '/var/log/cr_user'; opendir( my $dh, $dir ) or die "failed opening directory $dir: $!"; my @files = grep { ! /^\./ } readdir( $dh ); # skips dirs . and .. and + all dotfiles # faster but less readable would be something like grep { substr( $_, +0, 1 ) ne '.' ) }
Next, your foreach doesn't specify it's list. It should read
foreach my $file ( @files ) {
Same for
foreach my $account ( @account ) {
I added the my declarations in there, too, you're need to declare all your vars that way for strict mode to work.
--
Snazzy tagline here

Replies are listed 'Best First'.
Re: Re: Opening Unknown Filenames
by diotalevi (Canon) on Jul 05, 2003 at 13:02 UTC

    5.6.0+ glob() uses readdir internally already. Use that and save on the readability: glob "/var/log/cr_user/*".

      I hardly think using "glob" is more readable, though <$dir/*> might be.

      I'm not really sure it is though. It's more compact, to be certain. Assuming what I did in removing the dotfiles is an invariably desired behavior, the glob works fine. But I'd rather use an explict readdir on account of, what if I decide I want the dotfiles after all? If I'm using a readdir I only have to change the grep to grep { ! /^\.\.?$/ } or  grep { /[^\.]/ } whereas if you're using glob, you have to change the whole thing to use the readdir anyways. And so if you always start with glob, as soon as you need a dotfile you'll find yourself with a mixed bag using both. Hardly more readable, that.

      If the tools isn't a sysadmin tool, it might be fine to assume you're not wanting to include dotfiles.
      --
      Snazzy tagline here

        <$dir/*> is exactly the same as glob "$dir/*", just spelled differently. I spell glob() explicitly so there isn't confusion (in other programmer's minds) about when I mean readline() or glob(). Since the stated code went to efforts to remove ., .. and the dot files it is perfectly valid (and for this purpose) clearer to only code for that.

        If the code needed to handle dot files then it makes sense to use readdir. That just isn't the case here and it adds unnecessary obfuscation.

Re: Re: Opening Unknown Filenames
by hiddenlinux (Acolyte) on Jul 05, 2003 at 10:33 UTC
    I just tried to do it your way, but had no success. Instead i tried writing it like below, but now i am stuck with the fact that i get the error 'No such file or directory' and the code doesn't run with taint!
    #! /usr/bin/perl my $directory = '/var/log/cr_user/'; opendir DIR, $directory or die $!.$/; my @files = readdir(DIR); closedir DIR or warn $!.$/; for my $file (@files) { open FILE, $file or die $!.$/; my ( $username, $domain, $email, $service ) = split /,/, <FILE>; print "$username"; close FILE or warn $!.$/; unlink $file; }

      The filenames returned from readdir are relative. You can either chdir to the directory or add the file path:

      chdir '/var/log/cr_user/'; # or use File::Spec; my $dir = '/var/log/cr_user/'; my @files = map { File::Spec->catfile( $dir, $_ ) } grep { $_ !~ /^\.{1,2}/ } readdir DIR;