Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: (Buzzcutbuddha - Some Corrections) - File Find

by oaklander (Acolyte)
on Jan 16, 2002 at 00:35 UTC ( [id://139026]=note: print w/replies, xml ) Need Help??


in reply to Re: (Buzzcutbuddha - Some Corrections) - File Find
in thread File Find

I get an error message with this attempt saying it doesnt like the 'use warnings;' on line 2. Please advise if I need to add a 'warnings' module? Here is the error message:
BEGIN failed--compilation aborted at C:\Perl\bin\find9.pl line 2.

Script:
use strict; use warnings; use File::Find; my @dirs = qw( . ); find ( { wanted => \&change, no_chdir => 1}, @dirs ); sub change { my $file = $File::Find::name; if ( -f $file && $file =~ /\.html$/ ) { open(IN, $file) or warn "CANT OPEN FILE!\n"; my $buffer; { local $/ = undef; $buffer = (<IN>); } close IN; my $changecount = ($buffer =~ s/FLORIDA/NEWWORD/gi); if ($changecount) { open(OPF,">$file") or warn "NOT OPENING FILE FOR M +OD, $!\n+"; print OPF "$buffer"; close OPF; } } }

I tried running this without the 'use warnings;' part and it only changed words in the main directory so I assume I need this part to work so it changes the sub directories as well?

Replies are listed 'Best First'.
Re: (Buzzcutbuddha - Further Help) - File Find
by buzzcutbuddha (Chaplain) on Jan 16, 2002 at 01:30 UTC

    hmmmmm. this is certainly a stubborn little script we're dealing with. alright.

    About Warnings
    use warnings; is equivalent to putting the little -w at the end of your 'shebang line'. So if your Perl implementation does not grok use warnings' change your script to read #!/usr/bin/perl -w at the top of your code. This will have the same effect. Warnings have the effect of telling Perl you want it to complain about any code that's incorrect or not clear. An example would be (at least until Perl 6 comes out):

    Without warnings, Perl will compile and run the following code

    # this is a slice, probably not intended @someList[0] = 'foo';
    but with warnings, if you tried to use the above code, Perl will complain:
    @someList[0] better written as $someList[0]
    which is what you want. Using warnings (either through 'use warnings' or the '-w' flag is one of the most important steps you can take towards being a good Perl programmer.

    Back To the Problem At Hand
    That all being said, 'use warnings;' is not needed to make this script run correctly.

    Some Questions

    • What version of Perl are you using, and on which platform?
    • How big are the directories that you're trying to parse?
    • Are the directories local or are they network shares?
    And have you tried adding some debugging to the script to see what it's doing? Maybe you want to do something like:
    #!/usr/bin/perl -w #alternative warning syntax use strict; use File::Find; ######################################################### # a debugging variable ######################################################### my $totalChanges; my @dirs = qw(.); find ( { wanted => \&change, no_chdir => 1}, @dirs ); sub change { my $file = $File::Find::name; if ( -f $file && $file =~ /\.html$/ ) { ################################### # debugging print statement ################################### print "going into $file\n"; my $buffer; open(IN, $file) or warn "CAN'T OPEN $file!\n"; { local $/ = undef; $buffer = (<IN>); } close IN; my $changecount = ($buffer =~ s/FOO/BAR/gi); if ($changecount) { ####################################### # increment debug variable ####################################### $totalChanges++; ####################################### # debugging statement ####################################### print "changed $changecount instances in $file\n"; open(OPF,">$file") or warn "NOT OPENING $file FOR MOD, $!\ +n"; print OPF "$buffer"; close OPF; } } } ################################################### # debugging statement ################################################### print "$totalChanges files changed\n";

      I will have to play around with what you gave me to make it work. I appreciate all of your help on this! Can you just explain what this line is doing?
      find ( { wanted => \&change, no_chdir => 1}, @dirs );
      Is 'wanted' a function in the find module?? What is 'no_chdir => 1' doing??? Answers to your questions about what I am using. Perl version 5.005 on Windows NT. Directories are small size and are local

        From the File::Find doc pages: The first argument to find() is either a hash reference describing the operations to be performed for each file, or a code reference.

        wanted: The value should be a code reference. This code reference is called the wanted() function below.

        no_chdir: The script does not change each directory as it recurses.

        What that basically means is if you only want to pass the code reference, you can just pass if, as you did in your first script. However, if you want to add any parameters to your call of File::Find (all of which are explained in the doc pages), you need to construct an anonymous hash:
        {wanted => \&coderef, parameter => 'value'}

        This is a common way of passing named parameters into functions.

        hth.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found