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

First things first.
Great site and great people.
This is my first post ever, so I want to thank everybody for the help I got from this site since now.

Then the question:

let us suppose the following.

#!usr/bin/perl print <<HTMLCODE; ZXCZXC zxcZXCzx zxczxczxc zxczxczxzx HTMLCODE


Many (if not all :p) of you here know that the code outputs the text rows to STDOUT, wherever it is redirected.

My NECESSITY :) is to be able to assign the print<<HTMLCODE to a VARIABLE, kind of

#!usr/bin/perl $STDOUT_OVERRIDE_VARIABLE= print <<HTMLCODE; ZXCZXC zxcZXCzx zxczxczxc zxczxczxzx HTMLCODE


mumble mumble... pity it doesn't work...

please someone help me! I just need a workaround, not an alternative, since I know how to, for example, read from a file or similar, but I just want to assign the printing to a variable!
Is there any way to assign STDOUT TO SOMETHING? like
$variable = STDOUT so that the STDOUT printing gets stored into the variable?

Thanks in advance.

Replies are listed 'Best First'.
Re: print htmlcode issue
by mce (Curate) on Apr 19, 2002 at 11:53 UTC
    Hi,

    I don't know what your skill level is in perl, so I'll try to be general.

    print is a function which prints to the selected filehandle, by default STDOUT and returns true or false

    A way to actually print something into a variable is to use sprintf.

    BUT..., I guess your design isn't completely what is should be. Start by seperating the input to the output, and do something like this:

    $STDOUT_OVERRIDE_VARIABLE= <<HTMLCODE; ZXCZXC zxcZXCzx zxczxczxc zxczxczxzx HTMLCODE print STDOUT $STDOUT_OVERRIDE_VARIABLE;
    or change the STDOUT filehandle to something else using open.

    Just my 0.02$ worth
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

Re: print htmlcode issue
by davis (Vicar) on Apr 19, 2002 at 10:37 UTC
    Try this:
    #!/usr/bin/perl -w use strict; my $foo = <<'END'; The cat sat on the mat. The cow jumped over the moon. The horse fell down the stairs. END print $foo; #Or whatever you want to do with it.
    Hope that helps
    Update: On re-reading, this isn't quite the answer you were looking for - but it may still help to answer your question.
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

      my $foo = <<'END'; ... print $foo;

      Or in one go:

      print $foo = <<'END'; ... END

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Re: print htmlcode issue
by Zaxo (Archbishop) on Apr 19, 2002 at 10:45 UTC

    You need to obtain a filehandle for wherever you wish to direct the print:

    open FH, "> somefilename" or die $!; print FH <<HTMLCODE; ZXCZXC zxcZXCzx zxczxczxc zxczxczxzx HTMLCODE close FH or die $!;
    The handle may also be from pipe or socket.

    Update: To get STDOUT assigned somewhere else, just use that as the filehandle:

    open STDOUT, "> thefilepath" or die $!;
    Then the default handle for print is the file.

    Update: Juerd, yes, buffered writes will fail on close if the device is full or quota exceeded.

    After Compline,
    Zaxo

      close FH or die $!;

      Does close ever fail?

      To me, it seems testing the print makes more sense:

      print FH $foo or die $!;
      Which can come in handy, because filehandles are not protected by strict.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        Yes,

        they can be in 5.6.1.

        use strict; my $fh; open ( $fh, "> /tmp/out") or die $!; print $fh "hallo"; close($fh); exit 0;
        Or by using the IO modules.

        Which has the advantage of autoclosing when leaving the block.
        ---------------------------
        Dr. Mark Ceulemans
        Senior Consultant
        IT Masters, Belgium

Re: print htmlcode issue
by Juerd (Abbot) on Apr 19, 2002 at 10:39 UTC

    $variable = STDOUT so that the STDOUT printing gets stored into the variable?

    Not that way. You can however, tie a handle to a class. Fortunately for you, a module that does what you want already exists. Have a look at Tie::Handle::Scalar.

    use Tie::Handle::Scalar; { local *STDOUT; # Don't want to override it forever tie *STDOUT, 'Tie::Handle::Scalar', \my $variable; print "Hello, world!\n"; print STDERR "\$variable contains: $variable"; }

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: print htmlcode issue
by ejf (Hermit) on Apr 19, 2002 at 11:28 UTC

    Welcome to the monastery :)

    First off, why would you want to assign a print to a variable ? Why not just assign the string itself ?

    my $test = " ZXCXlala blub hey ";

    works just as well ... print $test; will work as expected.
    Of course you could also do

    my $test = <<HTML SGSDF aSDGAWR HTML ;

    if you want to use HERE-Documents.

    You can probably play games with STDIN and STDOUT, but that's really not needed here at all ...