Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Possible to refer to variable defined later in script?

by Athanasius (Monsignor)
on Nov 12, 2012 at 16:35 UTC ( #1003477=note: print w/ replies, xml ) Need Help??


in reply to Possible to refer to variable defined later in script?

It is possible to read the filehandle’s position immediately before printing the empty (zero) value, and then later to return to this position in the file and overtype the zero value with the correct one. But this requires the file to be opened for writing rather than appending:

open(my $HTML, '>', 'OUT-access.html') or die "Cannot open file 'OUT-a +ccess.html' for writing: $!"; ... print $HTML "There were "; my $offset = tell $HTML; printf $HTML "%7d", 0; print $HTML " unique visitors in the logfile.<BR>\n"; ... # Calculate $IPcount; ... seek $HTML, $offset, 0; printf $HTML "%7d", $IPcount;

(The value of “7” in the format "%7d" assumes that the count will have at most 7 digits. Choose whatever number is appropriate for your use-case.)

See tell and seek.

Athanasius <°(((><contra mundum


Comment on Re: Possible to refer to variable defined later in script?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2014-11-26 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (172 votes), past polls