in reply to New Discovery!!! (sub call without parentheses)

To catch typos.

Perl is dynamic enough to allow calling subs which are not pre declared.

I.e. not known at the time of parsing the call.

NB the sub doesn't even need to be declared in the same file.

And if the sub is missing you'll get a run time error.

To avoid ambiguity you need to be explicit that you are calling a sub.

Languages like Ruby allow dynamic subs without brackets, because a simple bare word isn't allowed to be a string there (like in non strict Perl)

See also use subs and strict#strict-subs

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

edit

I think it's an effect of Perl4's design, where bare words could be strings and &subs needed a sigil.

  • Comment on Re: New Discovery!!! (sub call without parentheses)

Replies are listed 'Best First'.
Re^2: New Discovery!!! (sub call without parentheses)
by LanX (Saint) on Dec 08, 2018 at 12:55 UTC
    This little demo in the debugger (without strict) should make it clearer

    DB<5> print "x".NAME # bareword treated as string xNAME DB<6> sub NAME {42} # sub declaration DB<7> print "x".NAME # bareword now known to be s +ub x42 DB<8> print "x".NAME2() # explicit sub call Undefined subroutine &main::NAME2 called at (eval 17)[C:/Perl_524/lib/ +perl5db.pl:737] line 2. DB<9> print "x".&NAME2 # explicit sub call Undefined subroutine &main::NAME2 called at (eval 18)[C:/Perl_524/lib/ +perl5db.pl:737] line 2. DB<10>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: New Discovery!!! (sub call without parentheses)
by markong (Pilgrim) on Dec 08, 2018 at 13:18 UTC
    Perl is dynamic enough to allow calling subs which are not pre declared. I.e. not known at the time of parsing the call.

    Is "dynamic" here used as a notion of "smart enough to understand by itself where to go look for things before exploding at run-time"? Because even in Java and C, which aren't "dynamic" in the classic sense of languages in which you must (statically) declare each variable/object type at compile time, you can do the same thing, e.g.: in Java you can call a method declared at the bottom of the class.

    There are difference in the way the compilation phase is carried out for these languages, but they generally tend to process the entire "compilation unit" when they are building the parse tree (or whatever they call it), so they can understand reference to functions wherever they are defined (C for example halts you at linking phase if it can't find the relative object). I think Perl does a similar thing when you call a sub() defined at the end of a module to avoid exploding during the compilation(somebody would say interpretation in strict terms) phase.

    This is not the case for Python, which stops if the token(function name) is unknown(NameError) during parsing.

      > Is "dynamic" here used as a notion of "smart enough to understand by itself where to go look for things before exploding at run-time"?

      dynamic in the sense of not exploding at compile-time.

      > I think Perl does a similar thing

      not really?

      Perl looks at runtime into the symbol table of the package. I wouldn't call this linking, but I'm no C expert.

      DB<10> func() Undefined subroutine &main::func called at (eval 19)[C:/Perl_524/lib/p +erl5db.pl:737] line 2. DB<11> eval { sub func {} } # created at run-time DB<12> func() DB<13> p \&main::func # STASH entry CODE(0x58443b8) DB<14>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        dynamic in the sense of not exploding at compile-time.

        i.e.: "smart enough to understand by itself where to go look for things(symbol table) before exploding at run-time" :P

        I wouldn't call this linking, but I'm no C expert.

        No, it is not. I was just referring to how different languages approach the "not still defined token/name" case, I wasn't suggesting Perl "linking" something during/after compilation. I was trying to break the supposed link between been able to call something not yet defined and dynamicity, because the word "dynamic" is so abused is CS literature and on the WWW, that I always have to ask further for disambiguate.