larryk has asked for the wisdom of the Perl Monks concerning the following question:
There is a module that lets me access a scalar variable as if it were a file: Tie::Handle::Scalar but I can't seem to find one to do the opposite - access a file as if it were a scalar variable.
The reason I want to do this is that I was looking at YAPE::HTML and it wanted a string containing HTML to parse and I had an HTML file. If I could get YAPE::HTML to think it had a string but was really accessing my file then I wouldn't have to slurp it in (not such a big problem for relatively small files but Out of memory! errors lurk around every corner).
After a while in the CB I couldn't find a suitable wheel. I looked at perldoc perltie but it won't let me override the core string functions. I decided the only way was to create an object and somehow force any string ops to use the object's methods - fine if I am writing the script as I can say $pseudo_string->substr(0,10) but not so fine if I am trying to fool A::N::Other package into thinking it has got a real scalar variable that it can do substr($pseudo_string,0,10). I read the WARNING section in perldoc perlobj about indirect object syntax and while my proposed solution does not suffer from the first problem:
The first problem is that an indirect object is limited to a name, a scalar variable, or a block, because it would have to do too much lookahead otherwise..., the second most certainly applies:
As if that weren't bad enough, think about this: Perl must guess *at compile time* whether "name" and "move" above are functions or methods. Usually Perl gets it right, but when it doesn't it, you get a function call compiled as a method, or vice versa.
I made a start (see code below) but the only way I can substr my $pseudo_string using indirect syntax is by fully qualifying the method name: Pseudo::Tie::Scalar::Handle::substr $pseudo_string, 10, 20. Which defeats the purpose if I have to then modify A::N::Other package.
Am I barking up the wrong tree? Or just barking? How can I force the indirect syntax of A::N::Other package to call the $pseudo_string's methods instead of core functions?
package Pseudo::Tie::Scalar::Handle; use strict; use warnings; use Fcntl; use IO::File; sub new { my($class,$file) = @_; my $fh = IO::File->new($file,O_RDWR|O_CREAT); return unless defined $fh; bless { file => $file, handle => $fh, }, $class; } sub AUTOLOAD { no strict 'vars'; warn "$AUTOLOAD not implemented for a Pseudo::Tie::Scalar::Handle +object\n"; } sub DESTROY {} sub substr ($$;$$) { my $self = shift; my $fh = $self->{handle}; seek $fh, shift, 0; my $size = shift; my $chunk; if (defined $size) { warn "substr replacement not implemented\n" if @_; read $fh, $chunk, $size; } else { $chunk = do { local $/; <$fh> } } return $chunk; } 1; package main; use strict; use warnings; my $pseudo_string = Pseudo::Tie::Scalar::Handle->new('/test.txt'); print "\nnot-working: ",substr $pseudo_string, 0, 43;
larryk perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: Indirect Object Syntax Tomfoolery
by tilly (Archbishop) on Feb 13, 2002 at 13:57 UTC | |
|
Re: Indirect Object Syntax Tomfoolery
by japhy (Canon) on Feb 13, 2002 at 15:04 UTC | |
|
(Zaxo) Mmap Re: Indirect Object Syntax Tomfoolery
by Zaxo (Archbishop) on Feb 14, 2002 at 04:45 UTC |