Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Including modules and pragmas in caller's scope via use

by peter.pl (Novice)
on Jul 31, 2014 at 03:28 UTC ( [id://1095679]=note: print w/replies, xml ) Need Help??


in reply to Including modules and pragmas in caller's scope via use

How about using do like this? Create your MyInit.pl like so:
use v5.18; BEGIN { use List::MoreUtils qw| any |; use Scalar::Util qw| looks_like_number |; use Fatal qw| open |; } sub mySub { say "This is mySub, foo!"; }
Then you can write your script like so:
#!/usr/bin/perl use v5.18; BEGIN { do 'MyInit.pl'; } mySub(); my @array = ( 'one', 'two', 'three' ); if( any {$_ eq 'two'} @array ) { say 'So far so good...'; } if( looks_like_number( "five" ) ) { say 'Hmm...'; } else { say 'Perl can\'t read.'; } open( my $dne, '<', 'nonexistentfile.txt' ); die 'Failed to import autodie';
Which seems to work like so:
peter@hermes:~/src/Perl/test> perl myscript.pl This is mySub, foo! So far so good... Perl can't read. Can't open(GLOB(0x9cc7d8), <, nonexistentfile.txt): No such file or di +rectory at (eval 3) line 4. main::__ANON__('GLOB(0x9cc7d8)', '<', 'nonexistentfile.txt') c +alled at myscript.pl line 22
The trick with autodie was that its import just gotos the import function from Fatal. So I just cut out the middleman.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-26 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found