in reply to design the open function

Use the subs pragma:

use strict; use warnings; use subs qw/ open /; open(); sub open { print "Hello world!\n"; }

Here is an excerpt from the Camel book, Programming Perl

Overriding may be done only by importing the name from a module--ordinary predeclaration isn't good enough. To be perfectly forthcoming, it's the assignment of a code reference to a typeglob that triggers the override, as in *open = \&myopen. Furthermore, the assignment must occur in some other package; this makes accidental overriding through typeglob aliasing intentionally difficult. However, if you really want to do your own overriding, don't despair, because the subs pragma lets you predeclare subroutines via the import syntax, so those names then override the built-in ones:

I'm not going to address whether this is actually a good idea, other than to say you ought to really really really think about other options first, especially if you care about writing maintainable code.

Update: There's additional information on the subject in perlfaq7 and perlsub.


Dave

Replies are listed 'Best First'.
Re^2: design the open function
by singam (Initiate) on May 10, 2006 at 07:34 UTC
    thanks Davido,
    but if i like to open a file inside that open subroutine.
    then it will become recursive function right.
    How to eliminate that error.

      Within the subroutine, call the fully qualified name for open: CORE::open( ....... ).

      CORE::open() will always refer to the original open. ....always, that is, unless you read in perlsub how to mess that up too. ;) (Hint: don't mess with CORE::open, without an extraordinarily good reason.)


      Dave

        thanks for your reply Davido,
        use warnings; use subs qw/ open /; sub open { print "Hello world!\n"; CORE::open($_[0],"d:\\input.txt") } 1;
        i call the above module named open_temp.pm from the following file
        use lib "d:\\"; use open_temp; open(SEL); print <SEL>;
        will the above code good and efficient??
        whether it will work for everything?