Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Exporting all variables and subroutines using Exporter

by BrowserUk (Patriarch)
on Jul 14, 2016 at 07:06 UTC ( [id://1167761]=note: print w/replies, xml ) Need Help??


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.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

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

    thanks... my strict and warnings are on hence i was getting a compilation error, i am not so adamant about changing the code but its a top down decision hence we have to comply by it :P

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1167761]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found