0

Here is the code that I currently have. Running on Perl v5.8.9.

#!/usr/bin/perl
# Prompts the user for their desired configuration
print "\n";
print "Please type BB for breadboard, or ACU for the Audio Control Unit.\n";
print "Do you want a BB or ACU version of alarm programmer: ";
$a = <STDIN>;
if($a == "ACU"){
    print("Initiating alarm programmer for ACU...\n");
}else{
    print("Initiating alarm programmer for breadboard...\n");
}

Here is some sample output:

C:\Users\U157280\Documents\Alarm Programmer>PerlAutonomy.pl

Please type BB for breadboard, or ACU for the Audio Control Unit.
Do you want a BB or ACU version of alarm programmer: ACU

Initiating alarm programmer for ACU...

And another one:

C:\Users\U157280\Documents\Alarm Programmer>PerlAutonomy.pl

Please type BB for breadboard, or ACU for the Audio Control Unit.
Do you want a BB or ACU version of alarm programmer: BB

Initiating alarm programmer for ACU...

As you can see, it selects ACU for both even though the statements appear to be correct.

What am I doing wrong?

Dada
  • 6,313
  • 7
  • 24
  • 43
yishud
  • 31
  • 4
  • Any help would be greatly appreciated! – yishud Oct 09 '21 at 04:46
  • Note: your perl version is quite old (2007), update at first opportunity to most recent version (5.34.0), [Perl version history](https://en.wikipedia.org/wiki/Perl_5_version_history) – Polar Bear Oct 09 '21 at 06:03
  • 1
    If you interested in learning [Perl](https://www.perl.org/) then you will appreciate following free book [Modern Perl](http://modernperlbooks.com/) available online. Extensive [documentation](https://www.perl.org/docs.html) is at your disposal. – Polar Bear Oct 09 '21 at 06:23
  • 2
    In Perl, don't use `$a` and `$b` for variables in your program because they are special variables used by comparison routines passed to `sort` and the usual `strict` rules do not apply to them. – Sinan Ünür Oct 09 '21 at 12:19

2 Answers2

3

Following code snippet is an OP's altered code with minimal corrections.

Please include into your scripts use strict; and use warnings, it will warn you about potential issues with your code -- in long run it will save you a lot of trouble with code correction.

You should use string comparison operator eq instead of number comparison operator ==

#!/usr/bin/perl
#

use strict;
use warnings;
use feature 'say';

# Prompts the user for their desired configuration
say  '
   Available options:

       BB  -- for breadboard
       ACU -- for the Audio Control Unit

';

print '   Please make a choice: ';

my $choice = <>;

chomp($choice);

if( $choice eq 'ACU') {
        say "\n  Initiating alarm programmer for ACU...";
} elsif( $choice eq 'BB' ) {
        say "\n  Initiating alarm programmer for breadboard...";
} else {
        say "\n  Wrong choice...";
}

exit 0;

To run external command or script you should use system


Reference:


Polar Bear
  • 6,762
  • 1
  • 5
  • 12
  • Thank you, I think I was thinking Python with the ==. Greatly appreciated. Do you know how to call a Perl script from another Perl script? – yishud Oct 09 '21 at 16:47
  • As I already indicated in my answer to call perl script you should use [system](https://perldoc.perl.org/functions/system) call. – Polar Bear Oct 09 '21 at 17:58
1

Try this:

#!/usr/bin/perl
# Prompts the user for their desired configuration
print "\n";
print "Please type BB for breadboard, or ACU for the Audio Control Unit.\n";
print "Do you want a BB or ACU version of alarm programmer: ";
$a = <STDIN>;
chomp($a);
if($a eq "ACU"){
    print("Initiating alarm programmer for ACU...\n");
}else{
    print("Initiating alarm programmer for breadboard...\n");
}

You had 2 issues, first, = is assignment not comparison. Second, you had a newline at the end of your input that you weren't accounting for. The chomp() gets rid of the newline, and the eq operator performs string comparison.

Dada
  • 6,313
  • 7
  • 24
  • 43
Eric McKeeth
  • 347
  • 5
  • 11