Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Override the open builtin globally

by Khen1950fx (Canon)
on Jun 13, 2011 at 19:47 UTC ( [id://909431]=note: print w/replies, xml ) Need Help??


in reply to Override the open builtin globally

I think that you were after something like this:
#!/usr/bin/perl use strict; use warnings; BEGIN { *CORE::GLOBAL::open = sub (*;$@) { if(defined($_[0])) { use Symbol qw(); my $handle = Symbol::qualify($_[0], (caller)[0]); no strict 'refs'; if(@_ == 1) { return CORE::open($handle); } elsif(@_ == 2) { return CORE::open($handle, $_[1]); } else { return CORE::open($handle, $_[1], @_[2..$#_]); } } }; }
As I understand it, to override globally, you need to use CORE::GLOBAL in a BEGIN block because it has to be done at compile time.

Replies are listed 'Best First'.
Re^2: Override the open builtin globally
by Eliya (Vicar) on Jun 13, 2011 at 20:56 UTC
    if(defined($_[0])) { ...

    You forgot to handle the case when $_[0] is undef, i.e. when the form open my $fh, ... is being used.

    BEGIN { *CORE::GLOBAL::open = sub (*;$@) { use Symbol (); my $handle = Symbol::qualify_to_ref($_[0], scalar caller); $_[0] = $handle unless defined $_[0]; # pass up to caller if (@_ == 1) { CORE::open $handle; } elsif (@_ == 2) { CORE::open $handle, $_[1]; } elsif (@_ == 3) { if (defined $_[2]) { CORE::open $handle, $_[1], $_[2]; } else { CORE::open $handle, $_[1], undef; # special case } } else { CORE::open $handle, $_[1], $_[2], @_[3..$#_]; } }; } # sample usage: open my $fh, ">", \my $buf or die $!; print $fh "hello"; print $buf; # prints "hello"
      Hi,

      Could you kindly advise about writing using dumper?

      The following open using last version:

      open(FILE,"> tmp.pm"); print FILE "blabla"; # works correctly #print FILE Dumper %tmp_hash; #works correctly print FILE Data::Dumper %tmp_hash; #fails: "Can't locate object metho +d "FILE" via package "Data::Dumper" "
      Any ideas/advises? since I would like to use specific pre defined Data::Dumper, which doesn't recognize the FILE.
        Add parentheses to disambiguate.

        Also, are you sure it shouldn't rather be

        print FILE Data::Dumper::Dumper( \%tmp_hash );

        ?

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        The following open using last version: open(FILE,"> tmp.pm");

        Please remember to use three argument open, see open for details

        Also use lexicals (my $file) instead of globals (FILE)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://909431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found