Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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";
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:

In reply to Re: elsif statement not being evaluated by eyepopslikeamosquito
in thread elsif statement not being evaluated by jonagondos

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2018-06-18 04:56 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (108 votes). Check out past polls.