Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Tie::File insertion problem

by Flubb (Acolyte)
on Jan 31, 2008 at 15:43 UTC ( [id://665372]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I have a script which needs to insert a line into an html file after a certain point (ie, at the beginning of the html file.) We have a build system that I want take results from and present the most recent results first.
The script looks like: (I've redacted redundant information)
#!/usr/local/bin/perl -w use strict; use Env qw(COMPUTERNAME); use Tie::File; use File::Copy; my(@cvscommits) = ""; my($cvscommits) =""; my($buildresultserver) = "\\\\Cvs3b\\stuff\\"; my($htmloldfile) = "Verification_Builds1.html"; print "Going to server!\n"; chdir $buildresultserver or die "Cannot change to cvs3b $!\n"; print "going to tie the file\n"; tie @cvscommits, 'Tie::File', "$htmloldfile", or die "Cannot open $htm +loldfile $!\n"; for (@cvscommits) { if (/<th>PC Number<\/th><th>Cores<\/th><th>Date<\/th><th>Customer< +\/th><th>Target<\/th><th>C\/O Time<\/th><th>Build Time<\/th><th>Statu +s<\/th><th>Changes from Last Build<\/th><tr>/) { print "Whee found it \n"; $_ .= "<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>$buildtime</td><td>$resu +lt</td><tr>@cvscommits</tr>\n"; last; }} untie @cvscommits;
I've been following a previous answer:
http://www.perlmonks.org/?node_id=155567
What I thought the script was going to do
Go to the file on the server and open it into an array
Search for the pattern that begins with <th>PC Number... and then after that line, insert my line <td><a href...
What is actually happening is that the contents of the html get copied repeatedly every time the script is run, so I get two copies of the contents, rather than the 1 line added I thought I was going to get. It finds the <th>PC Number</th> but then tacks the entire contents of the file onto the end of that line.
The html code is simply the following:
<html> <head> <title> BUILD ENVIRONMENT INFORMATION </title> <STYLE type="text/css"> </STYLE> </head> <body><hr> <h2><center> Daily Verification </center></h2> <table border="1"> <tr> <th>PC Number</th><th>Cores</th><th>Date</th><th>Customer</th><th>Targ +et</th><th>C/O Time</th><th>Build Time</th><th>Status</th><th>Changes + from Last Build</th><tr>

And then my script is supposed to add things to the top of the file. Can anyone enlighten me why it's not doing what I thought it would?

Replies are listed 'Best First'.
Re: Tie::File insertion problem
by citromatik (Curate) on Jan 31, 2008 at 16:47 UTC

    In:

    $_ .= "<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>$buildtime</td><td>$resu +lt</td><tr>@cvscommits</tr>\n";

    You are including the @cvscommits entire array, which happens to be tied to your html file. That is causing that the entire file is being inserted in that place

    citromatik
      Please tell me where I can buy your eagle eyes :(
      Thanks :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found