Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Variable declared in script, used by module, and used in script

by ExReg (Priest)
on May 16, 2018 at 17:04 UTC ( #1214672=perlquestion: print w/replies, xml ) Need Help??
ExReg has asked for the wisdom of the Perl Monks concerning the following question:

Having a bad morning remembering. I have a script that runs a bunch of checks. It uses a module that contains an array of checks. There are variables defined in the script that are in the checks in the module that I can get to work. Here is a simplified example:

checks.pl use strict; use warnings; use check_module; my $home_dir = '/home/mine/'; for my $check ( @checks ) { print "Checking $check->{name}\n"; `check->{script}`; } check_module.pm package check_module; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(@checks); our @checks = ( { name => "Anybody home?", script => qq/echo $home_dir/, }, ); 1;

When I run it I get

Checking Anybody home?

How do I get the $home_dir to evaluate so that I get

Checking Anybody home? /home/mine

I hope I typed this simplified example OK. It is on another system that cannot use CPAN or anything else except that which is installed. Thanks.

Replies are listed 'Best First'.
Re: Variable declared in script, used by module, and used in script (updated)
by AnomalousMonk (Chancellor) on May 16, 2018 at 17:28 UTC

    Quick and dirty (Windows 7, Perl 5.8.9):
    Module check_module.pm:

    package check_module; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(@checks); our @checks = ( { name => 'Anybody home?', script => 'qq/echo $home_dir/', }, ); 1;
    Note esp. that  'qq/echo $home_dir/' is single-quoted in the .pm file.

    Script:

    c:\@Work\Perl\monks\ExReg>perl -wMstrict -le "use strict; use warnings; use check_module; my $home_dir = '/home/mine/'; for my $check ( @checks ) { print qq{Checking $check->{name}}; my $evaled_script = eval qq{$check->{script}}; print `$evaled_script`; } " Checking Anybody home? /home/mine/
    (Update: The trick here is to eval the  $check->{script} string from the module in the scope of the script in which  $home_dir is declared and initialized.)

    Update 1: If you had included the  use warnings; use strict; statements in the .pm module (after the package statement), you would have had a slightly earlier and possibly slightly more informative notification that something was going sideways.

    Update 2:

    ... [a] system that cannot use CPAN ...
    See Yes, even you can use CPAN.


    Give a man a fish:  <%-{-{-{-<

      Many thanks. I don't know why I couldn't remember eval. That does the trick.

Re: Variable declared in script, used by module, and used in script
by pryrt (Vicar) on May 17, 2018 at 14:01 UTC

    When I want to pass a variable that's local to my script into a module, I pass it as an argument to a subroutine. In your case, it would be an anonymous sub in the array-of-hashrefs:

    package check_module; use warnings; use strict; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(@checks); our @checks = ( { name => 'Anybody home?', #script => 'qq/echo $home_dir/', script => sub { `echo $_[0]` }, }, ); 1; __END__
    use warnings; use strict; use lib '.'; use check_module; my $home_dir = '/home/mine'; for my $check ( @checks ) { chomp ( my $script_out = $check->{script}->($home_dir) ); print "Checking $check->{name}\n\t=> '$script_out'\n"; } __END__
    __OUTPUT__ Checking Anybody home? => '/home/mine'
Re: Variable declared in script, used by module, and used in script
by Anonymous Monk on May 17, 2018 at 08:45 UTC
    `check->{script}`; Backticks are for system shell code, how is this object oriented , is this OKAY syntax? What perldoc should I consult to understand this odd syntax?
Re: Variable declared in script, used by module, and used in script
by KolassaRaptor (Scribe) on May 17, 2018 at 14:49 UTC
    You could replace qq with qx and have it work as you expected it to.

      Can you give a code example of your idea?


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-10-21 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (119 votes). Check out past polls.

    Notices?