<?xml version="1.0" encoding="windows-1252"?>
<node id="1004391" title="Re: elsif statement not being evaluated" created="2012-11-18 06:09:57" updated="2012-11-18 06:09:57">
<type id="11">
note</type>
<author id="176576">
eyepopslikeamosquito</author>
<data>
<field name="doctext">
&lt;P&gt;
Since I assume you are new to Perl and to programming,
permit me to give you some general advice:
 &lt;ul&gt;
  &lt;li&gt; Take care with your indenting
  &lt;li&gt; In addition to "use warnings", add "use strict"
 &lt;/ul&gt;
&lt;/P&gt;

&lt;P&gt;
Your code could be written more simply like this:
&lt;CODE&gt;
use strict;
use warnings;

print &lt;&lt;'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 = &lt;STDIN&gt;;
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 vsgsrg );

if ($choice == 2) {
    print STDERR "enter your own strings\n";
    chomp(@lines = &lt;STDIN&gt;);
}

foreach my $line (reverse @lines) {
    print "\t$line\n";
}
&lt;/CODE&gt;
&lt;/P&gt;

&lt;P&gt;
First notice that I replaced:
&lt;CODE&gt;
print "do you want to print the program's\n", "1. default strings in reverse order\n", "2. or enter your own to be reversed ?\n";
&lt;/CODE&gt;
with:
&lt;CODE&gt;
print &lt;&lt;'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
&lt;/CODE&gt;
which is easier to read and maintain.
This uses a feature of Perl called "here-documents".
See [doc://perlop] (search for &lt;C&gt;&lt;&lt;&lt;/C&gt; in the "Quote-Like Operators" section).
&lt;/P&gt;

&lt;P&gt;
I added the lines:
&lt;CODE&gt;
chomp $choice;
if ($choice eq 'q') {
    print "quitting\n";
    exit;
}
&lt;/CODE&gt;
The chomp is to remove the newline before checking whether &lt;C&gt;$choice&lt;/C&gt;
is equal to &lt;C&gt;'q'&lt;/C&gt; or not.
Note that I used the "alphabetic" operator &lt;C&gt;eq&lt;/C&gt; rather than the
numeric equivalent &lt;C&gt;==&lt;/C&gt;.
In Perl, the "alphabetic" version of an operator (in this case &lt;C&gt;eq&lt;/C&gt;)
compares the two operands
as if they are strings, while the "punctuation" version (in this case &lt;C&gt;==&lt;/C&gt;)
compares them numerically.
For example, &lt;C&gt;"01" eq "1"&lt;/C&gt; is false because the strings are of different
length,
while &lt;C&gt;"01" == "1"&lt;/C&gt; is true because, numerically, they both evaluate to one.
See [doc://perlop] ("Equality Operators" section) for more information.
&lt;/P&gt;

&lt;P&gt;
I set the default value via:
&lt;CODE&gt;
my @lines = qw( wrda vzc rregr zfgr bzfgzffg rergrge d fgsrg rwer ds vsgsrg );
&lt;/CODE&gt;
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 [doc://strict] for details.
&lt;/P&gt;

&lt;P&gt;
Finally, I reorganized the code to remove unnecessary code duplication
(see &lt;a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself"&gt;DRY&lt;/a&gt;).
That is, the "user entered" data just sets the &lt;C&gt;@lines&lt;/C&gt; 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:
&lt;CODE&gt;
foreach $line (@proglines) {
	$line = "\t$line";
	$line .= "\n";
}
print "@proglines";
&lt;/CODE&gt;
which needlessly changes the &lt;C&gt;@proglines&lt;/C&gt; array, you can simply
print the formatted lines without changing the &lt;C&gt;@proglines&lt;/C&gt; array like so:
&lt;CODE&gt;
foreach my $line (@proglines) {
    print "\t$line\n";
}
&lt;/CODE&gt;
or even:
&lt;CODE&gt;
print map { "\t$_\n" } @proglines;
&lt;/CODE&gt;
if you want to get fancy.
&lt;/P&gt;

&lt;P&gt;
&lt;B&gt;Update:&lt;/B&gt; as for why you should use strict, I found these nodes:
 &lt;ul&gt;
  &lt;li&gt; [id://87956] (earlier version: [id://87628])
  &lt;li&gt; [id://111088]
  &lt;li&gt; [id://952446]
 &lt;/ul&gt;
&lt;/P&gt;

&lt;P&gt;
 &lt;ul&gt;
  &lt;li&gt; [id://115515]
  &lt;li&gt; [id://405770]
  &lt;li&gt; [id://482733]
  &lt;li&gt; [id://37204]
  &lt;li&gt; [id://686571]
 &lt;/ul&gt;
&lt;/P&gt;</field>
<field name="root_node">
1004376</field>
<field name="parent_node">
1004376</field>
</data>
</node>
