write to 2 file handles simultaneously

Hi, I want to know how could i write 2 file handles simultaneously in Perl? Any module i need to have? My problem is as below.

open( INFILE, "data" ); #open to read the data file
open( STDOUT, ">/dev/tty" );
open( SAVED, ">output" );

while() {
print #what should i put here?
}


Yup, i want to print the contain in data file to the screen and also save into the output file. How could i do so? Thanks

<< StephenLim >>

Comments

  • : Hi, I want to know how could i write 2 file handles simultaneously
    : in Perl? Any module i need to have? My problem is as below.
    :
    : open( INFILE, "data" ); #open to read the data file
    : open( STDOUT, ">/dev/tty" );
    : open( SAVED, ">output" );
    :
    : while() {
    : print #what should i put here?
    : }
    :
    :
    : Yup, i want to print the contain in data file to the screen and also
    : save into the output file. How could i do so? Thanks
    I take it that you mean how can I do it besides simply writing:-

    print STDOUT $stuff;
    print SAVED $stuff;

    Which will work. For something neater...I'm struggling. The easiest option may be a sub:-

    [code]sub dprint {
    print STDOUT @_;
    print SAVED @_;
    }[/code]

    Then you can do:-

    dprint $stuff;

    To print it. That feels a bit of a hack, but I can't think of anything better right now.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • : Hi, I want to know how could i write 2 file handles simultaneously in Perl? Any module i need to have? My problem is as below.
    :
    : open( INFILE, "data" ); #open to read the data file
    : open( STDOUT, ">/dev/tty" );
    : open( SAVED, ">output" );
    :
    : while() {
    : print #what should i put here?
    : }
    :
    :
    : Yup, i want to print the contain in data file to the screen and also save into the output file. How could i do so? Thanks
    :
    : << StephenLim >>

    I'm just giving my 2 cents because what Jonathan told you is correct. I just don't understand what you're really asking.
    Since you have both filehandles open, you just print to those filehandles and the same data goes to each file.

    X
  • There are a couple of modules that may help.
    FileHandle::Multi
    IO::File::Multi

    Both are available through CPAN or PPM

    : Hi, I want to know how could i write 2 file handles simultaneously in Perl? Any module i need to have? My problem is as below.
    :
    : open( INFILE, "data" ); #open to read the data file
    : open( STDOUT, ">/dev/tty" );
    : open( SAVED, ">output" );
    :
    : while() {
    : print #what should i put here?
    : }
    :
    :
    : Yup, i want to print the contain in data file to the screen and also save into the output file. How could i do so? Thanks
    :
    : << StephenLim >>
    :
    :

  • : There are a couple of modules that may help.
    : FileHandle::Multi
    : IO::File::Multi
    :
    : Both are available through CPAN or PPM
    :
    Ah, cool. I didn't know about those. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • If you really wanna get some syncrounous(sp?) filehandle action going you can call fork, or if you're on the riske side, maybe some of that ever so sexy perl thread action heh.

    : : There are a couple of modules that may help.
    : : FileHandle::Multi
    : : IO::File::Multi
    : :
    : : Both are available through CPAN or PPM
    : :
    : Ah, cool. I didn't know about those. :-)
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :

    -----------------------
    "Thou shalt not follow the null pointer, for chaos and danger lie at its end!"
    --C++ Commandments, 1st commandment

  • : If you really wanna get some syncrounous(sp?) filehandle action
    : going you can call fork,
    Hehe...yeah, it's synchronous. :-) Though if you do some fancy thing where you print and it forks each time, there is a real potential for things to go really forking wrong. Just imagine you print to both handles twice, this possibly might happen to you:-

    Multi-print called first time
    Forks
    Original app prints first output.
    Original app still has its time slice, forked proccess is waiting.
    Multi-print called second time
    Forks
    Original app's time slice ends
    For some weird reason, the proccess that was just forked runs
    Then, after all that, the proccess generated from the first fork runs

    OK, maybe that is not likely to happen, but then you fork you can never know what is going to get run when. I'll tell you for free that I've seen my Forking Perl Port Scanner find a higher port open before finding a lower one open. OK, maybe it's a network traffic issue, but it's hard to be sure.

    Also if you do fork, think of the overhead. When you think about the whole time slicing thing going on, there is a good chance the timing between the two outputs taking place is longer if you fork than if you do one print statement after the other.

    : or if you're on the riske side, maybe some of that ever so sexy perl
    : thread action heh.
    ...and if you thought proccesses where bad, just check out threads. :-) They're something I want to play with more; you just have to be careful about shared data. But threads are a Good Thing if you can get them working well for sure.

    Thankfully, Parrot has had thread support in mind from the start. So Perl 5.10 (aka Ponie) and Perl 6 should have much more solid cross-platform thread support. It seems it's not the most reliable thing at the moment. Probably because threads are more of a Windows thing that a UNIX one (Win32 architecture makes having many threads efficient, as threads are lightweight. I believe they are "heavier" on UNIX OSes.)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories