Perl6 YAML: Dumper module usage details

Source: Internet
Author: User
Tags regular expression sqlite

Since we have a Star package, we need to compile panda and other tools first after installing Rakudo on MoarVM. In this step, you need to pay attention to your @ * inc path and the actual $ PERL6LIB PATH. The $ PATH in panda after compilation is correct. If not, modify it ~ /. Bashrc.

My attempt to migrate objects is a very simple Puppet ENC script that only involves SQLite reading and YAML output. Run the panda install DBIish command to install the DBIish module.

The script itself is not difficult to modify. The result is as follows:

The code is as follows: Copy code
#! /Usr/bin/env perl6
Use v6;
Use DBIish;
Use YAML;
My $ base_dir = "/etc/puppet/webui ";
# Functions are still defined using the sub keyword in Perl6, but the cool feature is multi sub.
# It is not used in the script, but it is everywhere in YAML: Dumper. Here we also provide a sentence.
# In Perl6, the MAIN function can directly use the $ opt command parameter to play the role of getopt.
# However, the ENC script directly transmits a host name and does not use this cool feature.
Sub MAIN ($ node ){
# The receiving parameter option of the connect method is | % opts, so you can directly write the hash
# This | usage I saw in Using Perl6 a month ago
My $ dbh = DBIish. connect ('sqlite ', database => "{$ base_dir}/node_info.db ");
My $ Something = $ dbh. prepare ("select * from node_info where node_fqdn =? ");
Optional sth.exe cute ("$ node ");
My $ ret = $ Something. fetchrow_hashref;
My $ res;
If (! $ Ret ){
$ Res = {
# The qw () of Perl5 is written as <> in Perl6. You do not need to use [] to specify a reference.
Classes => <puppetd repos>,
Environment => 'testing ',
};
    }
Else {
$ Res = {
Environment => $ ret {'environment '},
Parameters =>{ role =>$ ret {'role '}},
Classes => {},
};
# This for usage is used in the Perl5 Text: using ate template.
For split (',', $ ret {'class'})-> $ class {
If ($ class eq 'nginx '){
# The <== symbol indicates the direction of the data stream. You can reverse the array and then use ==> to write this line.
# If you are not used to this type of flow operator, you can use the number. You cannot write anything like Perl5.
# The strange thing here is that I tried to divide such a long sentence into multiple lines for writing, including adding a line to the back of each line. I saw that the YAML code used a branch, but I will report an error.
# The regular expression changes greatly in Perl6. Here/^ #/should be written as/^ '#'/or/^ x23/
# Regular expression // no m is added before. // The matching will not start immediately.
# The Original s // g can be used to write s: g // or object-based. subst (m //, '',: g ),. the default $ _ is empty _
# The captured data exists in the @ () array and can also be obtained in the form of $/[I]
# During string interpolation, $ {*} is not written, but in the form of {$ *}.
# Naming capture is useless here. Write an example:
# $ Str ~~ /^ (W + ?) $ <Laststr >=( w ** 4) w $ /;
# $/<Laststr>. chomp. say;
# Note that w {4} is changed to w ** 4
My @ needs <= map {. subst (m/^ (. +) :( d +) $/, "{$/[0]} max_fails = 30 weight = {$/[1]}",: g )} <= grep {! M/^ x23/} <= split (',', $ ret {'extstr '});
$ Res {'classe'} {'nginx'} {'iplist'} = @ needs;
            }
Else {
# Perl5 undef is no longer used. You can use Nil or Any object.
$ Res {'classes '} {$ class} = Nil;
            }
        }
};
$ Dbh. disconnect ();
# This dump is the function exported by the YAML module.
# The Perl6 module can use our sub dump ($ obj) {} directly to export functions without Exporter.
Say dump ($ res );
};

However, the trouble is that the YAML module itself, which was replaced by ingydotnet a few years ago, is not enough to handle it. Actually, it cannot run at all. It took me half a day to learn and modify the changes. This mainly involves Attribute objects, Nil objects, and twigls prefixes. exists defines several concepts and the processing logic of the YAML format.

