Bug in overriding 'require' and 'do'?

Hi all,

There was some discussion on London.pm about overriding 'do'.  To
determine if something is overrideable, you can use the prototype
function:

  prototype FUNCTION
    Returns the prototype of a function as a string (or "undef" if
    the function has no prototype).  FUNCTION is a reference to, or
    the name of, the function whose prototype you want to retrieve.

    If FUNCTION is a string starting with "CORE::", the rest is
    taken as a name for Perl builtin.  If the builtin is not over-
    ridable (such as "qw//") or its arguments cannot be expressed
    by a prototype (such as "system") returns "undef" because the
    builtin does not really behave like a Perl function.  Other-
    wise, the string describing the equivalent prototype is
    returned.

In other words, if 'prototype "CORE::do"' returns undef, you cannot
override 'do'.

  ~ $ perl -MData::Dumper -le 'print Dumper prototype "CORE::do"'
  $VAR1 = undef;

  # apparently, we cannot override require either:
  ~ $ perl -MData::Dumper -le 'print Dumper prototype "CORE::require"'
  $VAR1 = undef;

  # here's an example of a built-in you can override
  ~ $ perl -MData::Dumper -le 'print Dumper prototype "CORE::die"'
  $VAR1 = '@';

However, it appears that you *can* override both 'do' and 'require':

  #!/usr/bin/perl -l

  use strict;
  use warnings;

  BEGIN {
    *CORE::GLOBAL::require =
      sub (*) { die "Death *is* an option for @{[shift]}" };
    *CORE::GLOBAL::do = *CORE::GLOBAL::require;
  }

  foreach my $stuff ( "use Fun", "do 'something'" ) {
    eval $stuff || print $@;
  }

That prints out:

  Death *is* an option for Fun.pm at over.pl line 8.
  BEGIN failed--compilation aborted at (eval 1) line 2.

  Death *is* an option for something at over.pl line 8.

What have I misunderstood?  Is this a bug?  Since there's code out
there which uses this feature, if this is a bug, I suggest that the
'prototype' docs be updated.

Cheers,
Ovid

--
Buy the book  - http://www.oreilly.com/catalog/perlhks/
Perl and CGI  - http://users.easystreet.com/ovid/cgi_course/
Personal blog - http://publius-ovidius.livejournal.com/
Tech blog     - http://use.perl.org/~Ovid/journal/
0
publiustemp
8/3/2007 4:32:38 PM
perl.perl5.porters 47435 articles. 0 followers. Follow

6 Replies
295 Views

Similar Articles

[PageSpeed] 12

On 03/08/07, Ovid <publiustemp-p5p3@yahoo.com> wrote:
> What have I misunderstood?  Is this a bug?  Since there's code out
> there which uses this feature, if this is a bug, I suggest that the
> 'prototype' docs be updated.

Yes I'll do it. A doc fix is fine for 5.10.
BTW I posted the excuse for the bug:
http://london.pm.org/pipermail/london.pm/Week-of-Mon-20070730/009377.html
0
rgarciasuarez
8/3/2007 4:39:56 PM
On Fri, August 3, 2007 9:32 am, Ovid wrote:

> There was some discussion on London.pm about overriding 'do'.  To
> determine if something is overrideable, you can use the prototype function:

That's a general rule, but not always true.  perlsub qualifies:
> Note however that some built-ins can't have their syntax expressed by a
> prototype (such as C<system> or C<chomp>).  If you override them you won't
> be able to fully mimic their original syntax.
>
> 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).

plus more about require.

> What have I misunderstood?  Is this a bug?  Since there's code out
> there which uses this feature, if this is a bug, I suggest that the
> 'prototype' docs be updated.

perlfunc:
> If FUNCTION is a string starting with C<CORE::>, the rest is taken as a
> name for Perl builtin.  If the builtin is not I<overridable> (such as
> C<qw//>) or its arguments cannot be expressed by a prototype (such as
> C<system>) returns C<undef> because the builtin does not really behave
> like a Perl function.  Otherwise, the string describing the equivalent
> prototype is returned.

Where do you see docs that need to be updated?

0
sthoenna
8/3/2007 4:47:33 PM
On 3 Aug 2007, at 17:32, Ovid wrote:
> However, it appears that you *can* override both 'do' and 'require':

Indeed. I'm just dusting down Devel::TraceLoad and confirm that :)

> What have I misunderstood?  Is this a bug?  Since there's code out
> there which uses this feature, if this is a bug, I suggest that the
> 'prototype' docs be updated.

