Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

how to name an anonymous reference

by omkellogg (Novice)
on Dec 17, 2017 at 21:59 UTC ( #1205752=perlquestion: print w/replies, xml ) Need Help??

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

Let there be a small language for creating annotations. (The actual case in point is section 7.4.15 in OMG IDL4, http://www.omg.org/spec/IDL/4.1/PDF).

Some annotations are predefined. In order to support both predefined annotations and user defined annotations, I would use an array, e.g.:

sub ULONG () { 9 } sub ENUM () { 23 } my @annotations = ( [ "id", [ ULONG, "value", undef ] ], [ "autoid", [ ENUM, "AutoidKind", [ "SEQUENTIAL", "HASH" ] ], [ $annotations[1]->[1], "value", "HASH" ] ] );

The idea is that the array is initialized with the predefined annotations, and user defined annotations are parsed and appended onto the same array later on.

In the example above, the predefined annotation autoid first defines an enum type AutoidKind with values SEQUENTIAL and HASH, and then shall define a parameter named value of the enum type just defined.

The notation $annotations[1]->[1] for referencing the enum type does not work - probably because the declaration @annotations is still in progress at the point of reference.

Perhaps there is a different way to achieve what I am trying to do?

Thanks.

Replies are listed 'Best First'.
Re: how to name an anonymous reference
by ikegami (Pope) on Dec 18, 2017 at 02:10 UTC
    sub ULONG () { 9 } sub ENUM () { 23 } my $AutoidKind = [ ENUM, "AutoidKind", [ "SEQUENTIAL", "HASH" ] ]; my @annotations = ( [ "id", [ ULONG, "value", undef ] ], [ "autoid", $AutoidKind, [ $AutoidKind, "value", "HASH" ] ], );
      Yes, this is very close to what I was looking for. In this particular language, enum type declarations are local to their enclosing annotation declarations; therefore I will go with http://www.perlmonks.org/?node_id=1205756
Re: how to name an anonymous reference (dictionary/hash)
by beech (Parson) on Dec 18, 2017 at 03:36 UTC

    Perhaps there is a different way to achieve what I am trying to do?

    Hi,

    Maybe you can use a dictionary of types?

    my %type; $type{ENUM} = 9; # :O ?? ;) $type{ULONG} = 23; $type{IdKind} = [ $type{ULONG}, "value", undef ]; $type{AutoIdKind} = [ $type{ENUM}, "AutoidKind", [ "SEQUENTIAL", "HASH +" ]]; my @annotations = ( [ id => $type{IdKind}, ], [ autoid => $type{AutoIdKind}, [ $type{AutoIdKind}, value => 'HASH' ] +], ); dd \%type, \@annotations; __END__

    ???

    AddAnnotation( autoid => Type('AutoIdKind'), [ Type('AutoIdKind'), val +ue => 'HASH' ] );
Re: how to name an anonymous reference
by Anonymous Monk on Dec 17, 2017 at 22:09 UTC
    sub ULONG () { 9 } sub ENUM () { 23 } my @annotations = ( [ "id", [ ULONG, "value", undef ] ], [ "autoid", [ ENUM, "AutoidKind", [ "SEQUENTIAL", "HASH" ] ], [ 'fixme', "value", "HASH" ] ] ); $annotations[1][2][0] = $annotations[1][1]; use Data::Dump; dd @annotations; __END__ do { my $b = [ "autoid", [23, "AutoidKind", ["SEQUENTIAL", "HASH"]], ['fix', "value", "HASH"], ]; $b->[2][0] = $b->[1]; (["id", [9, "value", undef]], $b); }
    but that's ugly - XY Problem?
      Oops, I guess I posted too early - there is a simple workaround:
      my $ref; # auxiliary to declaring @annotations my @annotations = ( [ "id", [ ULONG, "value", undef ] ], [ "autoid", $ref = [ ENUM, "AutoidKind", [ "SEQUENTIAL", "HASH" +] ], [ $ref, "value", "HASH" ] ] );
      Sorry for the noise.
Re: how to name an anonymous reference
by LanX (Archbishop) on Dec 17, 2017 at 22:30 UTC

      It's not circular.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2020-01-21 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?