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

Okay, so ... I have just read from an external file and printed out what was stored on the external file to the browser. for arguement sake lets say that the value of the external file is " 3 " How do I go about adding this value to another value. I would like both the external value + another value added together to create an outcome value. I've trouoble so many possible ideas just appear to be banging my head off the wall some what. any advice or help would be very grateful
  • Comment on Modifying file input before displaying output

Replies are listed 'Best First'.
Re: Modifying file input before displaying output
by moritz (Cardinal) on Jan 28, 2008 at 14:01 UTC
    You have to
    1. Print a http header (at least a Content-Type header)
    2. open the file (check for errors!)
    3. Read from the file (with the $variable = <$filehandle>; syntax) and print its content
    4. close the file

    Update: Op updated the post, so I'll do that as well ;) Step 3 involves a small computation then. And probably chomp to remove the newline from the string before treating it as a number.

Re: Modifying file input before displaying output
by Limbic~Region (Chancellor) on Jan 28, 2008 at 13:59 UTC
    SillyMonk,
    Traditionally, you would be asked to show what you have so far. You would also be admonished for such a poor node title. I am tired and cranky and want to forget about my problems so I am working on yours.
    #!/usr/bin/perl -T use strict; use warnings; use CGI; my $q = CGI->new(); print $q->header; my $other_value = 42; open(my $fh, '<', 'input.file') or die "Unable to open 'input.file' fo +r reading: $!"; while (<$fh>) { s/^\s+|\s+$/g; # strip whitespace $_ += $other_value; print "$_ "; }
    I haven't tested this. I don't know what your real data looks like. I have glossed over the fact you probably should be using HTML::Template or other templating for your output.

    Cheers - L~R