Click to See Complete Forum and Search --> : perl: TLD: mprint


YaRness
01-25-2001, 05:10 PM
i took out the "local $_", i think it was unnecessary. it works like it should (acting on @_ if there's no string included in the command, i even did "mprint(*STDERR, *STDOUT)" and it worked). take a look and see what you think.

(i also replaced the "if(not"s with "unless" like i said i would


sub mprint
{
#given a list of any combination of scalars, lists, references to lists,
#references to scalars, prints each scalar element to each file handle.
#possibly in something resembling the order in which they were received
my(@params) = @_;
my(@globs,@scalars);

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) unless (@globs);
push(@scalars, $_) unless (@scalars);
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/

jemfinch
01-25-2001, 10:43 PM
The "local $_" was there so the function didn't squash the $_ from the calling scope, which would be rude.

Jeremy

YaRness
01-26-2001, 09:40 AM
well, it works. BECAUSE, inside the "for (@params)" loop, $_ is locally scoped or something (which, when i think about it, makes a LOT of sense). at least it is on my machine. try this:

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

my @list = (1,2,3);
$_ = "FOO";

for (@list)
{
print $_, "\n";
}

print $_,"\n";

in the mprint sub, since nothing is done to change the incoming $_, it is still whole and fine when we get to the "push(@scalars, $_) unless (@scalars);" line.

which is pretty sweet.

i'm thinking of adding some stuff to sort and push key/element pairs from a hash into the scalars array, though most people would prolly wanna sort and format it themselves and push strings of pairs or whatever on.. but hey, i'm trying to please me, not anyone else.

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

TheLinuxDuck
01-26-2001, 01:10 PM
YaR:

Mighty fine work!!! I'm liking it! http://www.linuxnewbie.org/ubb/smile.gif I think adding some sort of hash thing would be qool too. An idea I had.. dunno how worthy an idea, but it's an idea.. maybe when you pass the hash into the program, the mprint sub will look for a hash key called 'sort'. If it finds it, it deletes it and then sorts the hash out as it pushes it onto @scalars. If it doesn't find it, it will act normally.. you could even go so far as to have a value in 'sort' as 'normal', 'reverse', 'alphabetical', or whatever.. this would give the user some control over the sort..

or am I making too complicated a thing out of it?

Just an idea.

jemfinch:

You thought exactly the same thing I did. I am very surprised that $_ maintains it's previous value after the for(), to be honest. The scoping issue seems to make sense, but I could have sworn that I've done something similar, and it trashed $_.. oh well.. if it works, qool. http://www.linuxnewbie.org/ubb/smile.gif

[ edit ]

After doing some research in 'PP3rd', I found this:

Pg 119:
"The loop variable is valid only from within the dynamic or lexical scope of the loop and will be implicitly lexical if the variable was previously declared with my. This renders it invisible to any function defined outside the lexical scope of the variable, even if called from within that loop. However, if no lexical declaration is in scope, the loop variable will be a localized (dynamically scoped) global variable; this allows functions called from within the loop to access that variable. In either case, any previous value the localized variable had before the loop will be restored automatically upon loop exit."

Hm... news to me, but there it is in b&w. http://www.linuxnewbie.org/ubb/smile.gif

Word to your skilz, YaR! http://www.linuxnewbie.org/ubb/smile.gif http://www.linuxnewbie.org/ubb/smile.gif


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

[This message has been edited by TheLinuxDuck (edited 26 January 2001).]

YaRness
01-26-2001, 01:30 PM
i think KISS (Keep It Simple, Stupid) applies here.

of course i'm not trying to be an ***. My idea was to include something for handling hashes just as a convenience. i figure if you don't care about how your hash is printed, you won't care if i sort it first. in reality, if you are printing out hash/key pairs, there are tons of ways you could do it: how to sort it, how to format the output ("Key=> Element" or "Key Element" or "Key, Element" or....). i figure, 90% of the time, you'd hafta pre-sort and format it anyway, and just dump the strings, or array of strings, into mprint anyway.

also, i really dont like the idea of determinig a subroutine's behaviour on the basis of the variable names.

now that i've got you against the wall ( http://www.linuxnewbie.org/ubb/biggrin.gif ), what we COULD do is think of a way to pass in formatting parameters for various things. maybe some optional first parameter that would be in a particular format, like "-s lexical -v 2 -h 3", stuff like that. i dunno.

or we could develop child packages that add on different functionality (Lno::Hash for instance). hmm hmm.

so, in conclusion, it's a good idea, i think it'd be suited for a more particular flavor of mprint though.

maybe we can work on breaking down the loop that sorts the tokens from the parameters into seperate subroutines (they could just be one line return thingies in the parent package), so if you make a child package, you can just modify a particular subroutine for hashes, or arrays, or whatever.

dude this is fun. i should have time to play over the weekend too, though i may be spending that time figuring out dpkg and apt and all that debian stuff.

i'm prolly going to be using the Lno package at work already! this is fun.

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

YaRness
01-26-2001, 01:34 PM
Originally posted by TheLinuxDuck:
Hm... news to me, but there it is in b&w. http://www.linuxnewbie.org/ubb/smile.gif

just seemed to make intuitive sense to me. i mean, it's just another block. but it's good to know it's doc'ed
Originally posted by TheLinuxDuck:
Word to your skilz, YaR! http://www.linuxnewbie.org/ubb/smile.gif http://www.linuxnewbie.org/ubb/smile.gif
sure thing home-slice. i just like to tinker http://www.linuxnewbie.org/ubb/eek.gif


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

TheLinuxDuck
01-26-2001, 05:52 PM
Originally posted by YaRness:
just seemed to make intuitive sense to me. i mean, it's just another block. but it's good to know it's doc'ed

I would have never thought of the loop block that way before, had you not said something about it. I just assumed (see what I get?) that any block would use the $_ the same, no matter what. I guess we learn something newd every day. http://www.linuxnewbie.org/ubb/smile.gif

sure thing home-slice. i just like to tinker http://www.linuxnewbie.org/ubb/eek.gif

You and me both. http://www.linuxnewbie.org/ubb/smile.gif perl has so much flexibility, it's fun to delve in and find all the juicy little tidbits. heck, if you and I keep up at this long enough, we're gonna know some serious perl! http://www.linuxnewbie.org/ubb/smile.gif http://www.linuxnewbie.org/ubb/smile.gif http://www.linuxnewbie.org/ubb/smile.gif

I kinda like the jumping back and forth and the flow of ideas that form into one shape.. pushing each other, if you will.. that's a great thing about this forum..

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