Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Changing data in a directory

by oaklander (Acolyte)
on Feb 05, 2002 at 11:21 UTC ( [id://143409]=perlquestion: print w/replies, xml ) Need Help??

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

Please advise why this script will not work. It gives me messages saying it is not a directory or "File does not open: Permission denied at..." Yet I have all the permissions and I can open the directory and files with all other scripts. I have tried "open" and "opendir" but cant get this to work?? I have tried on Solaris and Windows NT.
#!/usr/local/bin/perl @files = glob('/perl/bin/dir'); foreach $db (@files) { opendir(DATA, "$db") or die "File does not open: $!"; @data = (<DATA>); close (DATA); open(DATA, ">$db") or die "File not open: $!"; foreach $line (@data) { $line =~ s/<meta name\=\"/<meta name\=\"area\./gi; print DATA $line; } close(DATA); }

Replies are listed 'Best First'.
Re: Changing data in a directory
by dws (Chancellor) on Feb 05, 2002 at 11:33 UTC
    You're treating $db as both a directory and a file. I suspect that   opendir(DATA, "$db") or die "File does not open: $!"; should be   open(DATA, "$db") or die "File does not open: $!"; It also helps to report the filename in the error message.   open(DATA, $db) or die "$db: $!";
Re: Changing data in a directory
by rdfield (Priest) on Feb 05, 2002 at 11:35 UTC
    Change
    @files = glob('/perl/bin/dir');
    to
    @files = glob('/perl/bin/dir/*');
    and see if that helps.

    rdfield

    update and change opendir to open

      Many thanks for all the assistance! It now works. I needed the * to make it work as you suggested:
      @files = glob('/perl/bin/dir/*');
Re: Changing data in a directory
by gellyfish (Monsignor) on Feb 05, 2002 at 12:05 UTC

    I think you are going to have explain what it is you are trying to do here, because it isn't entirely clear from your code.

    If all you want to do is edit all of the files in the directory /perl/bin/dir then you can just do:

    perl -pi.bak 's/<meta name="/<meta name="area./gi' perl/bin/dir/*

    In the code that you have the glob() will only return one value (ie./perl/bin/dir) and then you appear to get confused about what opendir does.

    If you want to do this in longer code rather than a one-liner then you might try something like:

    #!/usr/bin/perl -w use strict; use Fcntl qw(:seek); my $dir = './dir'; opendir(DIR,$dir) || die "Can't open $dir - $!\n"; my @files = map { "$dir/$_" } grep !/^\.{1,2}$/, readdir(DIR); closedir DIR; foreach my $file (@files) { open(FILE, "+<$file") || die "Can't open $file - $!\n"; my $contents = do { local $/; <FILE> }; $contents =~ s/<meta name="/<meta name="area./gis; truncate(FILE,0) || die "truncate - $file : $!\n";; seek( FILE,SEEK_SET,0) || die "seek - $file : $!\n"; print FILE $contents; close FILE; }

    /J\

      Thanks for the additional info. Now everything works. Reference your script I would like to learn more about how it works. Can you please explain what is going on in this script? I am not familiar with this part:
      my @files = map { "$dir/$_" } grep !/^\.{1,2}$/, readdir(DIR);
      And I need some guidance on what the script is doing here:
      truncate(FILE,0) || die "truncate - $file : $!\n";; seek( FILE,SEEK_SET,0) || die "seek - $file : $!\n";
      Thanks!
Re: Changing data in a directory
by BazB (Priest) on Feb 05, 2002 at 11:42 UTC

    I can't quite see what you're doing, but I can point out a few errors.


    I've not messed about with opendir() so I'm going to steer clear of that.

    Cheers.

    BazB.

    Update: Looks like rdfield and dws have it cracked.
    Looks like the use of opendir() was incorrect.
    I'll leave this node here, since the advice is still valid.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found