package new;

our $VERSION = '0.000002'; # v0.0.2


use strict;
use warnings;

sub import {
  my ($me, $class, @args) = @_;
  return unless $class;
  my $targ = caller;
  require join('/', split '::', $class).'.pm';
  my ($name) = @args && $args[0] =~ /^\$/ ? map /^\$(.*)/, shift @args : 'O';
  my $obj = $class->can('new') ? $class->new(@args) : $class;
  no strict 'refs';
  ${"${targ}::${name}"} = $obj;


=head1 NAME

new - Object instantiation sugar for one-liners


Simplest possible usage:

  perl -Mnew=HTTP::Tiny -E \
    'say $O->get("")->{content}'

With arguments:

  perl -Mnew=HTTP::Tiny,max_redirects,3 -E \
    'say $O->get("")->{content}'

With custom object name:

  perl -Mnew=HTTP::Tiny,\$H -E \
    'say $H->get("")->{content}'

With both:

  perl -Mnew=HTTP::Tiny,\$H,max_redirects,3 -E \
    'say $H->get("")->{content}'


=head2 import

  new->import($class, @args)

First we C<require> the file for C<$class>, then call


then install the resulting object in C<$O> in the calling package.

B<Caveat>: if C<$class> loads successfully but does not have a C<new> method,
we install the C<$class> instead since you might want to call class methods.

If the first argument to C<import> after C<$class> begins with C<$>, this
is treated as the name to install the object as, so

  new->import($class, '$Obj', @args);

will create a variable C<$Obj> in the calling package instead of C<$O>.

=head1 AUTHOR

 mst - Matt S. Trout (cpan:MSTROUT) <>


None yet - maybe this software is perfect! (ahahahahahahahahaha)


Copyright (c) 2020 the new L</AUTHOR> and L</CONTRIBUTORS>
as listed above.

=head1 LICENSE

This library is free software and may be distributed under the same terms
as perl itself.