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

Re: undeclared named subs/subrefs under strict

Thread Previous | Thread Next
From:
Dagfinn Ilmari Mannsåker
Date:
November 22, 2022 00:17
Subject:
Re: undeclared named subs/subrefs under strict
Message ID:
87ilj7j109.fsf@wibble.ilmari.org
breno <oainikusama@gmail.com> writes:

> 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?
>
> On a somewhat related note, how would one detect if a named
> subroutine/subref was declared or not (it's ok if it was declared but is
> empty) at runtime? Right now I'm doing:
>
> sub subref_is_actually_there ($subref) {
>     require B;
>     my $cv = B::svref_2object($subref);
>     return B::class($cv->ROOT) eq 'NULL' && !${ $cv->const_sv };
> }
>
> and it seems to work fine but I'm not confident in it. Is there a better or
> more readable/portable way? One that would not need to rely on B::*, maybe
> just checking if {CODE} is undef?
>
> I looked on perlsub and perlref but could not reach any reasonable
> explanation on either, so any help would be greatly appreciated.

Have a look at how https://metacpan.org/pod/Sub::StrictDecl does it.

Once we decide how to do non-default strictures, would it be worth
bringing it into core?


> Cheers!
> garu

- ilmari

Thread Previous | Thread Next


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