Since I assume you are new to Perl and to programming,
permit me to give you some general advice:
- Take care with your indenting
- In addition to "use warnings", add "use strict"
Your code could be written more simply like this:
use strict;
use warnings;
print <<'JONAG';
Do you want to print the program's
1. default strings in reverse order
2. or enter your own to be reversed ?
Choose option 1 or 2 or q to quit
JONAG
my $choice = <STDIN>;
chomp $choice;
if ($choice eq 'q') {
print "quitting\n";
exit;
}
# This is the default value.
my @lines = qw( wrda vzc rregr zfgr bzfgzffg rergrge d fgsrg rwer ds v
+sgsrg );
if ($choice == 2) {
print STDERR "enter your own strings\n";
chomp(@lines = <STDIN>);
}
foreach my $line (reverse @lines) {
print "\t$line\n";
}
First notice that I replaced:
print "do you want to print the program's\n", "1. default strings in r
+everse order\n", "2. or enter your own to be reversed ?\n";
with:
print <<'JONAG';
Do you want to print the program's
1. default strings in reverse order
2. or enter your own to be reversed ?
Choose option 1 or 2 or q to quit
JONAG
which is easier to read and maintain.
This uses a feature of Perl called "here-documents".
See perlop (search for << in the "Quote-Like Operators" section).
I added the lines:
chomp $choice;
if ($choice eq 'q') {
print "quitting\n";
exit;
}
The chomp is to remove the newline before checking whether $choice
is equal to 'q' or not.
Note that I used the "alphabetic" operator eq rather than the
numeric equivalent ==.
In Perl, the "alphabetic" version of an operator (in this case eq)
compares the two operands
as if they are strings, while the "punctuation" version (in this case ==)
compares them numerically.
For example, "01" eq "1" is false because the strings are of different
length,
while "01" == "1" is true because, numerically, they both evaluate to one.
See perlop ("Equality Operators" section) for more information.
I set the default value via:
my @lines = qw( wrda vzc rregr zfgr bzfgzffg rergrge d fgsrg rwer ds v
+sgsrg );
Note the "my" variable declaration, which is required by "use strict".
Variables declared with "my", known as "lexical variables", are known
from the point of declaration to end of scope.
This protects you from many common booboos,
such as mis-spelling a variable name,
and generally makes the code easier to understand and maintain
by explicitly limiting variable scope.
See strict for details.
Finally, I reorganized the code to remove unnecessary code duplication
(see DRY).
That is, the "user entered" data just sets the @lines array. Nothing
more.
That guarantees that the identical code is used for both the default case
and the user-entered case.
Your original code had slightly different code for the two cases.
Also, your code was needlessly altering the array.
For example, instead of your:
foreach $line (@proglines) {
$line = "\t$line";
$line .= "\n";
}
print "@proglines";
which needlessly changes the @proglines array, you can simply
print the formatted lines without changing the @proglines array like so:
foreach my $line (@proglines) {
print "\t$line\n";
}
or even:
print map { "\t$_\n" } @proglines;
if you want to get fancy.
Update: as for why you should use strict, I found these nodes:
|