Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Ave:

I'm trying to create a module that for all practical purposes "sits astride" an application recording all of the application's input. (For this discussion let us assume that the application is file-wise relatively simple: open a file for reading only or open a file for writing only.)

STDIN is easily handled, but implicitly tying files being opened for input has me stumped. I've started by writing a tie package and overriding CORE::GLOBAL::open (thanks Eliya) with one that performs the tie. In the simplest case, opening one file and reading from it appears to work. Getting it to work when more than one file is opened is another story.

Let's begin with the tying package (call it TieInput.pm)

package TieInput; use Data::Dumper; use strict; use warnings; # TieInput subs { my @In; sub CLOSE { my $self_O=shift; my $fh=$In[$self_O->{fhno}]; close($fh); }; # CLOSE: sub DESTROY { }; # DESTROY: sub OPEN { }; # OPEN: sub READLINE { my $self_O=shift; my $fh=$In[$self_O->{fhno}]; if (wantarray()) { my @_a=<$fh>; warn __PACKAGE__,": ",Data::Dumper->Dump([\@_a],[qw(*_a)] +); return @_a; } else { my $_s=<$fh>; warn __PACKAGE__,": ",Data::Dumper->Dump([\$_s],[qw(*_s)] +); return $_s; }; }; # READLINE: sub TIEHANDLE { my $Class_C=shift; # Point $fh to the real thingie ... open(my $In,"<&",$_[0]); push(@In,$In); return bless { fhno=>$#In }; }; # TIEHANDLE: sub UNTIE { #say __PACKAGE__."::UNTIE<>\n"; my ($self_O,$count_S)=@_; return; }; # UNTIE: # }; # Internal (tie) Subs: BEGIN { # Thanks to Eliya # Currently not checking to insure the file will be opened readonl +y *CORE::GLOBAL::open=sub (*;$@) { #use Symbol(); #my $handle=Symbol::qualify_to_ref($_[0], scalar caller); # #$_[0]=$handle # unless defined $_[0]; # pass up to caller my $handle; if (@_ == 1) { CORE::open $handle or warn $! and return 0; } elsif (@_ == 2) { CORE::open $handle, $_[1] or warn $! and return 0; } elsif (@_ == 3) { if (defined $_[2]) { CORE::open $handle, $_[1], $_[2] or warn $! and return 0; } else { CORE::open $handle, $_[1], undef # special case or warn $! and return 0; }; } else { CORE::open $handle, $_[1], $_[2], @_[3..$#_] or warn $! and return 0; }; tie *$handle,"TieInput",*$handle; }; # CORE::GLOBAL::open }; __PACKAGE__ __END__

Example.pl: perl Example.pl or perl -MTieInput Example.pl

#! use Data::Dumper; use strict; use warnings; { # open one input - works with TieInput warn "opening in1.txt:\n"; open my $in1,"<","in1.txt" or die $!; my $s=<$in1>; warn Data::Dumper->Dump([\$s],[qw(*s)]).' '; my @a=<$in1>; warn Data::Dumper->Dump([\@a],[qw(*a)]).' '; close $in1; }; { # open two inputs - fails with TieInput warn "opening in1.txt and in2.txt:\n"; open my $in1,"<","in1.txt" or die $!; open my $in2,"<","in2.txt" or die $!; my $s=<$in1>; warn Data::Dumper->Dump([\$s],[qw(*s)]).' '; $s=<$in2>; warn Data::Dumper->Dump([\$s],[qw(*s)]).' '; my @a=<$in1>; warn Data::Dumper->Dump([\@a],[qw(*a)]).' '; @a=<$in2>; warn Data::Dumper->Dump([\@a],[qw(*a)]).' '; close $in1; close $in2; }; exit;

With in1.txt

in1 A in1 B in1 C

and in2.txt

in2 A in2 B in2 C

Obviously I've done something wrong! What would be appreciated!


In reply to Implicitly file tying ... by clueless newbie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (9)
    As of 2014-08-30 20:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (293 votes), past polls