prospect has asked for the wisdom of the Perl Monks concerning the following question:

So I am messing around with subroutines, I got this piece of code running last week but now when I return to it, it's giving me syntax error on line 18. I compared the sources I got it from and the syntax used in those cases and I just can't see it. I wanted to make a little exercise where the user inputs two values, 1 for $fred and 1 for $barney and it would say which value is bigger. But when I run it, it says there is a syntax error on line 18 and I cannot find it :/

sub combo { my $sum = $fred * $barney return $sum; if ($fred > $barney) { $fred; #also why do I have to put another $fred here? It doesn +#'t seem to be part of "if" nor of "else" } else { $barney; } } print "Enter value:"; $fred = <STDIN>; # chomp $fred; print "Enter value:"; $barney = <STDIN>; # chomp $barney;
  • Comment on Why am I getting "syntax error at Funcy.pl line 18, near "$barney return" ?
  • Download Code

Replies are listed 'Best First'.
Re: Why am I getting "syntax error at Funcy.pl line 18, near "$barney return" ?
by haukex (Archbishop) on Jul 24, 2017 at 08:40 UTC

    You're missing a semicolon on the line my $sum = $fred * $barney.

    Update: Also, note that since return will return from the sub, the if won't get executed. And I'm not sure why your if blocks only have one variable in them - if you're trying to return either one or the other value, don't use an if, use the Conditional Operator ?:, as in ( $fred > $barney ? $fred : $barney ).

      Omg I was stuck looking at line 18! Thank you, I thought I was going crazy :p (faith restored)
        Hi prospect,

        when you you have a syntax error indicating the last line of the program, it is very often the case that the actual error is somewhere earlier, sometimes much earlier, in the code.

        In these cases, the error is quite often a missing semi-colon, a missing closing quote mark for a string, a missing closing parenthesis or square bracket, and so on.

Re: Why am I getting "syntax error at Funcy.pl line 18, near "$barney return" ?
by AnomalousMonk (Archbishop) on Jul 24, 2017 at 14:32 UTC
    $fred;   #also why do I have to put another $fred here? It doesn #'t seem to be part of "if" nor of "else"

    To expand on the point that haukex made here, you don't have to put anything there. It doesn't matter what statements you put after the unconditional
        return $sum;
    subroutine return statement (as long as they're syntactically correct): they will never be executed.

    If you have Perl version 5.12 or greater, you can demonstrate this for yourself using the  ...; (yada) (update: or more correctly "ellipsis"; see Update below) statement (see perlop (update: but see also Update note below)). This statement can be compiled anywhere, but will throw an exception if it is ever executed.

    c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version: $]}; ;; my ($fred, $barney) = (7, 6); ;; sub combo { my $sum = $fred * $barney; return $sum; if ($fred > $barney) { ...; } else { ...; } } ;; print combo(); " perl version: 5.012003 42
    Put a  ...; yada | ellipsis statement anywhere in the path of active code execution in the  combo() subroutine and see what happens.

    Update: It seems that Perl documentation was re-organized at some point to move discussion of  ...; to The Ellipsis Statement in perlsyn (the true name of yada is "ellipsis") from perlop (as yada). I don't know when this change was made.


    Give a man a fish:  <%-{-{-{-<