in reply to How can read form __DATA__ via filehandler

The DATA filehandle is associated with a given file and is automatically created when the __DATA__ token exists in a script e.g
package foo; sub getdata { return *DATA } 1; __DATA__ this data is in foo
Now in the main script
use foo; my $d = foo->getdata; print "foo's data - ", <$d>; print "main's data - ", <DATA>; __DATA__ this data is in main
And the output is
foo's data - this data is in foo main's data - this data is in main
As you can see, if you just reference it (and the __DATA__ token exists) then perl does the right thing (of course :).
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: How can read form __DATA__ via filehandler
by filipe (Novice) on May 21, 2003 at 15:37 UTC
    Well, the two solutions worked just fine if you use the diamond operator
    and i've just dig a few lines of code in the HTML::Template and found that the above i've posted worked fine also, seems that it's a bug
    on the module that dosen't handle filehandlers properly (i've run a quick test). But thanks anyway, for the brief answers :=) I'll let the author know that and maybe try to fix-it if i got spare time. Cheers Filipe
      Bug in HTML::Template? What are you talking about? No offense, but something tells me that you are overlooking something on your side and you are thinking that it is a bug in that module (at the most i wager it's a feature that samtregar never intended HTML::Template to do). Abigail-II's solution is right on ... nobody says that you ever need use the <> operator to pass a package's DATA filehandle around (and it is filehandle by the way, not filehandler ... you aren't handling a file, you are getting a handle on the file). Here is some code to demonstrate:

      We call a package's (Foo) subroutine which returns an object to us (Bar). Bar contains one 'attribute' whose value is a reference to Foo's DATA filehandle. Back inside main we instantiate a new HTML::Template and pass Bar's 'filehandle' it to (which is really a reference to Foo's). We then pass it a param for it to substitute and then we print the output. Here goes:

      use strict; use warnings; use HTML::Template; my $bar = Foo::do_it(); my $tmpl = HTML::Template->new(filehandle => $bar->{filehandle}); $tmpl->param(baz => 'Hello GLOB!'); print $tmpl->output(); package Bar; sub new { my $class = shift; return bless {@_}, $class; } package Foo; sub do_it { my $bar = Bar->new(filehandle => \*DATA); return $bar; } __DATA__ <html> <body> <h1><tmpl_var baz></h1> </body> </html>
      Finally note, i tend to use DATA only for tests and example scripts - i avoid it when it comes to writing real production code. Use seperate files instead. I guess if you have each page associated with one and only one module it would make sense ... but i tend to associate multiple pages with each module (add, edit, view, delete, list all, etc.) ... Inline::Files? Nah! ;)

      Best of luck to you and please let us know how your progress goes. :)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)