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

1: I save a file then later in the program I reopen it but it dosent yet contain the information that I saved until I refresh the page. So if if I appended the file 'info.dat' and added 'hello there' I wouldent see what I wrote until I reloded the script. Is there anyway that I can get around this with like a delay or something?

- I'm using tripod by the way -

2: How do you print a format to the browser/STDOUT.

$name1 = 'whiteperl'; $name2 = 'batman'; format NAMES = @<<<<<<<< @<<<<<<<<< $name1 $name2 .

How do I get NAMES to go to the browser/STDOUT?

thanks.

2002-06-30 Edit by Corion : Added formatting, changed title

  • Comment on Two questions about CGI and format specifiers (was: Two questions)
  • Download Code

Replies are listed 'Best First'.
Re: Two questions
by caedes (Pilgrim) on Jun 30, 2002 at 08:23 UTC
    1. There is no reason that you shouldn't be able to read what you just wrote to the file. That is, assuming you are manipulating the file correctly. Be sure to close any files you open. For a better answer we need to see the code.

    2. To print to STOUT for the browser to get it through CGI you need to first print the HTTP headers:

    print "Content-type: text/html\n\n";
    Then print anything you want the browser to see:
    print $stuff;
    For details on using format() consult the Perl core docs.

    -caedes

      Thanks for editing my text - whoever it was :) - I gotta brush up on my html.

      So to print the format to STDOUT

      I rename the format STDOUT like:

      print "Content-type: text/html\n\n"; $name1 = 'whiteperl'; $name2 = 'batman'; format STDOUT = @<<<<<<< @<<<<<<<< $name1 $name2 . $~ = STDOUT; # Is this necessary? print STDOUT;

      Will this work? Tripod CGI is very restrictive. I don't think that I have ever gotten a select() to work, so I use $~.

      In the above, STDOUT isn't associated with a file handle. That's why I'm not too sure. But I'll go and see.

        You should check out Site How To to see about formating your posts here. As for format(), I can't say much because I've never used it, but print STDOUT; is most definitely wrong. :-)

        -caedes

Re: Two questions
by Zaxo (Archbishop) on Jun 30, 2002 at 08:43 UTC

    On the format question, rename to format STDOUT ..., or else see perlvar $~.

    After Compline,
    Zaxo

Re: Two questions about CGI and format specifiers (was: Two questions)
by bronto (Priest) on Jul 01, 2002 at 16:54 UTC
    Is there anyway that I can get around this with like a delay or something?

    Can't tell without seeing a snippet of the code, sorry

    How do I get NAMES to go to the browser/STDOUT?

    Fast solution: use STDOUT instead of NAMES.

    A little more elaborated solution: you could use IO::File, creating filehandle-like objects, like this:

    use strict ; # please, do use IO::File ; my ($name1,$name2) ; my $fh = IO::File->new() ; $fh->fdopen(fileno(STDOUT),'w') or die "Cannot open STDOUT: $!" ; $fh->format_name('NAMES') ; # Write to STDOUT using $fh, like this foreach my $data ([qw(mama papa)],[qw(tick tock)],[qw(cip ciop)]) { ($name1,$name2) = @$data ; $fh->format_write() ; } # your format definition goes here format NAMES = @<<<<<<<< @<<<<<<<<< $name1 $name2 .

    For more information see IO::File documentation, which will lead you almost immediately to IO::Handle docs.

    Note that you could do this also with plain Perl, too. Anyway, I prefer the object-oriented approach, since it doesn't depend on things such select calls and $~ variables. Instead, $object->format_name('MYFORMAT') call binds MYFORMAT to the specified object, which I find so desirable :-)

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }