in reply to Re: design the open function
in thread design the open function

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.

Replies are listed 'Best First'.
Re^3: design the open function
by davido (Cardinal) on May 10, 2006 at 07:38 UTC

    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?

        That won't work because you're not importing the version of open() defined within open_temp.pm into package main. You really should just read the perlsub document, like I linked to before. And honestly, I don't think you've followed my advice about really really really thinking about whether you actually need to do it this way. Just because Perl lets you override built-in functions doesn't mean you should. Why do you need an overridden open function? Why not just call your new function "myopen()"? Do you have to override for some good reason?


        Dave