How do i change STDOUT back to the screen

Discussion in 'Perl Misc' started by lozbrown, Jan 9, 2007.

  1. lozbrown

    lozbrown Guest

    Ok this is a very novice question but please help,

    I have a subroutine that uses STDOUT:

    ---------------------------------code--------------------------------------------------
    sub savefile {
    my $content = get($_[0]);
    print "saving file $_[0]";
    #download the file

    die "Couldn't get $_[0]" unless defined $content;


    #Open same file for writing, reusing STDOUT
    open (STDOUT, ">$_[1]") or die "Can't open $_[1]: $!\n";
    binmode(STDOUT);
    print "$content";

    #Finish up
    close STDOUT;
    }
    ------------------------------End
    code-----------------------------------------

    but how do I send stdout back to the screen so i can use the print
    command an it will appear on the screen.

    currently after the subroutine has been called i get this error message

    print() on closed filehandle STDOUT at C:\Docum...
     
    lozbrown, Jan 9, 2007
    #1
    1. Advertising

  2. lozbrown

    boyd Guest

    In article <>,
    "lozbrown" <> wrote:

    > Ok this is a very novice question but please help,
    >
    > I have a subroutine that uses STDOUT:
    >
    > ---------------------------------code-----------------------------------------
    > ---------
    > sub savefile {
    > my $content = get($_[0]);
    > print "saving file $_[0]";
    > #download the file
    >
    > die "Couldn't get $_[0]" unless defined $content;
    >
    >
    > #Open same file for writing, reusing STDOUT
    > open (STDOUT, ">$_[1]") or die "Can't open $_[1]: $!\n";
    > binmode(STDOUT);
    > print "$content";
    >
    > #Finish up
    > close STDOUT;
    > }
    > ------------------------------End
    > code-----------------------------------------
    >
    > but how do I send stdout back to the screen so i can use the print
    > command an it will appear on the screen.
    >
    > currently after the subroutine has been called i get this error message
    >
    > print() on closed filehandle STDOUT at C:\Docum...


    Most folks use another file handle than STDOUT to do the file writing,
    leaving STDOUT to write to the screen. Others might use STDERR to do
    screen output, since it is usually attached to the terminal screen.

    To print to a file handle, just put the filehandle after the print
    statement, as in:

    print STDERR "saving file $_[0]\n";

    The way to open a filehandle is
    open OUT, ">$_[1]";
    binmode(OUT);
    in your above script, and use
    print OUT "$content";
    ....
    close OUT;

    Boyd
     
    boyd, Jan 9, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elad
    Replies:
    0
    Views:
    415
  2. SamG
    Replies:
    10
    Views:
    425
  3. Andreas S
    Replies:
    3
    Views:
    265
    Eric Hodel
    Dec 9, 2006
  4. Replies:
    2
    Views:
    343
    A. Sinan Unur
    Dec 7, 2005
  5. apparker
    Replies:
    10
    Views:
    374
    apparker
    Apr 3, 2007
Loading...

Share This Page