The YAML module is modified as follows:

The code is as follows: Copy code

Diff -- git a/lib/YAML/Dumper. pm B/lib/YAML/Dumper. pm
Index d7a7981 .. ec47341 100644
--- A/lib/YAML/Dumper. pm
++ B/lib/YAML/Dumper. pm
@-2, 16 + 2, 16 @ use v6;
Class YAML: Dumper;
Has $. out = [];
-Has $. seen is rw = {};
+ Has $. seen = {};
Has $. tags = {};
Has $. anchors = {};
Has $. level is rw = 0;
-Has $. id is rw = 1;
+ Has $. id = 1;
Has $. info = [];
Method dump ($ object ){
$. Prewalk ($ object );
-$. Seen = {};
+ $! Seen = {};
$. Dump_document ($ object );
Return $. out. join ('');
 }
@-45, 11 + 45, 11 @ method dump_collection ($ node, $ kind, $ function ){
Method check_special ($ node ){
My $ first = 1;
-If $. anchors. exists ($ node. WHICH ){
-If $. anchors. exists ($ node. WHICH ){
+ If $. anchors {$ node. WHICH}: exists {
Push $. out ,'','&'~ $. Anchors {$ node. WHICH };
$ First = 0;
     }
-If $. tags. exists ($ node. WHICH ){
+ If $. tags {$ node. WHICH}: exists {
Push $. out ,'','! '~ $. Tags {$ node. WHICH };
$ First = 0;
     }
@-64, 7 + 64, 7 @ method indent ($ first ){
Return;
         }
If $. info [*-1] <kind> eq 'seq '& $. info [*-2] <kind> eq 'map '{
-$ Seq_in_map = 1;
+ $ Seq_in_map = 0;
         }
     }
Push $. out, "n ";
@-155, 7 + 155, 8 @ method dump_object ($ node, $ type ){
$. Tags {$ repr. WHICH} = $ type;
For $ node. ^ attributes-> $ {
My $ name = $ a. name. substr (2 );
-My $ value = pir: getattribute _ PPs ($ node, $ a. name); # RAKUDO
+ # My $ value = pir: getattribute _ PPs ($ node, $ a. name); # RAKUDO
+ My $ value = $ a. get_value ($ node); # for non-parrot
$ Repr {$ name} = $ value;
     }
$. Dump_node ($ repr );
$. Seen and $! Is seen lost? In fact, $. seen is equivalent to declaring $ First! Then, a method seen () {return $! is automatically created! Seen }.

The other is the pir: getattribute _ PPs () function. pir is the language on parrot, while MoarVM and JVM first implement a nqp and then use nqp to write Perl6, unfortunately, the getattribute _ PPs () in this pir does not yet have the corresponding nqp method. (Visible in the pir2nqp. todo file)

Therefore, you can only use the advanced Perl6 language.

In general, this yaml-pm6 code is a lot of places to try, the same effect of different writing, such as. WHICH and. WHAT. perl is also mixed. I also tested it at will. Even on parrot, pir: getattribute _ PPs is faster than Attribute. get_value.

At present, the operating time of the ENC script on perl5, perl6-m, perl6-p, perl6-j is about 0.13, 1.5, 2.8, 12 s. MoarVM is 10 times worse than Perl5, and twice ahead of parrot. However, it takes a long time to start the JVM itself. This is not good because a short script says it is too slow.

In addition, I also tried to write the modified YAML: Dumper class directly in the script to run it, that is, not compile it into the moarvm module. The time is about 2.5 s, it is a little faster than the parrot module.

But how to compile the perl6 script itself into the bytecode format of moarvm run has not been studied, direct perl6-m -- target = mbc -- output = name. moarvm name. run moar name in the file obtained in pl6. when moarvm runs, memory errors are reported.

Original article from: tiejiang.org

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.