Click to See Complete Forum and Search --> : perl: printing to two different places?


YaRness
01-18-2001, 11:08 AM
is there another way to do something like this:


print SOMEWHERE $string;
print SOMEOTHERWHERE $string;

?
i mean, i could do something like

sub myprint {
print STDOUT $_;
print LOGFILE $_;
}

but that's so boring.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

[This message has been edited by YaRness (edited 18 January 2001).]

jemfinch
01-18-2001, 11:38 AM
You could try this:

open(OUT, "| tee $logfile")
print OUT "a wondrous string!\n"


http://www.linuxnewbie.org/ubb/smile.gif

Jeremy

TheLinuxDuck
01-18-2001, 11:48 AM
You could:

myprint $string;
#
sub myprint($;@)
{
for(@_) {
print SOMEWHERE $_;
print SOMEOTHERWHERE $_;
}
}

or

select(SOMEWHERE);
print $string;
print SOMEOTHERWHERE $string;


I don't know of an easy solution minus making your own print function. I suppose you could make a print function that examines the type of input received (using refs), and pass in multiple file handles, and strings to print. Maybe something like:

#!/usr/bin/perl -w
use strict;

open SOMEWHERE, ">test.txt" or die "test.txt failed: $!\n";
open SOMEOTHERWHERE, ">test2.txt" or die "test2.txt failed: $!\n";

my($string)="Monkeys love me";
my($other)="Hey now where'd jimmy go?";
mprint (\*SOMEWHERE, \$other, \*SOMEOTHERWHERE, \$string);

close (SOMEWHERE);
close (SOMEOTHERWHERE);

sub mprint
{
my(@globs,@scalars);
for(@_) {
push(@globs, $_) if(ref($_) eq "GLOB");
push(@scalars, $_) if(ref($_) eq "SCALAR");
}
for(@globs) {
for my $data(@scalars) {
print $_ $$data;
}
}
}


Although you're passing references, you can put anything you want on the line, in any order and still get data printed to each. It's a little more complicated than my firect example, but it could be set up to handle the data in different ways..

Dunno, just brainstorming for you.. http://www.linuxnewbie.org/ubb/wink.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

YaRness
01-18-2001, 12:05 PM
Originally posted by jemfinch:
You could try this:

open(OUT, "| tee $logfile")
print OUT "a wondrous string!\n"


http://www.linuxnewbie.org/ubb/smile.gif

Jeremy

"hey boss, can i install this UnixUtils thing on the lab computer on the naval base?"

maybe i can fake something like that with dos...

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

YaRness
01-18-2001, 12:12 PM
Originally posted by TheLinuxDuck:
Dunno, just brainstorming for you.. http://www.linuxnewbie.org/ubb/wink.gif


more like brain hurricaine!

i'll prolly just hardcode a subroutine to print to STDOUT and whatever outputfile i'm designating.

actually, i think i'm only going to need to do that dual-stuff once (to print out one array that i am buffering my output in), i'll prolly just write two print lines.

oh god, i still have to comment this whole stupid thing. ergh.



------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

YaRness
01-18-2001, 12:34 PM
here's another way to do it (for any number of FILEHANDLES:


use strict;
open (my $fh, ">yar.txt");
my @handle_list = (*STDOUT, $fh);

foreach (@handle_list)
{
print $_ "FOOBAR\n";
}

also works if you replace "OUTPUT" with "$filehandle" or something

<edit> argh, well some SNAFUs with that under 'use strict'
<edit2> FIXED IT, see above.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/



[This message has been edited by YaRness (edited 18 January 2001).]

TheLinuxDuck
01-18-2001, 05:09 PM
Originally posted by YaRness:
here's another way to do it (for any number of FILEHANDLES:


use strict;
open (my $fh, ">yar.txt");
my @handle_list = (*STDOUT, $fh);

foreach (@handle_list)
{
print $_ "FOOBAR\n";
}



