Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear fellow Monks,
I have a parser that is working just fine and uses FileHandle for its input. I need to keep the same functionality, taking input from a string instead of a file.
I thought about writing a separate sub for this purpose, but I realized that a considerable part of my program architecture had to be changed. Therefore I tried another approach, i.e. fooling the application by giving it a handle that behaves like a text file, but has a string underneath.
I couldn't find any ready-to-use solution, and so I came up with my homemade package (see below).
The StringHandle package overloads the angle operator, returning an element of the internal array, which was created by the constructor.
The returned element is removed from the array, thus imitating a sequential file reading.

Also, the code requires that the newline is kept at the end of the returned string, again for compatibility with a file input. On the technical side, the best split pattern that I found was /^/m, which returns a complete string.
A last touch of imitation was the "close" sub, which I would call for a FileHandle object, and should be in my bogus package as well, if I want to avoid an error.
#!/usr/bin/perl -w use strict; #----------------------- package StringHandle; use overload '<>' => sub { return shift @{$_[0]}; }; sub new { my $class = shift; return bless [split /^/m, $_[0]], $class; } sub close { # does nothing. Just to emulate FileHandle }; #----------------------- package main; use FileHandle; my $param = shift; my $text = undef; if ($param) { $text = new FileHandle $param || die "can't open $param\n"; } else { $text = new StringHandle "aaa\nbbb\nccc\nddd\n" || die "can't create new handle\n"; } print while <$text>; # reads from either a file or a string $text->close;
Having this StringHandle package, my script can read from either a file or a string, with the same functionality and no visible problems.
Thus, my requests:
  1. Could you think of any side effect that might affect my parser if I use this emulation?
  2. Is there any module with a ready-to-use solution?
  3. Currently, instead of using eof, I am checking if $_ is defined. How can I increase the robustness of this package to fool the eof function as well?
(Please notice that for this parser I don't need other functions such as seek and tell. Also, the string handle is not publicly interfaced. Therefore I shouldn't worry about emulating FileHandle any further.)

TIA
 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to FileHandle emulation by gmax

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-09-23 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?