Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: New Discovery!!! (sub call without parentheses)

by markong (Pilgrim)
on Dec 08, 2018 at 13:18 UTC ( [id://1226968]=note: print w/replies, xml ) Need Help??


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

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.

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

Replies are listed 'Best First'.
Re^3: New Discovery!!! (sub call without parentheses)
by LanX (Saint) on Dec 08, 2018 at 18:12 UTC
    > 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.

        > 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.

        The technical term should be "Late binding" aka "Dynamic binding" (sic)

        Quotes:

        • With late binding the compiler does not read enough information to verify the method exists or bind its slot on the v-table. Instead the method is looked up by name at runtime.
        • In most dynamically-typed languages, the list of methods on an object can be altered at runtime. This requires late binding.

        HTH! :)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1226968]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found