in reply to Constant code

First, I always put an explicit return statement in a subroutine rather than relying upon "return value of last statement" except in sort{} blocks where no return statement is the norm.

When you call a sub() in an expression, the paren's are normally required. There are built in functions and ways to make a user defined function work like a built in function with prototypes.

The simple answer here is to call sub1() and sub2() with no arguments, use sub1(), etc. Don't use a bare word sub name for a normal user defined sub/function.

#!usr/bin/perl use strict; use warnings; sub sub1{return 2} sub sub2{return 2} my ($a, $b) = (sub1, sub2); my ($s, $t) = ((2*sub1()+sub2()), (2*$a+$b)); say STDERR "$a==", sub1; say STDERR "$b==", sub2; say STDERR "$s==$t"; __END__ 2==2 2==2 6==6

Replies are listed 'Best First'.
Re^2: Constant code
by Anonymous Monk on Jan 20, 2017 at 21:00 UTC

    The simple answer here is to call sub1() and sub2() with no arguments, use sub1(), etc. Don't use a bare word sub name for a normal user defined sub/function.

    And yet, we encourage this all the time in OO paradigms, where we have a member function foo to get the value of 'foo': $obj->foo instead of $obj->foo(), ostensibly because it is a 'value' and the fact that foo is a function is just an implemention detail.

    Fooey, I say: if you end up creating a stack entry, use parens. That's just my curmudgeonly USD 0.02.

      I have seen parentheses-less method calls outside of getting only a (variable) value, namely in cases where no parameters are being passed. (I don't have any examples handy right now.)

      Sorry, that ^ was me.

        I admit this is a bit of a syntactic nit, but in the  $object_reference->method and  ClassName->method OO cases, doesn't the  -> operator as unambiguously indicate function invocation (both to compiler and to programmer) as the parentheses in the  func() case? I.e., always writing  $obj->method without any parentheses is the same as always writing  func() with its empty parentheses. (Although I certainly agree that  $obj->method() is perfectly valid.)


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