yea, but how could you put it into a sub and still allow for multiple outputs? With mine, it autodetects which is a glob to a filehandle, and which is an actual scalar to dump to the file.. http://www.linuxnewbie.org/ubb/smile.gif

(I'm just giving you a hard time, btw)

------------------
TheLinuxDuck
I have a belly button.
:wq

TheLinuxDuck
01-18-2001, 05:37 PM
Just for my own personal enjoyment http://www.linuxnewbie.org/ubb/biggrin.gif I set it up to do without references. A little cleaner to look at, but still gets the job done. Isn't perl fun?
mprint(*SOMEWHERE, $other, *SOMEOTHERWHERE, $string);

close (SOMEWHERE);
close (SOMEOTHERWHERE);

sub mprint
{
my(@globs,@scalars);
for(@_) {
push(@globs, \$_) if(ref(\$_) eq "GLOB");
push(@scalars, \$_) if(ref(\$_) eq "SCALAR");
}
for(@globs) {
for my $data(@scalars) {
print $_ $$data;
}
}
}


------------------
TheLinuxDuck
I have a belly button.
:wq

YaRness
01-19-2001, 09:02 AM
damn, you can even pass a list to that. that's neat.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

YaRness
01-19-2001, 09:30 AM
Originally posted by TheLinuxDuck:
(I'm just giving you a hard time, btw)


mine is shorter, and takes up less resources. (and since i'm only using it once, there's no issue of writing the same code >1 times.)

so NYAH NYAH NYAH NYAH NYAH.

HEY check this out, i changed stuff so it will handle references. it recursively de-references stuff, so you can past a reference to an array of refs and still get all the scalars print out i think. (and probably handle references to globs as well, since all it really does is dereference stuff and push it back on the "stack" to be examined again.) doing recursiveness this way prolly has a helluva lot of less overhead than creating a sub-subroutine, but it's kind of fugly. oh well, so are camels http://www.linuxnewbie.org/ubb/biggrin.gif

i also tentatively added a line (commented) to add STDOUT to the @globs if none are found. of course, prolly better to do a "warn" or something if no globs are found, but the idea is the same.

i ALSO (just now) took out what seemed to be extraneous references. i couldn't tell if there was some hidden reason to have them, or if it was just leftover from something else, but it still works without them.

(sorry if my variable names are hard to follow)

use strict;
my $fh = *STDOUT;
my $fh2 = *STDERR;
my @list = (1,2,3,4,5,6);
my $other = "other";
my $string = "string";
my @reflist = (26,34,57,"hike");
my $ref = \@reflist;
my $number = 69;
my $numberref = \$number;
my @refs = ($ref, $numberref);


mprint($fh, $other, $fh2, $string, @list, $ref, $numberref);
print "\nbreak\n";
mprint ($fh, @refs);
close ($fh);
close ($fh2);
sub mprint
{
my (@params) = @_;
my(@globs,@scalars);
for(@params) {
push(@globs, $_) if(ref(\$_) eq "GLOB");
push(@scalars, $_) if(ref(\$_) eq "SCALAR");
push(@params, @$_) if(ref($_) eq "ARRAY");
push(@params, $$_) if(ref($_) eq "SCALAR");
}
# if (not @globs) {
# push @globs, STDOUT;
# }
for(@globs) {
for my $data(@scalars) {
print $_ $data;
}
}
}



------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

TheLinuxDuck
01-19-2001, 12:46 PM
Qool!! http://www.linuxnewbie.org/ubb/smile.gif

I've never really messed with typeglobs before, but always wanted an excuse... http://www.linuxnewbie.org/ubb/wink.gif The only other thing I could think of wanting to do with this sub is to use the prototyping, but I don't see how it could be done without losing the flexibility of the sub. http://www.linuxnewbie.org/ubb/smile.gif

Groovy... perl is groovy.

------------------
TheLinuxDuck
I have a belly button.
:wq

YaRness
01-19-2001, 01:41 PM
pro-toe-typing?

oh you mean like, declaring the sub as mprint($;@) or something? for right now, doing that in perl is bloody well useless from what i've seen.

or do you mean something else?

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

TheLinuxDuck
01-19-2001, 03:02 PM
Originally posted by YaRness:
oh you mean like, declaring the sub as mprint($;@) or something? for right now, doing that in perl is bloody well useless from what i've seen. or do you mean something else?


That's exactly what I mean.. I don't find it useless at all.. in fact, since I came from a C background, I hated the way perl handles passing parameters into a sub. I kinda like the flexibility now, but I also like the prototyping, because it forces you to code better, and to use the functions as they were meant (which makes for less error checking in the sub) and also lets you call the sub without parantheses, which can be nice.

I thought prototyping was one of the qool things of perl.. kinda like using -w and use strict, in the way that it lets you make the code have to work a certain way..(IMHO). http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

YaRness
01-19-2001, 03:39 PM
Originally posted by TheLinuxDuck:
That's exactly what I mean.. I don't find it useless at all.. in fact, since I came from a C background, I hated the way perl handles passing parameters into a sub. I kinda like the flexibility now, but I also like the prototyping, because it forces you to code better, and to use the functions as they were meant (which makes for less error checking in the sub) and also lets you call the sub without parantheses, which can be nice.

I thought prototyping was one of the qool things of perl.. kinda like using -w and use strict, in the way that it lets you make the code have to work a certain way..(IMHO). http://www.linuxnewbie.org/ubb/smile.gif


by useless i meant it's just too easy to work around, hardly worth bothering doing (all my coding in college was ADA and c++, so i know what you are talkin about, just the terminology escapes me on a daily basis).

i think for that mprint, a ($;@) would prolly suffice, if you set it to default to STDOUT like a regular print does (which would probably be a good thing to implement, one way or another). but i could be wrong. maybe, umm, maybe could figure out how to make it behave like print.. so "mprint;" would just act on the current filehandle and $_. hum hmmmm.

i wanna start building a "Myusefulstuff.pm" module sometime, i'm sure it will be half TLD code http://www.linuxnewbie.org/ubb/biggrin.gif (i already have a "tld.pl")

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

TheLinuxDuck
01-19-2001, 04:37 PM
Originally posted by YaRness:
maybe, umm, maybe could figure out how to make it behave like print.. so "mprint;" would just act on the current filehandle and $_. hum hmmmm.

That would be pretty qool, except if they wanted to do that, they could just use print.. http://www.linuxnewbie.org/ubb/wink.gif

i wanna start building a "Myusefulstuff.pm" module sometime, i'm sure it will be half TLD code http://www.linuxnewbie.org/ubb/biggrin.gif (i already have a "tld.pl")

LOL!!! I've got a folder in my perl devel dir called YaR, as well.. http://www.linuxnewbie.org/ubb/wink.gif kinda funny.. actually, when I come across someone here who is having a problem with perl or C, I create a dir of their name in my C/perl devel dirs, and put the code there.. that way I can go back and know exactly who it was for. http://www.linuxnewbie.org/ubb/smile.gif

If you get the usefulstuff.pm built, let me know, and maybe I can yank a copy of it. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

TheLinuxDuck
01-19-2001, 04:54 PM
Ok, I edited the sub to allow for blank calls, to utilize $_ (you had already added support for STDOUT). I also added the and next to each item of the parse loop. http://www.linuxnewbie.org/ubb/smile.gif


$_="Testing testing one two three";
mprint();

sub mprint
{
my(@params) = @_;
my(@globs,@scalars);

{
local $_;
for(@params) {
push(@globs, $_) and next if(ref(\$_) eq "GLOB");
push(@scalars, $_) and next if(ref(\$_) eq "SCALAR");
push(@params, @$_) and next if(ref($_) eq "ARRAY");
push(@params, $$_) and next if(ref($_) eq "SCALAR");
}
}

push(@globs, *STDOUT) if(not @globs);
push(@scalars, $_) if(not @scalars);

for(@globs) {
for my $data(@scalars) {
print $_ $data;
}
}
}


http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

YaRness
01-19-2001, 07:02 PM
wickedcool.

i'd prolly change the "if(not"s to "unless" just to make it prettier.

i'll think more about making it better, if it needs it, later. i wanna play with setting stuff up tonight.

http://www.linuxnewbie.org/ubb/cool.gif perl http://www.linuxnewbie.org/ubb/cool.gif

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

TheLinuxDuck
01-19-2001, 08:55 PM
Originally posted by YaRness:
i'd prolly change the "if(not"s to "unless" just to make it prettier.
i'll think more about making it better, if it needs it, later. i wanna play with setting stuff up tonight.

Post whatever changes you make, or anything else you're working on.. http://www.linuxnewbie.org/ubb/wink.gif this has been tres qool!!! http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

YaRness
01-19-2001, 09:22 PM
i'll prolly start on some web programmign stuff soon, so that should lead to new 1337 shiznit to post

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

Sterling
01-23-2001, 10:52 PM
Just reading through the Camel book, and something I saw reminded me of this thread. What you want to do could probably be done with a tied Filehandle. Look up tying somewhere, esp. tying filehandles. The example given (writing to STDOUT/STDERR and doing other stuff with the output) sounded like pretty much what you wanted...

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."

YaRness
01-24-2001, 09:23 AM
i thought the tie function had to do with packages/classes. i'm trying to find some more info on it though. if you have an example in your book of what i was trying to do, or something very close to it, i (and i'm sure others) would definitely appreciate you posting it when you can.

i'll holler if i find anything as well.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

Sterling
01-24-2001, 03:38 PM
Tying is like really, really advanced operator overloading. It basically lets you define a class that looks just like a primitive type, but does other stuff under the hood. I'm afraid most of the examples are far too long to paste - try checking the official Perl website or the O'Reilly Perl Programming webiste. One or both should have some samples.

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."

YaRness
01-24-2001, 03:59 PM
yeah, i'm looking at the stuff on perl.com. it's ugly. one of the simpler solutions above is definitely better for the purposes i stated.

i wouldn't mind learning how to use that, but i'd look for simpler examples first.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

kel
01-24-2001, 07:00 PM
It sounds like you just want a beefy script. You could use the 'FileHandle.pm' module like so;

#!/usr/bin/perl

use FileHandle;

$fh0 = new FileHandle;
$fh0->open( "filename1", "w" );

$fh1 = new FileHandle;
$fh1->open( "filename2", "w" );

foreach $line ( 0 .. $#array ) {
print $fh0 $_, "\n";
print $fh1 $_, "\n";
}


Or you could use the 'r+' argument to open another file for reading to get you information to output.

use FileHandle;

$fh0 = new FileHandle;
$fh0->open( "sourcefile", r+" );

while ( <$fh0> ) {
# code
# code
}

YaRness
01-24-2001, 09:32 PM
Originally posted by TheLinuxDuck:
LOL!!! I've got a folder in my perl devel dir called YaR, as well.. http://www.linuxnewbie.org/ubb/wink.gif kinda funny.. actually, when I come across someone here who is having a problem with perl or C, I create a dir of their name in my C/perl devel dirs, and put the code there.. that way I can go back and know exactly who it was for. http://www.linuxnewbie.org/ubb/smile.gif
damn that's a good idea. i've either been naming stuff by what it does.... and i've got a tld.pl, and a tld2.pl now, but that makes much more sense.

believe me, if i get around to putting together useful stuff, everyone gets a copy. i'll call it "Lno.pm" maybe.

kel: i'll look at that package. if it can't do what i want straight up, maybe we can put together a child package that can.


------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

YaRness
01-24-2001, 09:34 PM
well CRAP someone beat us to it: http://www.perl.com/CPAN-local//modules/by-category/21_File_Handle_Input_Output/FileHandle/

check out the multi thingie in there.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

Sterling
01-25-2001, 11:10 AM
That was the next thing I was going to ask, really! "Have you checked on CPAN yet?" :P Oh, well.

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."