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

O Great Monks of Perl, upon running a program involving this subroutine, I receive the error: "Illegal declaration of subroutine main::profit." I've read over this repeatedly and have not found an error.
sub profit = { (((($x-10)/20)*(((-1)*((1/10)*(10-$x)*($x+1)+1000) + sqrt(((1/10)* +(10-$x)*($x+1)+1000)**2 - (($x-10)/5)*(1000*$x+200)))/(2*($x-10)/20)) +**3)/3 +(1/2)*((1/10)*(10-$x)*($x+1)+1000)*(((-1)*((1/10)*(10-$x)*($x ++1)+1000) + sqrt(((1/10)*(10-$x)*($x+1)+1000)**2 - (($x-10)/5)*(1000* +$x+200)))/(2*($x-10)/20))**2+(1000*$x+200)*(((-1)*((1/10)*(10-$x)*($x ++1)+1000) + sqrt(((1/10)*(10-$x)*($x+1)+1000)**2 - (($x-10)/5)*(1000* +$x+200)))/(2*($x-10)/20))); }
The preview seems to have done strange things to the code, here it is in regular text: sub profit = { (((($x-10)/20)*(((-1)*((1/10)*(10-$x)*($x+1)+1000) + sqrt(((1/10)*(10-$x)*($x+1)+1000)**2 - (($x-10)/5)*(1000*$x+200)))/(2*($x-10)/20))**3)/3 +(1/2)*((1/10)*(10-$x)*($x+1)+1000)*(((-1)*((1/10)*(10-$x)*($x+1)+1000) + sqrt(((1/10)*(10-$x)*($x+1)+1000)**2 - (($x-10)/5)*(1000*$x+200)))/(2*($x-10)/20))**2+(1000*$x+200)*(((-1)*((1/10)*(10-$x)*($x+1)+1000) + sqrt(((1/10)*(10-$x)*($x+1)+1000)**2 - (($x-10)/5)*(1000*$x+200)))/(2*($x-10)/20))); }

Replies are listed 'Best First'.
Re: Illegal subroutine
by toolic (Bishop) on Oct 24, 2011 at 18:33 UTC
    sub profit = {
    Change that to (get rid of "="):
    sub profit {
    and the error goes away. See also perlsub.

      Do read perlsub if you meant to make a function. There are math examples you may emulate within perlsub (just search for the string "cube_root" within perlsub to get a feel for what you're trying to accomplish).

      FWIW,if you were not trying to create a function, and instead merely set a variable to calculate profit you would have the following (note: I changed the equation a lot):

      my $net = 123.456; my $coffee_allowance = 100.00; my $profit = ( $net - $coffee_allowance ); my $step_three = $profit; print "profit = $step_three \n";
      (minor code updates)
Re: Illegal subroutine
by Corion (Patriarch) on Oct 24, 2011 at 18:34 UTC

    Have you tried taking out terms? Try eliminating terms as long as the error persists.

    Have you looked at taking out the complete subroutine? Are you sure that the error is with your subroutine?

    Have you looked at perlsyn to check that your subroutine declaration is correct?