Thread: Perl question

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Perl question

    How to you redirect stdout to a file? I want to do something like:

    Code:
    open(OUTPUT, ">outfile.txt");
    setstdout(OUTPUT);
    print "hello outfile.txt";
    close(OUTPUT);

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Are you sure you want to redirect stdout? You could just print to the file
    Code:
    open(OUTPUT, ">outfile.txt");
    print OUTPUT "hello outfile.txt\n";
    close(OUTPUT);
    But if you want to redirect STDOUT, STDIN, or STDERR, just open the file and assign it to that handle
    Code:
    open(STDOUT, ">outfile.txt");
    print "hello outfile.txt\n";

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    This works for me:
    Code:
    #! perl
    
    open (STDOUT, ">stdout.txt");
    
    print "what what!";
    
    close (STDOUT);
    The only thing is, I'm not 100% sure how to bring STDOUT to point back to itself originally.

    edit: Maybe this might help; some more elegant solutions http://perlmonks.thepen.com/11007.html

    edit2: And I agree, I have no idea why you would want to redirect STDOUT.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  3. perl program question
    By newbie2c in forum Tech Board
    Replies: 2
    Last Post: 02-03-2003, 10:19 AM
  4. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM
  5. perl need help pls.....
    By magnum38 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-12-2001, 10:35 PM