Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Please Help

by kcott (Chancellor)
on Sep 29, 2012 at 23:28 UTC ( #996436=note: print w/replies, xml ) Need Help??

in reply to Please Help

G'day cpom1,

Welcome to the monastery.

The following should be sufficient information for you to complete your class assignment.

The site should provide most, if not all, of the documentation you need while initially learning Perl. I have it bookmarked and use it often; I suggest you do the same. I've included a number of links to documentation below - all to pages on this site.

After the shebang line (that's the #!... on line 1) put:

use strict; use warnings;

When you do this, Perl will tell you about mistakes you've made or potentially problematic code you're attempting to run. While you're learning Perl, you should do this in all of your scripts. See strict and warnings.

The line read -p "What is your name: " name is not Perl: it works in bash (and probably other shells). The typical way to achieve what you want here is with something like:

print 'Prompt message: '; my $reply = <>; print $reply;

The first line should be self-explanatory - single-quotes are for literal text (see print).

In the next line my declares a lexical variable: Perl has various types of variables - you'll use this type most often and it's all you need for this type of script. Unlike bash, where you assign to varname then read from $varname, in Perl you use $varname in both cases. The <> reads from standard input (in your case, this is what you type at the keyboard). One important point to note is that it reads the text you type as well as the newline you enter to terminate your input. You can read about it in perlop - I/O Operators but you may find that rather heavy going: it certainly has far more information than you need for this script.

Finally, you print whatever the user typed, including the newline.

That handles getting the input you need. Note that there's no checking of this input: the user could have just typed a newline for name and something that isn't a number for income. I suspect that's not a requirement for your assignment: add a comment in your code stating that no validation is performed to indicate that you are aware of this limitation in your script. (If it is a requirement, come back and ask a follow-up question.)

All of your conditions use bash (or other shell) code again (-lt and -gt). See perlop - Relational Operators for the Perl equivalents - note that there's different operators for string and numerical comparisons.

That should be enough to get your script up and running. Here's a few additional links you may find useful: perl; perlsyn; perlstyle.

-- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996436]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-06-23 12:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.