I wondered if it came under the

"If the builtin is not overridable (such as qw//) *or its arguments  
cannot be expressed by a prototype*"

clause (my emphasis). While overriding require certainly works enough  
(well enough for jazz anyway) is there something about its real  
prototype that makes it impossible for prototype (the function) to  
express?

-- 
Andy Armstrong, hexten.net

0
andy
8/3/2007 4:51:54 PM
On 3 Aug 2007, at 17:51, Andy Armstrong wrote:
> "If the builtin is not overridable (such as qw//) *or its arguments  
> cannot be expressed by a prototype*"
>
> clause (my emphasis). While overriding require certainly works  
> enough (well enough for jazz anyway) is there something about its  
> real prototype that makes it impossible for prototype (the  
> function) to express?

How does the london.pm listserv /know/ I'm just about to post a 'me  
too' so it can defer delivery of everyone else's mails until just  
after I've posted? :)

-- 
Andy Armstrong, hexten.net

0
andy
8/3/2007 4:54:57 PM
On Fri, 2007-08-03 at 17:54 +0100, Andy Armstrong wrote:
> On 3 Aug 2007, at 17:51, Andy Armstrong wrote:
> > "If the builtin is not overridable (such as qw//) *or its arguments  
> > cannot be expressed by a prototype*"
> >
> > clause (my emphasis). While overriding require certainly works  
> > enough (well enough for jazz anyway) is there something about its  
> > real prototype that makes it impossible for prototype (the  
> > function) to express?
> 
> How does the london.pm listserv /know/ I'm just about to post a 'me  
> too' so it can defer delivery of everyone else's mails until just  
> after I've posted? :)
> 

Well London.pm is omnipresent and omniscient in aggregate - you would
have that we'd have a clever list manager :-D

/J\
0
jns
8/3/2007 6:26:08 PM
--- Yitzchak Scott-Thoennes <sthoenna@efn.org> wrote:

> That's a general rule, but not always true.  perlsub qualifies:
<snip>
> > 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). 
> plus more about require.
>
> Where do you see docs that need to be updated?

Sorry in being late responding.

The docs for 'prototype' need to be updated because they are incorrect
(it is not correct that non-overrideable builtins always return 'undef'
for the 'prototype' function).  The person (me, in this case, but also
a number of folks on London.pm), did not realize that there were
exceptions to the very explicit rule laid down in the prototype docs,
nor was there even a reference to where to go for more information.

Cheers,
Ovid

--
Buy the book  - http://www.oreilly.com/catalog/perlhks/
Perl and CGI  - http://users.easystreet.com/ovid/cgi_course/
Personal blog - http://publius-ovidius.livejournal.com/
Tech blog     - http://use.perl.org/~Ovid/journal/
0
publiustemp
8/7/2007 7:56:15 AM
Reply:

Similar Artilces:

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

'''''
Name: mario Email: ramar17atfastwebnetdotit Product: Gran Paradiso Alpha 2 Summary: ''''' Comments: K: Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

