if statement - Perl else error -


hello new programming in perl trying make number adder (math) gives me 1 error here's 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 not done error on else here error:

c:\users\aries\desktop>"second perl.pl" syntax error @ c:\users\aries\desktop\second perl.pl line 9, near "} else" execution of c:\users\aries\desktop\second perl.pl aborted due compilation errors. 

please (also tried googling stuff still error)

since you're new perl, recommend add strict , warnings @ top of scripts. identify common problems , potentially dangerous code.

the main problem code you've appended else statement subroutine. here example of code think intended be:

use strict; use warnings;  use scalar::util qw(looks_like_number);  sub main {      print 'first :';      $num1 = <stdin>;      print 'second: ';     $num2 = <stdin>;      if( looks_like_number($num1) && looks_like_number($num2) ) {          $answer = $num1 + $num2;         print "$answer\n";     }     else {         die 'you have entered invalid arguments.';     } }  main(); 

there few things should note here differences between perl , python think understand perl better.

  1. unlike python, perl doesn't care white space. uses curly braces , semi-colons indicate end of blocks , statements , happily ignore white space isn't in quotes.

  2. the else statement appended subroutine won't work because perl isn't designed evaluate code blocks way. subroutine named code block can called @ other point in script. error handling need done inside of subroutine rather it.

  3. perl context-sensitive , doesn't make solid distinction between strings , integers in variables. if write $var_a = 1, perl read integer. $var_b = '1', read string. but, can still add them together: $var_c = ($var_a + $var_b), , perl make $var_c = 2.

    this reason else statement not work you've written it. python throw error if try add non-integers, perl figure out how combine them, , give result. if try add letter , number, perl still won't fail, warn if put "use warnings;" @ top of script.

    in example, , dada mentioned, can use looks_like_number() method scalar::utils module evaluate variables had intended.

  4. apart syntax, if/else statements work same way in perl in python. else-if different has s:

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

  5. in perl, it's practice assign lexical scope variables using my function. since perl context-sensitive, can prevent unexpected behavior when moving between different scopes.

  6. single- , double-quotes have different uses in perl. single-quotes read literally , double-quotes interpolated, if want combine variables , text together, can skip concatenating strings , do: print "got variable: $var\n";

  7. lastly, note added parentheses after main subroutine call. best practice make clearer calling subroutine opposed being bare word or bad variable name.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -