sub get_user_input
{
if ($#_ < 0)
{
die "get_user_input: needs parameters, stopped";
}
my @values;
foreach (@_)
{
print;
my $input = <STDIN>;
$input =~ tr/0-9a-zA-Z//cd; #this can be whatever of course, or just "tr/\n//;" DOES need to remove the newlines however
push @values, $input;
}
if ($#_ == 0)
{
return pop @values;
}
else
{
return @values;
}
}
so you can do either of these
my $input = get_user_input("Enter my input: ");
my ($input1, $input2, $input3) = get_user_input("Enter my first input: ","Enter my second input: ","Enter my third input: ");
thought that was kinda neat, just thought i'd share.
------------------
"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 10 January 2001).]
jemfinch
01-10-2001, 02:46 PM
Functions shouldn't die when they're misprogrammed (ie, when the user of your code doesn't give a parameter to the function). You should have a small comment that describes how to use the function, but the function definitely shouldn't die.
Jeremy
YaRness
01-10-2001, 02:47 PM
whoops. left that in there while i was playing with how it works. i'll change it to a print STDERR and exit
<edit> err, maybe not an exit. stop or last or break or something. or maybe just a null return.
<edit2> actually, a few minutes ago i may have been considering trying out the eval thingy, so i may use die after all
------------------
"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 10 January 2001).]
[This message has been edited by YaRness (edited 10 January 2001).]
jemfinch
01-10-2001, 02:59 PM
Functions are used for the programmer. The programmer using your code knows better than you what should be done, error-wise.
You provide the documentation for the usage of your function: the programmer using your code provides the brains.
Granted, since perl's nasty way of raising exceptions is to die, you may leave the die in there and consider it to be raising an exception; that's your choice. But you definitely shouldn't handle the die yourself (by putting it in an eval loop).
Jeremy
YaRness
01-10-2001, 03:05 PM
lucky for me, i'm the only programmer at the moment who's using this. i was going to play with die/eval stuff as more of an exercise than anything else, though i don't think it'd be all that interesting.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
TheLinuxDuck
01-10-2001, 04:17 PM
YaR:
Have you ever looked into prototypes? Using prototypes (much like defining paramters for C) determines what can and can't be passed into the function. Here's the same code using a prototype. It says that it must receive a single scalar, at least.. or it can receive an unlimited amount of scalars (or arrays).
Very handy.. it won't even compile if not used right.
# Uncomment this next line to receive a compilation error
#get_user_input();
sub get_user_input($;@)
{
my(@values);
foreach(@_) {
print;
my $input=<STDIN>;
$input=~ tr/0-9a-zA-Z//cd;
push @values, $input;
}
return @values;
}
------------------
TheLinuxDuck
I have a belly button.
:wq
YaRness
01-10-2001, 04:26 PM
Originally posted by TheLinuxDuck:
YaR:
Have you ever looked into prototypes? Using prototypes (much like defining paramters for C) determines what can and can't be passed into the function. Here's the same code using a prototype. It says that it must receive a single scalar, at least.. or it can receive an unlimited amount of scalars (or arrays).
Very handy.. it won't even compile if not used right.
i was trying to figure out how to do that earlier, i musta either over looked using that semicolon, or not read that part, or something. that rocks TLD, thanks.
<edit> i also changed the tr// to tr/\n//d;. i figure let the user of the function be responsible for anything else
------------------
"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 10 January 2001).]
jemfinch
01-10-2001, 04:26 PM
TLD: Is that a new feature of 5.6.0?
Jeremy
TheLinuxDuck
01-10-2001, 04:46 PM
Originally posted by YaRness:
i was trying to figure out how to do that earlier, i musta either over looked using that semicolon, or not read that part, or something. that rocks TLD, thanks.
It took me a minute to find it in the PP book. I remembered seeing it, but wasn't quite sure how to go about using it either.. I don't care what anyone says about PP, I think it's the best book on perl a programmer can buy.
<edit> i also changed the tr// to tr/\n//d;. i figure let the user of the function be responsible for anything else
http://www.linuxnewbie.org/ubb/smile.gif Are there any benefits to using tr/\n//d over chomp? Just curious if one performs any quicker or anything. I've always just used chomp (for removing newlines)
Btw, since we used a prototype for the function, you don't need the parentheses around the parameters.
------------------
TheLinuxDuck
I have a belly button.
:wq
TheLinuxDuck
01-10-2001, 04:50 PM
Originally posted by jemfinch:
TLD: Is that a new feature of 5.6.0?
To be honest with you, I'm not sure. I am running 5.6.0, but haven't ever used it with any previous versions. I thought I remembered seeing it in PP 2nd edition also, but I could be mistaken.
I believe it was takshaka (where is he, btw?) that I was talking to about passing parameters.. I didn't like the fact that perl didn't have any kind of parameter definition. It seemed too open... but the prototypes certain help.
Perl is phun!
------------------
TheLinuxDuck
I have a belly button.
:wq
YaRness
01-10-2001, 05:13 PM
Originally posted by TheLinuxDuck:
http://www.linuxnewbie.org/ubb/smile.gif Are there any benefits to using tr/\n//d over chomp? Just curious if one performs any quicker or anything. I've always just used chomp (for removing newlines)
Btw, since we used a prototype for the function, you don't need the parentheses around the parameters.
parentheses... erg? if you just mean i can do '($yar1,$yar2) = get_user_input "yar","yar"', then that's cool... i'm so used to using parantheses though i'd prolly never notice.
chomp works just as fine as far as i know, i'm just used to doing tr/\n// now from doing some web page work that involved parsing some weird stuff that had newlines all over the place (i think it was the HTML::TableExtract module that uses newlines for the seperator for extracted cells or something, plus extra newlines in the actual html cuz the page was built with FrontPage.. i dunno). TMTOWTDI.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
TheLinuxDuck
01-10-2001, 05:57 PM
Originally posted by YaRness:
parentheses... erg? if you just mean i can do '($yar1,$yar2) = get_user_input "yar","yar"', then
that's cool... i'm so used to using parantheses though i'd prolly never notice.
I know that you prefer to use parentheses. You've said, in not so
many words, that you'd prefer to overdo it than to not have enough.
I don't blame you, either. http://www.linuxnewbie.org/ubb/smile.gif
chomp works just as fine as far as i know, i'm just used
to doing tr/\n// now from doing some web page work that involved parsing
Qool.. just curious.. that's all.. http://www.linuxnewbie.org/ubb/wink.gif The tr/// is nice because
it doesn't parse regexps or interpolate. Nuttin like having options, eh?
All I gotta say is... perl.
------------------
TheLinuxDuck
I have a belly button.
:wq
jemfinch
01-10-2001, 07:35 PM
Originally posted by TheLinuxDuck:
Are there any benefits to using tr/\n//d over chomp?
On the contrary, chomp is a far better way to do it.
if tr/\n//d was in the function, then the function assumes that $/ is set to '\n' (which is the default, but is by no means the only thing used.) Say, for instance, you wanted the user to input a uncertain amount of data, and end input with a dot on a line by itself (you might recognize that from SMTP). You'd set $/ to '\n.\n' and then do an "$foo = <STDIN>". The angle operators would read until they saw $/, or '\n.\n'. If you then chomp'ed the string, it would rip off the final '\n.\n', leaving the rest of the string intact. tr/\n//d, on the other hand, would remove all newlines, which isn't what you want.
Chomp is the better choice.
YaRness
01-10-2001, 08:17 PM
the chomp is definitely the more context-independent solution here.
i think if i were to implement this in a generic package, i would probably leave the trimming, chomp or otherwise, up to the user (or child package).
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
TheLinuxDuck
01-11-2001, 05:46 PM
Originally posted by YaRness:
the chomp is definitely the more context-independent solution here.
You were needing something, though, that removed newlines from everywhere in the scalar, though, not just at the end, which is why you were using tr///, correct?
I don't think I've hardly used tr/// for anything.. I tend to use s/// mostly. Cuz I'm a super freak. Super freak. Super freaky.
I've been spending the last few days really trying to get to know java..
I can't say that I'm as hooked as I was when I first got into perl.. perl really just pulled me right in, and showed me the light.. java is showing me various shades of the light, very slowly. http://www.linuxnewbie.org/ubb/smile.gif
It's not too bad.. I actually can look at a precompiled java source and basicaly get what's going on, minus all this swing stuff.. I want to digest the language first before delving into graphics, although the graphics implementation doesn't appear to be hard. http://www.linuxnewbie.org/ubb/smile.gif
Anyhow, look what Dru's got me started on!! (I think my head may explode)
------------------
TheLinuxDuck
I have a belly button.
:wq
jemfinch
01-12-2001, 01:41 AM
Originally posted by TheLinuxDuck:
I don't think I've hardly used tr/// for anything.. I tend to use s/// mostly. Cuz I'm a super freak. Super freak. Super freaky.
For what it does, tr/// is much faster than s///.
Far faster.
Jeremy
YaRness
01-12-2001, 09:19 AM
Originally posted by TheLinuxDuck:
You were needing something, though, that removed newlines from everywhere in the scalar, though, not just at the end, which is why you were using tr///, correct?
for ANOTHER project (web stuff i'm doing for someone else), i was needing to remove new lines all throughout a string (the string had a leading null thingy or other, a newline, text, two newlines, more text, and at least one trailing newline. it was pretty ugly). using that is what got tr/\n//d stuck in my head.
java looks scary. i don't like anything where.functions.look.like.this(), plus i haven't really done any C in a year and a half or longer. i prolly should work on some sometime just in case i ever need to know how to do c again.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
TheLinuxDuck
01-12-2001, 10:48 AM
Originally posted by jemfinch:
For what it does, tr/// is much faster than s///. Far faster.
I suppose that shouldn't surprise me. http://www.linuxnewbie.org/ubb/smile.gif
It actually took me a while to understand the
differences between s/// and tr///.
TheLinuxDuck
01-12-2001, 10:56 AM
Originally posted by YaRness:
java looks scary. i don't like anything
where.functions.look.like.this(),
I'm with you on that. It's hard for me to
get used to printing by typing
System.out.println() everytime instead
of simply print. The big thing I am
having trouble with is knowing what functions
are available with what classes. I can't
seem to find that kind of information in the
docs I d/l (even though I've been told they
are there).
I went and bought 'Java Examples in a
Nutshell' and have been reall enjoying it. It
doesn't have all the explanations (that I
prolly need) but, it gives great examples and
shows me how to use a lot of functions and
such I didn't know about or how to use.
Java is scary, though.. my C background
hasn't prepared me well for this. http://www.linuxnewbie.org/ubb/smile.gif
plus i haven't really done any C
in a year and a half or longer. i prolly
should work on some sometime just in case i
ever need to know how to do c
again.
I went like 4 or 5 years without touching C
and then got back into it when I began
getting into linux.. it's amazing how quickly
it came back. I sure like C. Even though
perl is much easier, C sure has a special
place in my heart (as much as any programming
language can).
The way I look at programming right now,
the more languages that I know decently, the
better chance I have of getting a good job.
Even though I don't know java right now, in
about 6 months, hopefully, I'll have a much
better understanding. Then, I can look for
work with C, perl, and java. Definitely
opens more doors, I think.
But, that certainly doesn't mean I'm any
good at any one of them.. LOL!! http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.