develooper Front page | perl.perl5.porters | Postings from May 2012

[perl #108286] Bugs in ‘do’ override parsing (Wishlist: Overridable keywords)

From:
Father Chrysostomos via RT
Date:
May 16, 2012 18:10
Subject:
[perl #108286] Bugs in ‘do’ override parsing (Wishlist: Overridable keywords)
Message ID:
rt-3.6.HEAD-4610-1337216994-1545.108286-15-0@perl.org
On Sun Jan 15 12:51:08 2012, sprout wrote:
> do, glob, and require should be overridable based on the hints where
>    the sub is defined, not where the sub is used, because otherwise
>    this would violate existing subs’ expectations. Existing require
>    subs expect to receive "Foo/Bar.pm" rather than "Foo::Bar".  A true
>    override (with a * prototype) would leave it as "Foo::Bar".
>    Similarly for glob overrides: an existing custom glob function
>    might expect the automatic defined() in while(glob()).

perlsub has this to say about do’s prototype:

The built-ins C<do>, C<require> and C<glob> can also be overridden, but due
to special magic, their original syntax is preserved, and you don't have
to define a prototype for their replacements.  (You can't override the
C<do BLOCK> syntax, though).

What happens in reality is different:

$ perl -le 'do("foo","bar")'
Too many arguments for do "file" at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

$ perl -le 'use subs "do"; sub do {warn @_}; do("foo","bar")'
foobar at -e line 1.

So it actually does take a list with no prototype.

With a scalar prototype, I get this:

$ perl -le 'use subs "do"; sub do($) {warn @_}; do("foo","bar")'
bar at -e line 1.

So the entire argument list is being treated as one of the arguments and
put in scalar context, resulting in this oddity:

$ perl -le 'use subs "do"; sub do($$) {warn @_}; do("foo","bar")'
Not enough arguments for main::do at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

And this:

$ perl -le 'use subs "do"; sub do($@) {warn @_}; do("foo","bar")'
bar at -e line 1.

So the only cases that are not buggy are no prototype (or @_), and ($)
called with one argument.

Hence, should I make it so that the prototype simply does apply properly
in all cases (except do{}), since that is what the only non-buggy cases
are doing?  perlsub would have to be updated accordingly, but what it
has right now is not true.

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=108286



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