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:
ilmari
Date:
February 23, 2018 17:06
Subject:
Re: Why does inclusion of require that is not executed have sideeffects?
Message ID:
d8j1shbtz44.fsf@dalvik.ping.uio.no
demerphq <demerphq@gmail.com> writes:

> Repost with a better subject. Sorry for the duplication....
>
> On 23 Feb 2018 17:31, "Yves Orton" <demerphq@gmail.com> wrote:
>
> +            if ($pack->can("((")) {
> +                # this eval is required, or fail the overload test
> +                # in dist/Carp/t/vivify_stash.t, which is really quite weird.
> +                # Even if we never enter this block, the presence of
> the require
> +                # causes the test to fail. This seems like it might be a bug
> +                # in require. Needs further investigation - Yves
> +                eval "require overload; 1"
> +                    or return "use overload failed";
> +            }
> +            my $sub = _fetch_sub(overload => 'StrVal');
> +            return $sub ? &$sub($arg) : "$arg";
>          }
>      }
>
>
>
> Can anybody shed any light on why the eval above is required? If you
> replace that line with a simple "require overload;" then even when the
> if condition is false and the require is NOT executed we fail the
> "didn't load overload" test in vivify_stash.t which makes no sense to
> me.
>
> 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).

- ilmari
-- 
"I use RMS as a guide in the same way that a boat captain would use
 a lighthouse.  It's good to know where it is, but you generally
 don't want to find yourself in the same spot." - Tollef Fog Heen

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