Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Accessing the __DATA__ blocks of other packages

by pfaut (Priest)
on Dec 11, 2002 at 22:03 UTC ( [id://219193]=note: print w/replies, xml ) Need Help??


in reply to Accessing the __DATA__ blocks of other packages

Why not have the calling code pass a reference to its DATA handle instead?

#Used.pm package Used; use strict; 1; sub called { my $fh = shift; my $data = <$fh>; print "Used::called read: $data\n"; } #User.pl #!/usr/bin/perl -w use strict; use Used; Used::called(\*DATA); __DATA__ This is a test

BTW, is there a better way to pick up the DATA handle reference?

Replies are listed 'Best First'.
Re: Re: Accessing the __DATA__ blocks of other packages
by caedes (Pilgrim) on Dec 11, 2002 at 22:19 UTC
    I originally had the calling code slurp the __DATA__ into a scalar and then pass that to the other package's subroutine, however I don't want to have to do this for every package that wants to call the package that uses __DATA__.

    -caedes

      I would say that what you are doing is probably not a very good software design. If you need something from your caller, it's probably a better idea to have the caller pass it as an argument. That way, it's documented that the called routine is using it. Using backdoor methods such as this to get information from the caller leads to unmaintainable code. Make sure you document this behavior so that anyone that uses this code in the future knows their DATA handle is going to be used when they call this method.

      With that said, if you really need to do it without modifying the calling code, try this.

      sub called { my $caller = (caller)[0]; my $fh = eval '\*'.$caller.'::DATA'; my $data = <$fh>; print "read: $data\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found