'do' won't 'do' if '/'
Greetings to All from Au, Have a NetWare Perl 5.8.4 and wanted to tweak File\Spec\NW.pm to try and standardise on '/' separators. If I run a test script (t/uni/lower.t) with an unmodified NW.pm, it calls t/uni/case.pl, that, in case.pl, (when the path separators are '\'), the $file is '..\lib\unicore\To\Lower.pl' and the following code portion works, with $simple getting a returned table: sub casetest { my ($base, $spec, $func) = @_; my $file = File::Spec->catfile(File::Spec->catdir(File::Spec->updir, "lib", &qu...

'''
Name: L Lachowsky Email: e2brutus_10atyahoodotcom Product: Firefox Summary: ''' Comments: why duddn this surprise me... I change default server from IE to Mozilla, and then i get error messages. well, shud I decide to not get on my computer with Mozilla..or shud I risk security breeches with IE....hmmm at least i can get on the internet with IE Browser Details: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expectation of a response to this fee...

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

table_info('','','','%') cannot return any types
I was recently confirming table_info special cases and discovered the case for getting table_types cannot work. table_info('','','','%') should return a list of table types but it returns a list of empty strings instead: my @types = $h->tables('', '', '', '%'); print "all types:\n", join("xxx\n", @types), "\n"; # should output something like: # "dbo" # "INFORMATION_SCHEMA" # "sys" # and actually outputs: xxx xxx It seems to be down to the following ...

'use' and 'require'
------_=_NextPart_001_01C35AB9.4ADA5CA0 Content-Type: text/plain Hello all, Is there a way to 'variablize' the version for a 'use' statement. This way, if I wanted to change version I could only change it in one place with those changes cascading across all apps. Usage would look something like this: use $perl_version; Thanks in advance, Jason Allison Principal Engineer ARINC Incorporated Office: (410) 266-2006 FAX: (410) 573-3026 ------_=_NextPart_001_01C35AB9.4ADA5CA0-- Allison, Jason (JALLISON) wrote: > Hello all, > > Is there ...

'IN' Clause or 'OR'
Hello, [1]: select * from TABLEA where COL1 IN('value1','value2''valu3'....) [2]:select * from TABLEA where COL1 = 'value1' OR COL1= 'value2' OR COL1='valu3'. TABLEA is a huge table and it has non-clustered index on COL1. Among the above 2 queries, which query will give me the better performance or fast response and WHY? What is the difference between 'IN' and 'OR' clauses as for as Sybase Optimization is concerned. Which is the better one to be used on huge tables. Thanks. Mac An IN list is treated ...

'b'..'a'
Hello. I'm using defferent 5.6.0's for Win32. I wonder wheter following behaviour is intentional or not: d:\>perl -e "print 'b'..'c'" bc d:\>perl -e "print 'b'..'a'" bcdefghijklmnopqrstuvwxyz I expected empty list in latter case, like in perl -e "print 'bb'..'a'" <!ENTITY Vadim REALLIFE "Vadim V.Konovalov, St.Petersburg, Russia"> &Vadim; On Wed, Sep 20, 2000 at 03:14:41PM +0400, Konovalov, Vadim wrote: > Hello. > > I'm using defferent 5.6.0'...

'or' or 'union'
Hello I was just wondering, in general what is better to use, an 'or' clause in a select or a 'union' to join two selects together. Do both statements create work tables? Many thanks Alex I think OR will be better than union. because suppose u have 3 tables and using OR u can join table a and table b and table c so each table will have only one read. but using union you will join table a and table b and in another query of union u will use table (a or b) and table c so ur one read is more in union . Ramdas Alex Cheung wrote: > Hello > >...

'Value' should be between 'minimum' and 'maximum'.
I'm attempting to invoke with parameters:  Dim thing2 As New mydelsubPBStep(AddressOf pbStep) Me.Invoke(thing2, "setMax", dt.Rows.Count)      <---- this is where the error occurs   the delegate looks like this: Private Delegate Sub mydelsubPBStep(ByVal mode As String, ByVal value As Integer)   the function it's calling looks like this:Private Sub pbStep(ByVal mode As String, ByVal value As Integer) Select Case mode.ToLower Case "clear" ProgressBar1.Value = 0 Case "step" ProgressBar1.PerformStep() L...

EXEC sp_msforeachtable 'sp_spaceused ''?'''
What does the following SQL code mean?  EXEC sp_msforeachtable 'sp_spaceused ''?'''Johan TheunissenMCPD, MCSE, MCTS BizTalk 2006==============================Please mark the most helpful reply/replies as "Answer". JohanNL:sp_msforeachtable This is the name of the SP that resides in the master database.  This SP executes one or more commands for a table. JohanNL:sp_spaceused This is the command that you want to run for each of the tables residing in your current database.  As you might have understood so far, that this is again a...

'To', 'CC', & 'BCC'
Name: Dick Tracy Email: philipdottracyatoptusnetdotcomdotau Product: Thunderbird Summary: 'To', 'CC', & 'BCC' Comments: I have just started using Thunderbird and while I find it quite excellent- I suggest having a button to add addressees to 'BCC' as well. Currently each 'BCC' addressee has to be selected manually. I send e-mail to quite a number of people at a time and I do not wish to readily spread others addresses easily. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0....

Changing 'Bug' to 'Issue'
Hi, I am using Bugzilla 2.22. I've changed the relevant parameters (in template/en/custom/global/variables.none.tmpl) to change all instances of 'bug' in bugzilla to 'issue'; but there is one mention of bug that hasn't changed and I can't find the relevant code to change it. It's the first option in the first drop down box of the Boolean Charts in the advanced search. Has anyone else had this problem and/or know how to correct it? Thanks ...

Web resources about - Bug in overriding 'require' and 'do'? - perl.perl5.porters

Resources last updated: 1/9/2016 4:20:21 AM