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 second most certainly applies:

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"

In reply to Indirect Object Syntax Tomfoolery by larryk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.