Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I tend to use perl one-liners extensively, mostly for testing purposes. In particular, I often load a module with a loooooooooong name only to instantiate it once. And it's boooooooring to type the whole name twice.

So I came up with an idea of a one-liner shortener along the lines of:

perl -we 'use new qw(My::Very::Long::Module x foo 42); print $x->get_f +oo;'
Which is a rough equivalent of
perl -we 'use My::Very::Long::Module; our $x = My::Very::Long::Module- +>new( foo => 42 ); print $x->get_foo;'

And I have a proof-of-concept implementation that works as follows:

  • require module being called;
  • create a new instance;
  • create a package variable in the calling package (so that strict doesn't complain);
  • set that variable to instance from above.

Here it is.

package new; use strict; use warnings; =head1 NAME new - shorted require Foo; Foo->new(...) to just one call =head1 SYNOPSYS use new qw(My::Very::Long::Module x foo 42); is an exact equivalent of our $x; BEGIN { require My::Very::Long::Module; $x = My::Very::Long::Module->new( foo => 42 ); }; or a rough equivalent of use My::Very::Long::Module; our $x = My::Very::Long::Module->new( foo => 42 ); Not a big deal in a real program, but may save some typing in a one-liner test script. Works well under C<use strict;>. =cut $Carp::Internal{ (__PACKAGE__) }++; sub import { my ($self, $target, $name, @args) = @_; my $filename = $target; $filename =~ s#::#/#g; $filename .= ".pm"; require $filename; my $obj = $target->new( @args ); my $caller = caller; $name ||= 'new'; my $sym = join "::", $caller, $name; no strict qw(refs vars); *$sym = \$$sym; $$sym = $obj; }; 1;

Made purely for fun, but maybe there's some point in it after all.

UPDATE Available as gist.


In reply to RFC: new.pm - a perl -e use/new shortener by Dallaylaen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found