| 1 | package ore; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use routines; | ||
| 8 | |||
| 9 | use base 'Exporter'; | ||
| 10 | |||
| 11 | use Data::Dump; | ||
| 12 | use Data::Object::Space; | ||
| 13 | |||
| 14 | no strict 'refs'; | ||
| 15 | |||
| 16 | our @EXPORT = ('dd'); | ||
| 17 | |||
| 18 | our $VERSION = '0.04'; # VERSION | ||
| 19 | |||
| 20 | sub import { | ||
| 21 | my $args = [map { /([^=]+)=(.*)/; $ENV{$1} = $2 if $1; $1 || () } @ARGV]; | ||
| 22 | |||
| 23 | new_vars([grep /^New_/, @$args ? @$args : keys %ENV]); | ||
| 24 | use_vars([grep /^Use_/, @$args ? @$args : keys %ENV]); | ||
| 25 | |||
| 26 | ore->export_to_level(1, @_); | ||
| 27 | } | ||
| 28 | |||
| 29 | fun new_vars($args) { | ||
| 30 | [map new_vars_pump(new_vars_form($_)), @$args] | ||
| 31 | } | ||
| 32 | |||
| 33 | fun new_vars_args($spec) { | ||
| 34 | [map { /^\$(\w+)$/ ? ${"ore::$1"} : $_ } @$spec[2..$#$spec]] | ||
| 35 | } | ||
| 36 | |||
| 37 | fun new_vars_form($name) { | ||
| 38 | new_vars_make(new_vars_spec($name)) | ||
| 39 | } | ||
| 40 | |||
| 41 | fun new_vars_make($spec) { | ||
| 42 | [$spec->[1], Data::Object::Space->new($spec->[0])->build(@{new_vars_args($spec)})] | ||
| 43 | } | ||
| 44 | |||
| 45 | fun new_vars_name($name) { | ||
| 46 | $name =~ s/^New_//gr =~ s/_/\//gr | ||
| 47 | } | ||
| 48 | |||
| 49 | fun new_vars_spec($name) { | ||
| 50 | [new_vars_name($name), split /;\s*/, $ENV{$name}] | ||
| 51 | } | ||
| 52 | |||
| 53 | fun new_vars_pump($conf) { | ||
| 54 | ${"ore::$$conf[0]"} = $$conf[1]; push @EXPORT, '$'.$$conf[0]; $conf | ||
| 55 | } | ||
| 56 | |||
| 57 | fun use_vars($args) { | ||
| 58 | [map use_vars_pump(use_vars_form($_)), @$args] | ||
| 59 | } | ||
| 60 | |||
| 61 | fun use_vars_args($spec) { | ||
| 62 | [map { /^\$(\w+)$/ ? ${"ore::$1"} : $_ } @$spec[2..$#$spec]] | ||
| 63 | } | ||
| 64 | |||
| 65 | fun use_vars_form($name) { | ||
| 66 | use_vars_make(use_vars_spec($name)) | ||
| 67 | } | ||
| 68 | |||
| 69 | fun use_vars_make($spec) { | ||
| 70 | [$spec->[1], Data::Object::Space->new($spec->[0])] | ||
| 71 | } | ||
| 72 | |||
| 73 | fun use_vars_name($name) { | ||
| 74 | $name =~ s/^Use_//gr =~ s/_/\//gr | ||
| 75 | } | ||
| 76 | |||
| 77 | fun use_vars_spec($name) { | ||
| 78 | [use_vars_name($name), split /;\s*/, $ENV{$name}] | ||
| 79 | } | ||
| 80 | |||
| 81 | fun use_vars_pump($conf) { | ||
| 82 | ${"ore::$$conf[0]"} = $$conf[1]; push @EXPORT, '$'.$$conf[0]; $conf | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | |||
| 87 | =encoding utf8 | ||
| 88 | |||
| 89 | =head1 NAME | ||
| 90 | |||
| 91 | ore | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | =head1 ABSTRACT | ||
| 96 | |||
| 97 | Sugar for Perl 5 one-liners | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | =head1 SYNOPSIS | ||
| 102 | |||
| 103 | BEGIN { | ||
| 104 | $ENV{New_File_Temp} = 'ft'; | ||
| 105 | } | ||
| 106 | |||
| 107 | use ore; | ||
| 108 | |||
| 109 | $ft | ||
| 110 | |||
| 111 | # "File::Temp" | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | =head1 DESCRIPTION | ||
| 116 | |||
| 117 | This package provides automatic package handling and object instantiation based | ||
| 118 | on environment variables. This is not a toy, but it's also not a joke. This | ||
| 119 | package exists because I was bored, shut-in due to the COVID-19 epidemic of | ||
| 120 | 2020, and inspired by L<new> and the ravings of a madman (mst). Though you | ||
| 121 | could use this package in a script it's meant to be used from the command-line. | ||
| 122 | |||
| 123 | =head2 new-example | ||
| 124 | |||
| 125 | Simple command-line example using env vars to drive object instantiation: | ||
| 126 | |||
| 127 | $ New_File_Temp=ft perl -More -e 'dd $ft' | ||
| 128 | |||
| 129 | # "File::Temp" | ||
| 130 | |||
| 131 | =head2 use-example | ||
| 132 | |||
| 133 | Another simple command-line example using env vars to return a | ||
| 134 | L<Data::Object::Space> object which calls C<children> and returns an arrayref | ||
| 135 | of L<Data::Object::Space> objects: | ||
| 136 | |||
| 137 | $ Use_DBI=dbi perl -More -e 'dd $dbi->children' | ||
| 138 | |||
| 139 | # [ | ||
| 140 | # ..., | ||
| 141 | # "DBI/DBD", | ||
| 142 | # "DBI/Profile", | ||
| 143 | # "DBI/ProfileData", | ||
| 144 | # "DBI/ProfileDumper", | ||
| 145 | # ..., | ||
| 146 | # ] | ||
| 147 | |||
| 148 | =head2 arg-example | ||
| 149 | |||
| 150 | Here's another simple command-line example using args as env vars with ordered | ||
| 151 | variable interpolation: | ||
| 152 | |||
| 153 | $ perl -More -E 'dd $pt' New_File_Temp=ft New_Path_Tiny='pt; $ft' | ||
| 154 | |||
| 155 | # /var/folders/pc/v4xb_.../T/JtYaKLTTSo | ||
| 156 | |||
| 157 | =head2 etc-example | ||
| 158 | |||
| 159 | Here's a command-line example using the aforementioned sugar with the | ||
| 160 | ever-awesome L<Reply> repl: | ||
| 161 | |||
| 162 | $ New_Path_Tiny='pt; /tmp' reply -More | ||
| 163 | |||
| 164 | 0> $pt | ||
| 165 | |||
| 166 | # $res[0] = bless(['/tmp', '/tmp'], 'Path::Tiny') | ||
| 167 | |||
| 168 | Or, go even further and hack together your own environment vars driven | ||
| 169 | L<Dotenv>, L<Reply>, and C<perl -More> based REPL: | ||
| 170 | |||
| 171 | #!/usr/bin/env perl | ||
| 172 | |||
| 173 | use Dotenv -load => "$0.env"; | ||
| 174 | |||
| 175 | use ore; | ||
| 176 | |||
| 177 | my $reply = `which reply`; | ||
| 178 | |||
| 179 | chomp $reply; | ||
| 180 | |||
| 181 | require $reply; | ||
| 182 | |||
| 183 | Then, provided you've the set appropriate env vars in C<reply.env>, you could | ||
| 184 | use your custom REPL at the command-line as per usual: | ||
| 185 | |||
| 186 | $ ./reply | ||
| 187 | |||
| 188 | 0> $pt | ||
| 189 | |||
| 190 | # $res[0] = bless(['/tmp', '/tmp'], 'Path::Tiny') | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | =head1 AUTHOR | ||
| 195 | |||
| 196 | Al Newkirk, C<awncorp@cpan.org> | ||
| 197 | |||
| 198 | =head1 LICENSE | ||
| 199 | |||
| 200 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 201 | |||
| 202 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 203 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 204 | file"|https://github.com/iamalnewkirk/ore/blob/master/LICENSE>. | ||
| 205 | |||
| 206 | =head1 PROJECT | ||
| 207 | |||
| 208 | L<Wiki|https://github.com/iamalnewkirk/ore/wiki> | ||
| 209 | |||
| 210 | L<Project|https://github.com/iamalnewkirk/ore> | ||
| 211 | |||
| 212 | L<Initiatives|https://github.com/iamalnewkirk/ore/projects> | ||
| 213 | |||
| 214 | L<Milestones|https://github.com/iamalnewkirk/ore/milestones> | ||
| 215 | |||
| 216 | L<Contributing|https://github.com/iamalnewkirk/ore/blob/master/CONTRIBUTE.md> | ||
| 217 | |||
| 218 | L<Issues|https://github.com/iamalnewkirk/ore/issues> | ||
| 219 | |||
| 220 | =cut |