Wiggins D'Anconia wrote:
>
> Dan Anderson wrote:
>
> > I have a module that works with a couple of different file handles.  Is
> > it possible to hide them within an anonymous hash?  {} (i.e. the objects
> > data).  Right now I have:
> >
> > if (condition_is_met()) {
> >   open("FILE","<file");
> > }
> >
> > This is in the main body of my package (and thus, I assume, accessible
> > by all functions).  Is it better to use tighter encapsulation (i.e. a
> > closure or throwing it into an anonymous hash) or just to leave it in
> > the body?
> >
>
> Depending on your Perl version you can store filehandles to a lexical, like:
>
> my $FILEHANDLE;
> open($FILEHANDLE,...) or die ....;

This form puts a reference to a glob value into the scalar variable.
The only entry used in that glob value is its I/O handle.

> And then presumably you can store your lexical whereever you want. It
> is my understanding that when you reference it, it must be in a simple
> scalar, in which case you will have to pull it out of your hash reference
> before using it.  For example:
>
> print $FILEHANDLE 'foo';  works but,
>
> print $hashref->{'filehandle'} 'foo';  doesn't.  but,

You can use any expression here which evaluates to a glob value
or a reference to one. But f you use anything other than a simple
scalar variable then you need to put it into a block.

  print { $hashref->{filehandle} } 'foo'

will work fine. (And by the way you don't need those quotes in
a hash reference unless it has any non-word ( /\W/ ) characters
in it.

> my $newlocation = $hashref->{'filehandle'};
> print $newlocation 'foo';  should...
>
> Gurus please correct me on this if I am wrong!!!
> And this depends on the way in which you want to use it.
>
> Again depending on your preference you could look at using the object classes for
> IO:: to handle all of this at which point you can store the object whereever you
> like, like any other Perl object.

Cheers,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to