develooper Front page | perl.perl5.porters | Postings from February 2018

Re: Why does inclusion of require that is not executed have sideeffects?

Thread Previous | Thread Next
From:
Eirik Berg Hanssen
Date:
February 23, 2018 17:27
Subject:
Re: Why does inclusion of require that is not executed have sideeffects?
Message ID:
CAHAeAG6jyDTQ1rn8xGfD-GaYy0TB7QS+urDyCr82mtSsLV7jXw@mail.gmail.com
On Fri, Feb 23, 2018 at 6:06 PM, Dagfinn Ilmari Mannsåker <ilmari@ilmari.org
> wrote:

> demerphq <demerphq@gmail.com> writes:
> > Why would a require that is not executed have any side effects? Why is
> > the eval necessary to avoid those side effects? Is this a bug or have
> > I missed something?
>
> As haarg pointed out on #p5p, merely compiling a 'require BAREWORD'
> creates the package.  A bit of digging leads to a
> gv_stashpvn(…, GV_ADD|…) call in toke.c, and some more digging with git
> blame shows that this was added in 5.001, with the following Changes
> entry:
>
> NETaa13235: require BAREWORD now introduces package name immediately.
> From: Larry Wall
> Files patched: toke.c
>  require BAREWORD now introduces package name immediately.  This lets the
>  method intuit code work right even though the require hasn't actually run
>  yet.
>
> So it's a deliberate desicion by Larry in 1995 to make indirect method
> syntax work better with require (as opposed to use).
>

  Nice sleuthing.

  Now, I do find myself wondering if C<< perldoc -f require >> should
document this compile time effect?

  I mean, I doubt it could be called an implementation detail likely to
change, but neither do I imagine most programmers would ever need to know,
so ... I dunno, what does the documentation aspire to be?


Eirik

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