Perl: How to create objects on the fly? -
My goal is to be able to use the $ obj
like this:
Print $ obj- & gt; Hello () $ Obj- & gt; {Foo};
and I would like to make an object inline, possibly as this:
my $ obj = (foo) = & gt ; 1, hello = & gt; Sub (return "world"});
But when I try to use $ obj
as an object, I get an error saying that
For those people who know Javascript, I am trying to do the following, but in Perl:
# Obj = {foo: 1, hello: function () {returning 'world'}} below # js code; Echo obj.hello () + obj.foo;
Pearl will need some help in doing this. Since it does not consider stored code references in a hash as "methods". Methods are applied as entries in a package symbol table. Perl is more class-oriented than Javascript, which proudly declares that it is more of the object-oriented ( individual ) objects)
To do that functionality, you must create a category that is mapped in that context. Method of how to get the methods in the symbol table is the AUTOLOAD
method. If a package contains a autolide
sub-routine, when the call is made for a blessed object that Pearl can not find in the heritage series, then it will be AUTOLOAD
And will set up a package-scotch ( our
) variable $ AUTOLOAD
will have the name full .
We get the name of the method that is said to get the last node (after the last '::') of the fully qualified sub-name. We see that there is a coderef at that place, and if there is, we can return it.
Package auto object; Strict use; Use warnings; Use carp; Params :: Util QW & lt; _CODE & gt ;; Use; Our $ Atolled; Sub-otolyoid {my $ method_name = substr ($ autolode, index ($ autoloada, '::') + 2); Mine ($ self) = @_; My $ Math = _CODE ($ self-> {$ method_name}); Unless ($ meth) {carp :: crock ("object does not support method = '$ method_name'!);} Goto and $ meth;} 1;
In that class, bless the object:
package main; my $ obj = bless {foo = & gt; 1, hello => sub (returns' world ')},' AutoObject '; print $ obj-> Hello ();
Generally, in a AUTOLOAD
sub I in "cement" behavior, i.e., next To avoid the bar AUTOLOAD
created entries in the package symbol table But this is usually for a defined class behavior.
I have also designed a QuickClass
, which creates a package for each object, but there are a lot of Symbol table is lengthening, which is probably better with days
Given the suggestion given by Eric Storm, you can add the following code to the AutoObacket package. import
the sub at any time AutoAbstract
(with parameter 'object'
).
# Definition: Subject ($) {Return $ _ [0], __PACKAGE__; }; Sub-import {# is called when Pearl uses 'AutoAbject;' Shift; # Return my name until $ _ [0] eq 'object'; # Objects This is the only export usage mark; * {Symbol :: qualification_to_reference ('object', scalar collar ())} = \ & object; }
And then, when you wanted to make "object official", you could do this:
auto-object qw
And the expression will be:
object {foo = & gt; 1, hello = & gt; Sub (Return 'world')};
You can also do this:
object {name = & gt; 'world', hello = >
and you have an "object" Verbatim "expression is probably the module will be called the better object :: Literal
Comments
Post a Comment