Pacific-Design.com

    
Home Index

1. Perl5

2. Serialization

+ JSON

+ YAML

Perl5 / Serialization /

Extended processing of command line options and Serialization

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;

#-------------------------------------------------------------------------#
use Getopt::Long;
use YAML::Tiny;
$|++;
use vars qw( %opts );

#-------------------------------------------------------------------------#

my @getopt_args = ( 'task=s', 'h' );
GetOptions( \%opts, @getopt_args );

my $menu = <<EOT;
Usage:  [-options]
  -task    Task
  -h       Help Menu
EOT

if ( not exists $opts{task} ) { print $menu; exit; }

display_one_yaml($opts{task});

print "-----------------------------------------------\n\n";

display_all_yaml();

#-------------------------------------------------------------------------#
sub display_one_yaml {

    my($pos) = shift;
    my $yaml = YAML::Tiny->new;
    $yaml = YAML::Tiny->read('metadata.yml');

#<<<
    print 'position  = ' . $yaml->[$pos]->{position} . "\n"
        . 'def_rank  = ' . $yaml->[$pos]->{def_rank} . "\n"
        . 'def_hits  = ' . $yaml->[$pos]->{def_hits} . "\n"
        . 'myhealth  = ' . $yaml->[$pos]->{myhealth} . "\n"
        . 'mysocial  = ' . $yaml->[$pos]->{mysocial} . "\n"
        . 'sequence  = ' . $yaml->[$pos]->{sequence} . "\n"
        . 'berkeley  = ' . $yaml->[$pos]->{berkeley} . "\n"
        . 'new_link  = ' . $yaml->[$pos]->{new_link} . "\n\n";
#>>>
    return;
}

#-------------------------------------------------------------------------#
sub display_all_yaml {

    my $yaml = YAML::Tiny->new;
    $yaml = YAML::Tiny->read('metadata.yml');

    foreach my $y ( @{$yaml} ) {

#<<<
    print 'position  = ' . $y->{position} . "\n"
        . 'def_rank  = ' . $y->{def_rank} . "\n"
        . 'def_hits  = ' . $y->{def_hits} . "\n"
        . 'myhealth  = ' . $y->{myhealth} . "\n"
        . 'mysocial  = ' . $y->{mysocial} . "\n"
        . 'sequence  = ' . $y->{sequence} . "\n"
        . 'berkeley  = ' . $y->{berkeley} . "\n"
        . 'new_link  = ' . $y->{new_link} . "\n\n";
    }
#>>>
    return;
}

#-------------------------------------------------------------------------#


metadata.yml

---
position : 0
def_rank : 0
def_hits : 0
myhealth : engine50
mysocial : engine60
sequence : /home/spider/data/sequence0.dat
berkeley : /data1/logs/g0.bdb
new_link : /data1/logs/1/g0.log
---
position : 1
def_rank : 10
def_hits : 10
myhealth : engine50
mysocial : engine60
sequence : /home/spider/data/sequence1.dat
berkeley : /data1/logs/g1.bdb
new_link : /data1/logs/1/g1.log
---
position : 2
def_rank : 20
def_hits : 20
myhealth : engine50
mysocial : engine60
sequence : /home/spider/data/sequence2.dat
berkeley : /data1/logs/g2.bdb
new_link : /data1/logs/1/g2.log