What a pleasant thread we've got here. ) Suppose a bit of my ranting won't
spoil it much, will it? ))

2012/2/21 Vyacheslav <agapov.sl...@gmail.com>


> I'm new in perl and have many questions.
>
And there's a place to ask them, believe me. )


> This my first programm.
>
Going straight to the point, I see... Good.

But it'll be even better to start with describing your environment a bit
(version or Perl and platform will surely suffice), and defining your task
as clearly as possible.
In fact, sometimes writing down your task will leave you out of our
pleasant company... )

Let's assume you're working with some kind of built-in Perl, version 5.8,
and your task is as follows: 1) ask your user to enter an integer number,
2) check the input, 3) and print it back only if user followed your rules
and actually entered an integer.

It looks like an easy task - and actually is easy to solve in Perl. )


> my $number = 0;

my $_ = 0;

print "Enter number:";

chomp($number = <>);


Here's a part of your code that solves the first sub-task.
Does it work? Mostly, yes. Can it be improved? Definitely.

First, you don't have to initialize variables in Perl, especially if these
variables are used to store the user input. In fact, it's common to declare
them at the same line where input is collected.

Second, it's a little... optimistic, may I say it, to name an unchecked
variable by its supposed type. ) Doesn't it sound weird to you when you
need to check whether the $number is actually a number, after all? ))

Third, $_ is just that - _special_ Perl variable, waiting there for you at
the first line of Perl code, ready and willing. You don't need to declare
it explicitly - may I say it, EVER. )

So the block we're talking about may be happily rewritten to just:

print "Enter a number: ";
chomp (my $user_input = <STDIN>);

By the way, did you notice the change? ) Only if no arguments were passed
to your program <> operator will try to read from STDIN (standard input
stream). But you happen to ask your user for input exactly (remember
'print'?), so I guess STDIN should be processed in every case.

if ( $number = /[0-9]/) ...
>
> Well, as my colleagues pointed out, what Perl actually sees here is ...if
($number = $_ =~ /[0-9]/)... Perhaps that's why you've tried to initialize
the $_, to get rid of that nasty warning? ) If that's the case, you tried
the wrong remedy: the syntax of checking has to be fixed, not some
declarations.

But here we come back to the first step: unless you define your task
clearly, you won't know what needs to be checked. What is the number YOU
were looking for, after all? ) 3.14 - is it 'number enough' for you? How
about -2.7e10? )

Anyway, if you're looking for integers only, as assumed previously, the
corresponding check should be made of this:
*match the beginning of the line marker, then, optionally, a minus sign,
then any number of digits, then the end of the line marker*, or just
/^-?\d+$/

Finally, about printing the result. It's a matter of taste, but I like to
separate "\n" symbol from the rest of the line, like this:

print "Your number is $user_input", "\n";

...or (preferably) just to use 'say' built-in from Perl 5.10, like this:

say "You actually entered number $user_input, bless you!";

... as both solutions make the useful content stand more clearly.

#########################
TL;DR: the core of your program may be rewritten as...

print 'Please, enter an integer number, as I really need it: ';
chomp (my $user_input = <STDIN>);
if ($user_input =~ /^-?\d+$/) {
  print "My hero! You've actually entered <$user_input>, which is an
integer! I salute you!", "\n";
}
else {
  print "How dare you insult me with pathetic <$user_input>!?", "\n";
}

Hope this'll be helpful. )

-- iD

Reply via email to