| 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.02'; # VERSION | ||
| 19 | |||
| 20 | sub import { | ||
| 21 | my $args = [map { /([^=]+)=(.*)/; $ENV{$1} = $2; $1 } @ARGV]; | ||
| 22 | |||
| 23 | new_vars([@$args ? @$args : keys %ENV]); | ||
| 24 | use_vars([@$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($_)), grep /^New_/, @$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($_)), grep /^Use_/, @$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 | =cut | ||
| 112 | |||
| 113 | =head1 DESCRIPTION | ||
| 114 | |||
| 115 | This package provides automatic object instantiation based on environment | ||
| 116 | variables. This package exists because I was bored and inspired by L<new>. | ||
| 117 | |||
| 118 | =head2 examples | ||
| 119 | |||
| 120 | Simple command-line example using env vars to drive object instantiation: | ||
| 121 | |||
| 122 | $ New_File_Temp=ft perl -More -e 'dd $ft' | ||
| 123 | |||
| 124 | # "File::Temp" | ||
| 125 | |||
| 126 | Another simple command-line example using env vars to return a | ||
| 127 | L<Data::Object::Space> object which calls C<children> and returns an arrayref | ||
| 128 | of L<Data::Object::Space> objects: | ||
| 129 | |||
| 130 | $ Use_DBI=dbi perl -More -e 'dd $dbi->children' | ||
| 131 | |||
| 132 | # [ | ||
| 133 | # ..., | ||
| 134 | # "DBI/DBD", | ||
| 135 | # "DBI/Profile", | ||
| 136 | # "DBI/ProfileData", | ||
| 137 | # "DBI/ProfileDumper", | ||
| 138 | # ..., | ||
| 139 | # ] | ||
| 140 | |||
| 141 | Here's another simple command-line example using args as env vars with ordered | ||
| 142 | variable interpolation: | ||
| 143 | |||
| 144 | $ perl -More -E 'dd $pt' New_File_Temp=ft New_Path_Tiny='pt; $ft' | ||
| 145 | |||
| 146 | # /var/folders/pc/v4xb_.../T/JtYaKLTTSo | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | =head1 AUTHOR | ||
| 151 | |||
| 152 | Al Newkirk, C<awncorp@cpan.org> | ||
| 153 | |||
| 154 | =head1 LICENSE | ||
| 155 | |||
| 156 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 157 | |||
| 158 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 159 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 160 | file"|https://github.com/iamalnewkirk/ore/blob/master/LICENSE>. | ||
| 161 | |||
| 162 | =head1 PROJECT | ||
| 163 | |||
| 164 | L<Wiki|https://github.com/iamalnewkirk/ore/wiki> | ||
| 165 | |||
| 166 | L<Project|https://github.com/iamalnewkirk/ore> | ||
| 167 | |||
| 168 | L<Initiatives|https://github.com/iamalnewkirk/ore/projects> | ||
| 169 | |||
| 170 | L<Milestones|https://github.com/iamalnewkirk/ore/milestones> | ||
| 171 | |||
| 172 | L<Contributing|https://github.com/iamalnewkirk/ore/blob/master/CONTRIBUTE.md> | ||
| 173 | |||
| 174 | L<Issues|https://github.com/iamalnewkirk/ore/issues> | ||
| 175 | |||
| 176 | =cut |