in reply to Re^2: Exporting all variables and subroutines using Exporter
in thread Exporting all variables and subroutines using Exporter
that doesn't work
Oh, but it does "work"; and has done since perl 4 days. Here's proof:
fred.pm:
#package fred; sub X123{ return 'x123'; } sub Y456 { return 'Y456'; } $localScalar = 'from fred'; @localArray = qw[ also from fred ]; %localHash = qw[ and this comes from fred as well ! ]; print 'fred.pm loaded';
And junk33.pl that does it:
#! perl -slw do './fred.pm' or die "$@ / $!"; print $localScalar; print @localArray; print %localHash; print X123(); print Y456();
And the output from a run:
C:\test>junk33 fred.pm loaded from fred alsofromfred well!comesfromandthisfredas x123 Y456
See subs and variables 'exported' into the calling code.
Of course, if you enable warnings, you do get a few:
C:\test>junk33 Name "main::localArray" used only once: possible typo at C:\test\junk3 +3.pl line 6. Name "main::localHash" used only once: possible typo at C:\test\junk33 +.pl line 7. Name "main::localScalar" used only once: possible typo at C:\test\junk +33.pl line 5. fred.pm loaded from fred alsofromfred well!comesfromandthisfredas x123 Y456
And if you enable strict, things start to fall apart:
C:\test>junk33 Global symbol "$localScalar" requires explicit package name at C:\test +\junk33.pl line 6. Global symbol "@localArray" requires explicit package name at C:\test\ +junk33.pl line 7. Global symbol "%localHash" requires explicit package name at C:\test\j +unk33.pl line 8. Execution of C:\test\junk33.pl aborted due to compilation errors.
And that is my point. do 'file' is "is almost forgotten, little used syntax," for a reason; Perl 5 vastly improved upon the mechanisms available in Perl4, by introducing packages & namespaces & lexical variables, and the tools (require & use) to make use of them.
And in order to achieve what you are asking for in the OP, is to return to Perl4 -- which Perl will allow you to do -- but to achieve it you need to throw away namespaces and packages and lexical variables and strict & warnings. If you're prepared to do that, perl makes it simple. If you are not; then you might reconsider the reasons why you want to do it.
I could have told you: thou shalt not; but it's better that you understand the reasons why it probably better that you don't; that you understand the trade offs; and what you have to give up in order to avoid re-structuring the code that you are so adamant that you cannot do.
In other words; what you are asking for is doable; but given the trade-offs, you'll probably decide that you don't actually want to do it any more. But it is your decision to make.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Exporting all variables and subroutines using Exporter
by t-rex (Scribe) on Jul 14, 2016 at 08:54 UTC |