Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Why does eof have a prototype?

by LanX (Sage)
on Aug 07, 2022 at 16:57 UTC ( #11145999=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I'm trying to parse the usage infos in the pod of perlfunc to create code snippets, and am getting surprised by idiosyncrasies.

For instance is eof magic when called with empty brackets eof()

but has a prototype

$ perl print prototype 'CORE::eof',"\n" __END__ ;* $

See also perlsub

> Note however that some built-ins can't have their syntax expressed by a prototype (such as system or chomp). If you override them you won't be able to fully mimic their original syntax.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: Why does eof have a prototype?
by haukex (Archbishop) on Aug 07, 2022 at 19:13 UTC
    Note however that some built-ins can't have their syntax expressed by a prototype (such as system or chomp). If you override them you won't be able to fully mimic their original syntax.

    IMHO the first sentence doesn't apply here because eof's syntax can be expressed by a prototype - this is also the reason why I personally am not bothered by the fact it does have a protoype. The second sentence only applies because AFAIK there isn't a built-in way for functions to know whether they were called with parens or not, but this has nothing to do with the prototype.

      > IMHO the first sentence doesn't apply here because eof's syntax can be expressed by a prototype

      Depends how you define "syntax can be expressed by a prototype".

      A "can be parsed without errors" criteria is not enough for me.

      eof(); and eof; are (magically) different syntax for eof, and this difference can't be expressed with prototypes.

      In the past the prototype symbol set was extended with *, + or _ in order to add prototypes to other built-ins.

      This criteria isn't met here.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        eof(); and eof; are (magically) different syntax for eof, and this difference can't be expressed with prototypes.

        I'm not sure this is a job for prototypes. Every Perl function can be called with and without parens (given the compiler knows about it), it's always valid syntax. The trick would be to detect inside a hypothetical sub myeof whether the parens are there are not - how would one do this with prototypes? Seems more like a job for something like caller. It's also worth noting that AFAIK eof is the only function in Perl that behaves this way... the exception that confirms the rule ;-)

        Interesting side note: see eof in perltie.

        A "can be parsed without errors" criteria is not enough for me.

        I don't think that's the criteria. It's more like: "Every variation of the builtin's syntax can be expressed by prototypes" - look at threads like Prototype like sort()? where everyone's implementations got close to the original, but noone was able to replicate all of sort's syntax. All of eof's syntax can be replicated, but not its functionality. That's where the difference is.

        I do understand how you are interpreting that piece of documentation, but I think you should cut the authors some slack :-) eof is a unique case, and knowing this, I don't think that passage is inaccurate.

Re: Why does eof have a prototype?
by ikegami (Patriarch) on Aug 08, 2022 at 18:08 UTC

    Two reasons I can think of:

    • So it can be overridden. As I remember it, prototypes were added to ops as part of making them overridable.
    • It was done by accident (i.e. without knowledge the prototype was inadequate).
      the prototype was inadequate

      how so?

        That certain operators (eof, open, etc.) have prototypes that aren't adequate for representing the syntax of the operator is the premise of the question.

        For example,

        • An overridden eof operator can't distinguish whether it was was called as eof or eof() while the real operator does.

        • open( STDERR , ">&=", STDOUT ) is perfectly valid with the real operator, but throws a strict error with an overridden open.

          $ perl <<'.' use strict; open( STDERR , ">&=", STDOUT ); BEGIN { *CORE::GLOBAL::open = sub (*;$@) { } } open( STDERR , ">&=", STDOUT ); . Bareword "STDOUT" not allowed while "strict subs" in use at - line 4. Execution of - aborted due to compilation errors.

        Upd: Fixed accidental double negation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11145999]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2022-12-06 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?