Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Encapsulate function and variables

by nictam (Initiate)
on Jun 19, 2007 at 11:34 UTC ( [id://621992]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
here is what I'd like to be able to implement:
- creates variables and/or functions in file foo.pl. e.g.:
$text = "foo"; sub func { return "something"; } ... ...

- now I would like to load this file so that its content is encapsulated either in a hash table or in an object. E.g. in file main.pl, I would like to use a function such as capsule that would create such object:
$obj = capsule("foo.pl");
so that I can now access $obj{"text"}, or $obj->text... I could also call $obj->func().

- in general there would be many different files such as foo.pl, and I want to access all variables from a top-level script.
In fact I want to make all declarations from foo.pl which seem to be global, as local from the top level script. I don't want to use different namespaces.

thanks in advance! nictam

Replies are listed 'Best First'.
Re: Encapsulate function and variables
by blazar (Canon) on Jun 19, 2007 at 11:53 UTC
    - now I would like to load this file so that its content is encapsulated either in a hash table or in an object. E.g. in file main.pl, I would like to use a function such as capsule that would create such object:

    Smells strongly like an XY problem, but you can require your file. Then if its code is within an own package, the latter's stash will be filled with variables and functions. (If you want -and you have to- stay under strict, then use our for the declarations.) Then you can map (perhaps by means of an actual map) that stash into a generic hash. Very clumsy and no reason why one would want to do that, if you ask me, but certainly doable. If you want to have object access, instead, it's enough that you enclose again the code of your file in a package declaration and that obj is blessed into that package. Just very few lines of code to add. Of course this won't also make for automatic access to $obj->text out of a bare $text package variable, but if you really really want that you will need to implement you accessor creating logic mangling the symbol table: again, no really good reason for doing so that I can imagine of.

Re: Encapsulate function and variables
by gaal (Parson) on Jun 19, 2007 at 11:46 UTC
    For most practical purposes, you can treat a namespace exactly as if it were a hash.

    sub capsule { my($capsule_name, $file) = @_; my $src = slurp $file; eval "package $capsule_name; \n#line 1\n$src" or die $@; } [...] capusle("Foo", "foo.pl"); print $Foo::{text};

    An alternative is to load everything into a dummy package with a throwaway name, then to export everything out of it. Of course, both these proposals break if foo.pl switches namespaces itself, and that's hard to catch.

Re: Encapsulate function and variables
by guinex (Sexton) on Jun 19, 2007 at 12:05 UTC
    First, it's not clear to me why you would want to do this.

    Second, I think you probably don't really want to do this :).

    Are you sure you just don't want a plain ol module?

    -guinex

Re: Encapsulate function and variables
by clinton (Priest) on Jun 19, 2007 at 12:57 UTC
    Am I right in saying that you are wanting to specify static variables (as in configuration data) as Perl code, but contained in a number of different files?

    You can use Config::Loader Config::Merge with your data contained in different files, in a directory tree. Config::Loader Config::Merge will do the work of loading them into a hash.

    Or are you wanting to be able to access the same data via a hash deref or via a function call, in which case you want to use overload.

    Clint

    Update: Changed the module name to its new name

      Clint, thanks.
      what I want to do is in fact almost what you describe. I have a set of configuration files that define the same variables, and I need to be able to load and access all of them.
      I will investigate the use of Config::Loader.
      cheers, nictam
Re: Encapsulate function and variables
by Anonymous Monk on Jun 19, 2007 at 11:44 UTC
    I don't think perlmonks help create nightmares

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found