Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Possible to make alias for 'use' ?

by exilepanda (Monk)
on Oct 07, 2011 at 05:24 UTC ( #930107=perlquestion: print w/ replies, xml ) Need Help??
exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

Hello world! My first post here, so hope not be blamed. First, introduce that I like to call my module in this way :
use myRoboFolks; eval { hey myRoboFolks -> provided(%argv) -> please("do something"); }; Sorry if $@; # hey() is the constructor alias to new() for real # Sorry is an exported sub which extends die() by # $SIG{__DIE__} capture

Question: Can I do something to make "With" alias to use()? So I can write

With myRoboFolks; eval {...}
Plus, if there's such a way, can it apply to other perlfuncs ?

My current approach is to use eval{require()} at the BEGIN block, because 'require' looks better than 'use' but I think I better reserve the BEGIN block more clear for some init processes. And this approach actually looks bulky.

So... I am not a professional programmer, and I just want to write codes in the style un-dull, for my very own long boring projects. So please don't blame me on the style manner, I really need some fun with the codes...

Any clues would be much appreciate! Thanks a lot!

Comment on Possible to make alias for 'use' ?
Select or Download Code
Re: Possible to make alias for 'use' ?
by AnomalousMonk (Abbot) on Oct 07, 2011 at 05:57 UTC

    Some people have a very strange notion of 'fun'. Why not just write everything in brainfuck?

      No, because I am fan of Perl, and I like that TMTOWTDI idea with linguistic human friendly capability.
Re: Possible to make alias for 'use' ?
by afresh1 (Hermit) on Oct 07, 2011 at 06:12 UTC

    Yes, but it's probably a bad idea. You can declare a sub that will "use" the module for you.

    sub With { eval "use @_"; } With 'myRoboFolks'; ...
    l8rZ,
    --
    andrew
      It seems won't work...
      sub With { eval "use @_"; } With 'strict'; $x = 10;
      It didn't die() as what I would expect... It that any hacks to deal with *CORE:: stuff ?
        strict is lexically scoped, this won't die either
        sub Foo { use strict; @_ } $x = 1;

        If you want to fiddle with strict/warnings, see Toolkit, nonsense...

Re: Possible to make alias for 'use' ?
by moritz (Cardinal) on Oct 07, 2011 at 10:20 UTC
    If you want to replace use, you should be aware of what it does. use Foo; is short for
    BEGIN { require Foo; Foo->import; }

    Having just a function that calls use at run time, as afresh1 suggested, has the problem that the imports are done after the rest of the file has been parsed, so Perl doesn't know about subroutines and variables that the module exports.

    So your use replacement/alias needs to execute stuff at compile time. I'm not aware how that can be done with "normal" Perl tools, you'd need to write a source filter. Source filters are considered to be rather evil and fragile (for good reason); using Devel::Declare to implement the source filter can make it a tad less evil.

    In summary, I don't think the required effort for doing it properly justifies the gain you get, but of course your mileage my vary.

    See also this blog post on using modules for some of the difficulties of proper module loading in Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930107]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-09-16 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (44 votes), past polls