user6032623
user6032623

Reputation:

Perl else error

Hello I am new to programming in perl I am trying to make a number adder (math) but it gives me 1 error here's my code:

sub main {
    print("First: ");
    $num1 = <STDIN>;
    print("Second: ");
    $num2 = <STDIN>;

    $answer = $num1 + $num2;
    print("$answer")
} else {
    print("You have entered invalid arguments.")
}
main;

now obviously its not done but I get an error on ONLY else here is the error:

C:\Users\aries\Desktop>"Second perl.pl"
syntax error at C:\Users\aries\Desktop\Second perl.pl line 9, near "} else"
Execution of C:\Users\aries\Desktop\Second perl.pl aborted due to compilation errors.

please help (also I tried googling stuff still error)

Upvotes: 0

Views: 422

Answers (1)

interduo
interduo

Reputation: 401

Since you're new to Perl, I recommend you add strict and warnings at the top of your scripts. This will help identify common problems and potentially dangerous code.

The main problem with your code is that you've appended the else statement to your subroutine. Here is an example of your code as I think you intended it to be:

use strict;
use warnings;

use Scalar::Util qw(looks_like_number);

sub main {

    print 'First :'; 
    my $num1 = <STDIN>;

    print 'Second: ';
    my $num2 = <STDIN>;

    if( looks_like_number($num1) && looks_like_number($num2) ) {

        my $answer = $num1 + $num2;
        print "$answer\n";
    }
    else {
        die 'You have entered invalid arguments.';
    }
}

main();

There are a few things I should note here about the differences between Perl and Python that I think will help you understand Perl better.

  1. Unlike Python, Perl doesn't care about white space. It uses curly braces and semi-colons to indicate the end of blocks and statements and will happily ignore any white space that isn't in quotes.

  2. The else statement appended to the subroutine won't work because Perl isn't designed to evaluate code blocks that way. A subroutine is simply a named code block that can be called at any other point in the script. Any error handling will need to be done inside of the subroutine rather than to it.

  3. Perl is very context-sensitive and doesn't make a solid distinction between strings and integers in variables. If you write $var_a = 1, Perl will read it as an integer. $var_b = '1', it will read it as a string. But, you can still add them together: $var_c = ($var_a + $var_b), and Perl will make $var_c = 2.

    This is another reason the else statement would not work as you've written it. Python would throw an error if you try to add non-integers, but Perl will just figure out how to combine them, and give you the result. If you try to add a letter and a number, Perl still won't fail, but it will warn you if you put "use warnings;" at the top of your script.

    In the example, and as Dada mentioned, you can use the looks_like_number() method from the Scalar::Utils module to evaluate the variables as you had intended.

  4. Apart from the syntax, if/else statements work the same way in Perl as in Python. The else-if is slightly different as is has an extra s:

    if (condition) { ... } elsif (other condition) { ... } else { ... }

  5. In Perl, it's good practice to assign lexical scope to variables using the my function. Since Perl is so context-sensitive, this can help prevent unexpected behavior when moving between different scopes.

  6. Single- and double-quotes have different uses in Perl. Single-quotes are read literally and double-quotes are interpolated, so if you want to combine variables and text together, you can skip concatenating the strings and just do: print "Got variable: $var\n";

  7. Lastly, note that I added parentheses after the main subroutine call. This is another best practice to make it clearer that you are calling a subroutine as opposed to it being a bare word or a bad variable name.

Upvotes: 1

Related Questions