Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

When do subs get overridden?

by LAI (Hermit)
on Apr 03, 2003 at 21:06 UTC ( [id://247882]=perlquestion: print w/replies, xml ) Need Help??

LAI has asked for the wisdom of the Perl Monks concerning the following question:

I was reading through a pretty masterful bit of redirection in a recent obfu written by SarahM. (It really is quite nice; I recommend you check it out before reading the spoilers below) Something in it prompted me to wonder about the way perl deals with namespaces of packages, and I was a little confused. I knew what was happening, but I couldn't put my finger on why.

Here's the spoiler to the obfu: SarahM defined a sub q, and later used the builtin q to fool the reader into thinking she was using her own sub. So my question is: under what circumstances does the builtin q// get overridden (assuming one would want to do such a thing)? Is it possible to code the following so that a simple call of q("something") will execute main::q?

sub q { return "inside main sub q"; } print "Ambiguity gets us the builtin: \n"; print q("not in sub q"), "\n"; print "Let us be more specific: \n"; print main::q("this does not get printed"), "\n"; __END__ Ambiguity gets us the builtin: "not in sub q" Let us be more specific: inside main sub q

LAI

__END__

Replies are listed 'Best First'.
Re: When do subs get overridden? (&)
by tye (Sage) on Apr 03, 2003 at 21:28 UTC

    My practice it to use mixed case in subroutine names or to use both & and () when calling my subroutines. See (tye)Re: A question of style for more details.

    print &q("calling a subroutine"), $/;

    And, no, you can't override q//. You can override things like open. And you can tell this because prototype("CORE::q") returns undef while prototype("CORE::open") returns "*;$@". See prototype.

                    - tye
Re: When do subs get overridden?
by MrYoya (Monk) on Apr 03, 2003 at 21:34 UTC
    Check out perlsub, under "Overriding Built-in Functions". Also, 12.11 in the Perl Cookbook.

    According to the Perl Cookbook, there are some functions that simply cannot be overridden -- 'q' is one of these.

Re: When do subs get overridden?
by Mr. Muskrat (Canon) on Apr 03, 2003 at 21:12 UTC

    You missed one more way two ways of calling main::q

    print ::q("this isn't printed either"),$/; print &q("me either"),$/;

    Update: added second line of code and this:
    If I read the docs correctly, you should be able to override the built-ins by using prototypes. And I had better shut up before I give away all my obfu secrets ;-)

      I know there's different ways of calling main::q. What I'd like to do, though, is to override the builtin q so that I can call main::q with a plain ol' q().

      Actually, no. I don't want to do that, I want to know how to do that, if possible. I actually have no plans to override builtins for any purpose, but I'm just curious as to if and how it's possible.

      LAI

      __END__
Re: When do subs get overridden?
by dakkar (Hermit) on Apr 05, 2003 at 11:24 UTC

    You see, q is not a subroutine, it's a syntactic construct, like quotes or braces. There is nothing to "override". You can, however, change its semantic a bit with:

    • a source filter (ok, this is obvious)
    • redefinition of stringification for some datatype (package MyQ;use overload '""' => sub {return "overridden"};)

    -- 
            dakkar - Mobilis in mobile
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://247882]
Approved by Enlil
Front-paged by Coruscate
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found