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

Re: undeclared named subs/subrefs under strict

Thread Previous
From:
Paul "LeoNerd" Evans
Date:
November 21, 2022 18:38
Subject:
Re: undeclared named subs/subrefs under strict
Message ID:
20221121183749.6adbe099@shy.leonerd.org.uk
On Mon, 21 Nov 2022 15:33:12 -0300
breno <oainikusama@gmail.com> 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

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About