develooper Front page | perl.perl5.porters | Postings from November 2022

Re: undeclared named subs/subrefs under strict

Thread Previous
Paul "LeoNerd" Evans
November 21, 2022 18:38
Re: undeclared named subs/subrefs under strict
Message ID:
On Mon, 21 Nov 2022 15:33:12 -0300
breno <> wrote:

> Dear porters,
> when sub bar is not declared at all, 'sub foo { bar() }' compiles
> under strict/warnings without any issues. Same goes for 'my $x =
> \&bar'.
> Is this by design? I understand why glob refs would not warn or croak
> but Is there a reason why explicitly named subroutines that don't
> exist at compile time would not yield even a warning until they are
> actually called?

Short answer: AUTOLOAD

Long answer: Back when memory and CPU were nowhere near as large/fast as
they are now, it was common for a perl module to not try to define
dozens or hundreds of subs on startup, because that would slow things
down and eat memory. Instead it would keep them in plain text somewhere
cheap, and provide an AUTOLOAD sub that would lazily define & compile
them as necessary, first time they were called.

Paul "LeoNerd" Evans      |  |

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About