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

Hy all. My problem is simple, i got a package say
package Foo;
use Bar;

use vars qw/$fd/;

sub do_it {
my $answer = Bar->new(filehandler => $fd);
}
....

1;
__DATA__
data goes in here.


now how can i assign a filehandler to the Foo::DATA file dsecriptor to pass it to another function? it seems that all my solution aren't working.

i've tried IO::File->new_from_fd(Foo::DATA, "<") but it didn't work, and $fd = *{"Foo::DATA"} only works inside of my scope.

I've saw some workarounds but i don't want to modify the other package code to use the <>

Cheers and Thanks in advance
Filipe
  • Comment on How can read form __DATA__ via filehandler

Replies are listed 'Best First'.
Re: How can read form __DATA__ via filehandler
by Abigail-II (Bishop) on May 21, 2003 at 14:40 UTC
    Pass \*DATA, as shown below:
    #!/usr/bin/perl use strict; use warnings; package Bar; sub read_it { my $fh = shift; print while <$fh>; } package Foo; Bar::read_it \*DATA; __DATA__ one two three four

    Abigail

Re: How can read form __DATA__ via filehandler
by broquaint (Abbot) on May 21, 2003 at 14:41 UTC
    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

      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)