User InputHaving a program that displays the exact same message
every time you run is nice when it comes to being
consistent, but not so entertaining as a program.
"What does it do?" "It prints out my
name." "Oh." Let's make things a little
more interesting. We could change the value of
It's so neat that I'm going to show you two ways to do it. TraditionalHere's how I got user input before Perl 5.10. You'll find a lot of code that looks like this.
Why don't we run this before we start trying to decipher it?
There is a lot going on in the few lines we added. Let's look at them one by one.
We've got some funny-looking thing instead of "Brian". What is it? Here's the short explanation:
There's also a long version. Feel free to skip it if you just want to get on with it. The long version works out like this:
There's a really long version, but I'm getting bored so we'll skip it.
You can see that you're going to use
A new wayThe new way looks better to my eyes, but I'm willing to admit that there's a lot going on for a beginner. You can feel free to ignore this section if you like, and my feelings won't be hurt at all.
The program is doing nearly the same thing as before,
but the code looks a lot different. We Looking at the changes
These lines tell Perl to load a couple of modules for
some additional functionality. This is different from when
we called Why do we want those modules? So we can create an object that reads user input.
There are a lot of things to learn in these two lines of code, so please forgive me if I rush through them too quickly. What's an object? Oh boy. That is a tricky question for a beginner tutorial. It's so tricky that I've decided to skip it completely. Almost completely. Objects are basically magic scalar variables that hold extra information such as data (the fields) and subroutines (the methods). Intelligent use of objects allow you to quickly write powerful programs while hiding the complexity of what's going on behind a sweet and smiling face. Classes define the structure of a particular
type of object. You create an object with a
constructor method defined in the class. The
constructor is usually called The last source of confusion in this new example is the
way we asked I'll get more into dictionaries later, but that's the basic idea for now. Wait, one more thing. It's helpful to think of those special keyword lookup collections as dictionaries, but they're really called hashes. Enough babbling. Let's run it. $ perl hello.pl What is your name? Brian Hello, Brian! Nice, it adds some formatting to the process! Well, it does for me. You might not get the formatting if your console doesn't support it. We only added a few lines to our program, but it made a significant difference in the end. Maybe you don't understand what is going on behind those changes. That's okay. Perl is a strange language, full of things that are very easy and fairly challenging at the same time. Give yourself time to learn the language and explore its features. Definitely explore the available libraries, because you will be amazed by how much you can improve your programs. And I don't forget to explore CPAN, the gigantic repository of libraries for Perl. |
![]() |
|
|
Copyright 1999 - 2009 Brian Wisti
|
||