For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > Can the defined operator/function be overridden?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can the defined operator/function be overridden?
Marcio Faustino

2007-09-22, 7:00 pm

Hi,

Can the defined operator/function be overridden? I ask this because
when I try to override it, apparently nothing happens. For example:

#----------------------------
use strict;
use subs 'defined';
use warnings;

sub defined($@) {
print "Inside defined\n";
}

defined(0) or die;
#----------------------------

When running this code, nothing is printed.
Am I doing something wrong?

Thanks,

Tom Phoenix

2007-09-22, 7:00 pm

On 9/21/07, Marcio Faustino <m.faustino@gmail.com> wrote:

> Can the defined operator/function be overridden?


Have you seen what the perlsub manpage has to say about "Overriding
Built-in Functions"?

http://perldoc.perl.org/perlsub.htm...ORE%3a%3aGLOBAL

It should be possible, even if it's not a good idea. Good luck with it!

--Tom Phoenix
Stonehenge Perl Training
John W. Krahn

2007-09-22, 10:03 pm

Marcio Faustino wrote:
> Hi,


Hello,

> Can the defined operator/function be overridden?


perldoc -f prototype

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 overridable
(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. Otherwise, the
string describing the equivalent prototype is returned.


So a function like 'open' can be overridden:

$ perl -le' print prototype "CORE::open"'
*;$@

But it looks like 'defined' cannot:

$ perl -le' print prototype "CORE::defined"'




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Gunnar Hjalmarsson

2007-09-22, 10:03 pm

Marcio Faustino wrote:
> Can the defined operator/function be overridden? I ask this because
> when I try to override it, apparently nothing happens. For example:
>
> #----------------------------
> use strict;
> use subs 'defined';
> use warnings;
>
> sub defined($@) {
> print "Inside defined\n";
> }
>
> defined(0) or die;
> #----------------------------


"use subs" doesn't seem to work on defined(). OTOH, you can call the
function with the ampersand or with a fully qualified name.

&defined();

main::defined();

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Nobull67@Gmail.Com

2007-09-23, 6:59 pm

On 23 Sep, 00:10, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
>
> "use subs" doesn't seem to work on defined(). OTOH, you can call the
> function with the ampersand or with a fully qualified name.
>
> &defined();
>
> main::defined();


This is true but not really very useful. The real reason to define
subroutines that happen to have the same name as built-in functions is
so that they can be called as methods.

ISTR that a recent version of Perl introduces weak built-in functions.
These _can_ be overridden simply by declaring a subroutine of the same
name. This is, of course, an ugly hack to add reserved words to the
language without breaking existing code.

Marcio Faustino

2007-09-23, 9:59 pm

On Sep 22, 9:47 pm, t...@stonehenge.com (Tom Phoenix) wrote:
> Have you seen what the perlsub manpage has to say about "Overriding
> Built-in Functions"?
>
> http://perldoc.perl.org/perlsub.htm...Functions-bu...
>
> It should be possible, even if it's not a good idea. Good luck with it!


No I haven't, but I've read the Camel book instead :-)


On Sep 23, 12:01 am, kra...@telus.net (John W. Krahn) wrote:
> So a function like 'open' can be overridden:
>
> $ perl -le' print prototype "CORE::open"'
> *;$@
>
> But it looks like 'defined' cannot:
>
> $ perl -le' print prototype "CORE::defined"'


Thanks, I didn't know that functions whose arguments cannot be
expressed by a prototype couldn't be overridden.


On Sep 23, 12:10 am, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
> "use subs" doesn't seem to work on defined(). OTOH, you can call the
> function with the ampersand or with a fully qualified name.
>
> &defined();
>
> main::defined();


Thanks for the tip!

John W. Krahn

2007-09-24, 4:00 am

Marcio Faustino wrote:
>
> On Sep 23, 12:01 am, kra...@telus.net (John W. Krahn) wrote:
>
> Thanks, I didn't know that functions whose arguments cannot be
> expressed by a prototype couldn't be overridden.


That's the only reason (AFAIK) that prototypes were added to Perl, to override
built-in functions.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com