in reply to Is this safe to export and import functions this way?

I didnt know which reply to reply to, so ill just reply to myself haha ;) Anyway, i did change up the module and script a little, to this:
#Package.pm package My::Package; use strict; #can use strict and warnings as usual now as well without +any warnings use warnings; use Exporter qw(import); our @EXPORT = qw(print_lines test test_2); #just add in function names + as you make them our %EXPORT_TAGS = (all => \@EXPORT); sub print_lines{ my ($input) = shift; while(<$input>){ print; } close($input); } sub test{ my ($input) = shift; print $input; } sub test_2{ my ($input) = shift; print $input; }

And here is the script calling it

#script.pl use strict; use warnings; use My::Package qw(:all); open (my $file, '<', shift); print_lines($file); test("hello world\n"); test_2("hello this is dog"); # i love this meme lol

That ":all" is what i was originally shooting for. Though I also figured out you could just require My::Package; and then just call it in the script with use My::Package; and do not have to specify any functions. Anyways, thanks for the help fellers :)

UPDATE: Also I wanted to ask, if I use strict and warnings in the main script, is that in the modules scope as well, or do I need to use strict and warnings in the module as well? Also IF strict and warnings are in the same scope, is it just for the functions/subs or the whole module?

Replies are listed 'Best First'.
Re^2: Is this safe to export and import functions this way? (strict)
by tye (Sage) on Aug 20, 2015 at 21:25 UTC
    UPDATE: Also I wanted to ask, if I use strict and warnings in the main script, is that in the modules scope as well, or do I need to use strict and warnings in the module as well?

    'use strict' is a pragma that has lexical scope. Loading a module via use loads the module via require which notes "The file is included via the do-FILE mechanism, which is essentially just a variety of eval with the caveat that lexical variables in the invoking script will be invisible to the included code". So a lexical pragma will also not stay in effect.

    So you need to 'use strict;' in each module.

    - tye        

Re^2: Is this safe to export and import functions this way?
by RonW (Parson) on Aug 20, 2015 at 20:31 UTC

    I suggest doing it this way:

    our @EXPORT_OK = qw(print_lines test test_2); our %EXPORT_TAGS = (all => \@EXPORT_OK);

    Because when you set @EXPORT, the items in @EXPORT are automatically exported.