in reply to Re: Perl noob is lost
in thread Perl noob is lost
Is there a way to retain the original format of the document? These documents will be read by another program.
OBJECT=(removed) page_id=#### (usually 3-5 digits) page_description=GRAPHICS product=(removed) study_number=(removed) content_provider=(removed) site_code=### subject_id=######### CONTENT=test.pdf SAVE
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl noob is lost
by ig (Vicar) on Apr 05, 2009 at 18:29 UTC | |
There are many alternatives. Here is one way that keeps the overall layout of the file:
In this case, the entire file content is read into a single string (slurped) then pattern matching and substitution are used to extract data and modify the description without chaning the overall layout. The result is:
| [reply] [d/l] [select] |
by sensesfail (Initiate) on Apr 06, 2009 at 01:24 UTC | |
| [reply] [d/l] |
by ig (Vicar) on Apr 06, 2009 at 21:10 UTC | |
Your script doesn't compile as it is. It produces the following warnings/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:
| [reply] [d/l] [select] |