in reply to Re^4: Perl noob is lost
in thread Perl noob is lost
Your script doesn't compile as it is. It produces the following warnings/errors:
Found = in conditional, should be == at ./test.pl line 26. Found = in conditional, should be == at ./test.pl line 27. Found = in conditional, should be == at ./test.pl line 28. syntax error at ./test.pl line 24, near ") {" Global symbol "$page_id" requires explicit package name at ./test.pl l +ine 26. Global symbol "$page_description" requires explicit package name at ./ +test.pl line 26. Global symbol "$page_id" requires explicit package name at ./test.pl l +ine 27. Global symbol "$page_description" requires explicit package name at ./ +test.pl line 27. Global symbol "$page_id" requires explicit package name at ./test.pl l +ine 28. Global symbol "$page_description" requires explicit package name at ./ +test.pl line 28. Global symbol "$page_id" requires explicit package name at ./test.pl l +ine 29. syntax error at ./test.pl line 29, near ") (" Global symbol "$page_description" requires explicit package name at ./ +test.pl line 29. Global symbol "$content" requires explicit package name at ./test.pl l +ine 33. Global symbol "$page_description" requires explicit package name at ./ +test.pl line 33. Global symbol "$content" requires explicit package name at ./test.pl l +ine 35. Bareword "then" not allowed while "strict subs" in use at ./test.pl li +ne 31. Execution of ./test.pl aborted due to compilation errors.
You are using "strict" and "warnings" which is good, but you have to look at the warnings and errors they produce.
Starting with Found = in conditional, should be == at ./test.pl line 26.: You are using the assignment operator in the condition of an if statement which is usually (but not always) an error. I am guessing that you want a regular expression pattern match here - something like: $page_id =~ m/^27/, and similarly in the following statements. You can read more about regular expressions in perlre and more about the binding operator ( =~ ) in perlop. Both are worth reading completely once or twice, just to get familiar with what's in them. You can then return to them for details when appropriate.
Also, you have next if(...) {die}. You must choose between if() as a statement modifier and if() as a compund statement - it can't be both at the same time. It may be best to remove the {die} in this case.
Otherwise you have some relatively minor typos which you can find and fix by running your script and following up on the errors. If you want to check your script without running it you can use perl -c script.pl, assuming your script is named script.pl.
The script will not automatically save anything. If you want to update the .ATT files with the revised content, you will have to open them for output and write the new content to them. You should probably rename the originals (e.g. by appending ".bak" to the name) before writing the new content. Otherwise, if something is wrong with your script you might lose all your data. Alternatively, you can write the new content to a file with ".new" appended to the original file name and check these new files carefully before replacing the originals.
Until you are sure all is correct you might do well to do something like the following:
my $newfile = "$file.new"; open(my $fh, ">", $newfile) or die "$newfile: $!"; print $fh $content; close($fh);
|
|---|