Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Tie::file and html vs dos

by Flubb (Acolyte)
on Feb 07, 2008 at 17:32 UTC ( [id://666823]=perlquestion: print w/replies, xml ) Need Help??

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

Hullo, I have the following script with bits snipped out:
my(@cvscommits) = ("Lala", "po", "tinkywinky"); my($cvs) = join("<br>", @cvscommits); my($updatetime) = "11"; my($line) = "<tr><td><a href=\"\\\\$COMPUTERNAME\\$softdev\\$dirtime\" +<b>$COMPUTERNAME</b></a></td><td>$processors</td><td>$dirtime</td><td +>$cust</td><td>$target</td><td>$cotime</td><td>$updatetime</td><td>$b +uildtime</td><td>$result</td><td>$cvs</td></tr>\n"; for (@testarray) { #find the line below if (/<tr><th>PC Number<\/th><th>Cores<\/th><th>Date<\/th><th>C +ustomer<\/th><th>Target<\/th><th>C\/O Time<\/th><th>Update time<\/th> +<th>Build Time<\/th><th>Status<\/th><th>Changes from Last Build<\/th> +<\/tr>/) { print "Whee found it \n"; #then add the following line *after it* $_ .= $line; last;}} untie @testarray; #finish the array :)
It's supposed to open up an html file and insert the $line after the point referenced above. Instead of inserting $line after it (as it should) it writes $line at the bottom of the file *after* everything else. I tried with a simple text file, and it inserted my ersatz text line with ease, but as soon as I insert html, it starts adding it to the end of the file.
I know it finds the line because of the "Whee found it" message.
Whenever I open up the html file in an editor, it asks if I want to convert the html file to a dos file. (I also end up with some funky ^M characters under Vim.)
So the questions are:
1.Is there something fundamentally different between html and txt files that I should know about here?
2. Have I made another noob mistake somewhere I can't see?
3. Is it likely that my join command is throwing in problems?
Thanks for your time :E

Replies are listed 'Best First'.
Re: Tie::file and html vs dos
by Tanktalus (Canon) on Feb 07, 2008 at 17:54 UTC

    Are you sure it's not working as-written? That is, the line you're adding probably *is* being added to the end of that line - right after the carriage return. Which may be 10k characters later (the end of the file). While that "dos" error message may be related, it should be easy enough to ignore it.

    I'd try this:

    for (@testarray) { if (s[(<tr><th>PC Number</th><th>Cores</th><th>Date</th><th>Customer +</th><th>Target</th><th>C/O Time</th><th>Update time</th><th>Build Ti +me</th><th>Status</th><th>Changes from Last Build</th></tr>)][$1$line +]) { last; # found it, done. } }
    Well, I might find yet other ways to clean that up.

    Actually, I'd probably use XML::Twig (if the HTML were actually well-formed XHTML) or some other HTML parser to find the location and then insert my extra text in that way.

Re: Tie::file and html vs dos
by citromatik (Curate) on Feb 08, 2008 at 09:12 UTC

    Maybe I'm wrong, but I think that you are experiencing problems with the encoding of the end of line in your html file. Is your html file written in dos/Windows?, i.e has '\r\n' at the end of each line?. Are you processing it in *nix ('\n'). That would explain why you are seeing those ^M characters in vim. If that is true, Tie::File is seeing the whole file as just one line, and inserting your $line at the end of that unique line

    You can see how many lines Tie::File is seeing with print $#testarray

    Try one of the following (not both!):

    • Convert the html file to unix with dos2unix
    • Tie the file specifying a different recsep: tie my @testarray,'Tie::File',recsep='\r\n' or die $!

    Hope this helps

    Citromatik
      Thanks guys, I'll have a go at them this weekend. The files are both processed in windows machines, but somehow the interpolation of perl functions is mucking about with that function. I had a look to see if Tie::file had anything to do with unix vs. dos formats but couldn't make any headway. Cheers ;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found