Mercurial > repos > cpt > cpt_psm_plotter
comparison lib/CPT/Util.pm @ 1:8691c1c61a8e draft default tip
planemo upload commit 94b0cd1fff0826c6db3e7dc0c91c0c5a8be8bb0c
author | cpt |
---|---|
date | Mon, 05 Jun 2023 02:48:47 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:54c7a3ea81e2 | 1:8691c1c61a8e |
---|---|
1 package CPT::Util; | |
2 use strict; | |
3 use warnings; | |
4 use Moose; | |
5 | |
6 #ABSTRACT: CPT convenience functions | |
7 | |
8 | |
9 | |
10 sub JSONYAMLopts { | |
11 my ( $self, %data ) = @_; | |
12 my %hash; | |
13 if ( $data{'file'} ) { | |
14 my $ext = substr($data{'file'}, rindex($data{'file'}, '.') + 1); | |
15 if ( lc $ext eq 'yaml' || lc $ext eq 'yml' ) { | |
16 require YAML::XS; | |
17 %hash = %{ YAML::XS::LoadFile( $data{'file'} ) }; | |
18 } | |
19 elsif ( lc $ext eq 'json' ) { | |
20 require JSON::XS; | |
21 require File::Slurp; | |
22 my $json = File::Slurp::read_file( $data{'file'} ); | |
23 %hash = %{ JSON::XS::decode_json($json) }; | |
24 } | |
25 else { | |
26 confess "Requested JSON/YAML file lacked a recognisable suffix $ext"; | |
27 } | |
28 } | |
29 else { | |
30 confess 'Error, no options provided'; | |
31 } | |
32 return \%hash; | |
33 | |
34 } | |
35 | |
36 | |
37 | |
38 sub untaint_path { | |
39 delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' }; | |
40 $ENV{'PATH'} = '/bin:/usr/bin'; | |
41 my $path = $ENV{'PATH'}; | |
42 return 1; | |
43 } | |
44 | |
45 | |
46 | |
47 no Moose; | |
48 1; | |
49 | |
50 __END__ | |
51 | |
52 =pod | |
53 | |
54 =encoding UTF-8 | |
55 | |
56 =head1 NAME | |
57 | |
58 CPT::Util - CPT convenience functions | |
59 | |
60 =head1 VERSION | |
61 | |
62 version 1.99.4 | |
63 | |
64 =head1 FUNCTIONAL INTERFACE | |
65 | |
66 my $libCPT = CPT::CPT->new(); | |
67 | |
68 =head2 JSONYAMLopts | |
69 | |
70 my %colour_options = %{ | |
71 $libCPT->JSONYAMLopts( | |
72 'file'=>$options{'optionsfile'}, | |
73 'string'=> $options{'optionsstring'} | |
74 ) | |
75 }; | |
76 | |
77 Reads from a file or from a string passed to it describing additional options in JSON or YAML. (Should I support other options?) | |
78 | |
79 For scripts that require significant numbers of input parameters where they are often re-used, it isn't sensible to require people to specify ten flags on the command line. Offering a JSON/YAML file reader simplifies their life by providing re-usable config files. | |
80 | |
81 =head2 untaint_path | |
82 | |
83 $libCPT->untaint_path(); | |
84 | |
85 Convenience function | |
86 | |
87 =head1 AUTHOR | |
88 | |
89 Eric Rasche <rasche.eric@yandex.ru> | |
90 | |
91 =head1 COPYRIGHT AND LICENSE | |
92 | |
93 This software is Copyright (c) 2014 by Eric Rasche. | |
94 | |
95 This is free software, licensed under: | |
96 | |
97 The GNU General Public License, Version 3, June 2007 | |
98 | |
99 =cut |