Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Passing file handle to tee that goes into open

by pjkang7 (Novice)
on Nov 09, 2016 at 22:18 UTC ( [id://1175627]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm trying to print to multiple file handles and I have found several examples

However, when I tried to pass file handle to it, instead of file name, it fails to print. Here's several code that I tried which produces error

my $f1 = "fileName1"; my $f2 = "fileName2"; open my $fh_email, ">", $f2; #Print stuff to $fh_email open($fh, "| tee -a $f1 $fh_email") or die "Teeing off: $!\n"; #Several other variations I have tried #open($fh, "| tee -a $f1 \*$fh_email") or die "Teeing off: $!\n"; #open($fh, "| tee -a $f1 \\*$fh_email") or die "Teeing off: $!\n"; #open($fh, "| tee -a $f1 \\*\$fh_email") or die "Teeing off: $!\n"; #open($fh, "| tee -a $f1 \*\$fh_email") or die "Teeing off: $!\n"; #Wh +ich was a big mistake as I appended bunch of stuff into lots of file +(which of course I simply did svn revert *) #Print stuff into both files close $fh; #Then continue printing to $fh_email

Error message I see are something like this:

sh: -c: line 0: syntax error near unexpected token `(' sh: -c: line 0: `tee -a fileName1 \*GLOB(0x2c06b18)'

Maybe I just have to use for loop to print to both file handles.... Easy way out, but I still would like to know where I've got it wrong :(

Thanks for help!!

Replies are listed 'Best First'.
Re: Passing file handle to tee that goes into open
by choroba (Cardinal) on Nov 09, 2016 at 22:30 UTC
    You can't pass a file handle to tee, it's a shell command, it doesn't understand Perl file handles.

    I'd probably just print to several file handles:

    sub tee { my ($fhs, $strings) = @_; print {$_} @$strings for @$fhs; } open my $fh1, '>', $f1 or die "$f1: $!"; open my $fh2, '>', $f2 or die "$f2: $!"; tee([ $fh1, $fh2 ], [ 'Whatever you want to ', $print ]);
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Oh yeah of course... I spent too much time trying to figure that out. I guess I will have to simply iterate of file handles. I wonder what will happen if I try to tee(append) directly to the file without closing the perl file handle for that file. Intuition tells me I shouldn't but yeah..

      Thanks!

        If you have written to an open file handle in a Perl script the file pointer is at the end of the file. If you then append something to that file using an external program the file pointer in the Perl script is not updated and any subsequent writes in the script will overwrite the externally written data. Doing a seek to end of file in the script after the external program has completed should update the file pointer so that subsequent writes by the script continue after the external data.

        There may be better ways to write both internal and external data to the same file but I have used this technique without problems.

        Cheers,

        JohnGG

        "I wonder what will happen if I try to tee(append) directly to the file without closing the perl file handle for that file. Intuition tells me I shouldn't..."

        Perhaps worthy of a test, but I'd go with your instinct here. You may introduce an issue in code that may work now, but when it goes awry later, you may be scratching your head as to wtf is happening... "it worked before!".

        I've never tried it to be honest, but doing things manually outside of an open file handle just can't be good :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1175627]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found