Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

pointing a filehandle at a scalar or array ?

by lindex (Friar)
on Jul 26, 2000 at 13:47 UTC ( [id://24435]=perlquestion: print w/replies, xml ) Need Help??

lindex has asked for the wisdom of the Perl Monks concerning the following question:

This may sound like a pretty dumb question buts its something
that I have been wondering about. Is it possible to point
a glob at a scalar or array.

Like if I wanted todo maybe:

$string = "this is a test"; # ... code to point *FD at $string ... while(<FD>) { print $_,"\n"; }
So basicly reading $string like a filehandle.

P.S. my reason behind this is, Iam sick of
some modules forcing you to pass a filehandle to a method in
order for it to read the data its processing, so I want to trick
it, Example: MIME::Decoder

Replies are listed 'Best First'.
Re: pointing a filehandle at a scalar or array ?
by davorg (Chancellor) on Jul 26, 2000 at 14:07 UTC

    You could do something using tie. The docs for tie in 5.005_03 cover tieing filehandles but it's not mentioned in Perl In A Nutshell so I'm not sure when this support was added.

    Update:
    In fact there seems to be a Tie::Handle module that would serve as a base class for this. It's part of the standard distribution.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
      Besides all other answers to this question, I'm desperate to see how this is done. I've blown 10 minutes of my life (so far) trying to figure out how to use Tie::Handle. If I figure it out I'll certainly post it here, but I'd rather that someone else figure it out and tell me. :)

      update: I think I got it! That bugged the s**t outta me for 30 minutes. Learn something new every day eh?

      #!/usr/bin/perl my $damn_string = "This is so cool...\nThis is less cool.\nThis sucks. +\n"; tie *H, 'str', $damn_string; while(<H>) { print "$_\n"; } package str; use Tie::Handle; my @a; sub TIEHANDLE { my $i = bless \$i, shift; @a = split "\n", shift; return $i; } sub READLINE { return shift @a; }
RE: pointing a filehandle at a scalar or array ?
by merlyn (Sage) on Jul 26, 2000 at 16:47 UTC
Re: pointing a filehandle at a scalar or array ?
by athomason (Curate) on Jul 26, 2000 at 14:18 UTC
    If the data you want to pass is static (which I doubt, since there's little sense in using MIME::Decoder for static data), you could stick it at the end of your source after the __DATA__ line and just pass around the *DATA filehandle. A quick hack that is possibly more flexible would be to pass *STDOUT as the input for whatever function you're dealing with and then print to it. Actually, that sounds too funky, but it's late and I can nail down why (blocking issues?). As davorg mentioned above, your best bet is probably looking into using tie for dynamic data.
      Well I was thinking about something like that, maybe define a null filehandle then write to it and pass it :\, I dunno



      lindex
      /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/
RE: pointing a filehandle at a scalar or array ?
by jimt (Chaplain) on Jul 26, 2000 at 20:05 UTC
    This is interesting, since I've noticed a lot of people grumbling about this with modules that require globs (one of mine included). The true wizards can always overload a filehandle, using Tie::Handle if necessary. But what I was wondering is if anyone thinks it'd be worthwhile for me (or someone else, I suppose) to whip up a module to wrap up all of this overloading stuff into one convenient package. Say....Tie::UniversalHandle or something. So you could just do: tie *FH, Tie::UniversalHandle, $some_string; or tie *FH, Tie::UniversalHandle, @some_array; and have all of the magic immediately wrapped up insde of the module. It'd spare people the trouble of having to roll their own module every time.
      When faced with this sort of problem, I've found it useful to just drop back one level of indirection and use subroutine references:

      my $gimme = $fh ? sub { <$fh> } : sub { pop @array }; ... while (defined($x = &$gimme)) { ... }
A thnx to all of you
by lindex (Friar) on Jul 27, 2000 at 03:59 UTC
    I just wanted to thank all of you guys for some many reply's
    to this question, It did in fact help me figure out my problem
    and here is an example (in poetry) of my solution :)
    Haiku


    lindex
    /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found