Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Passing variables to scripts rans through 'do'

by c (Hermit)
on Dec 02, 2001 at 19:28 UTC ( [id://128985]=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

i am working on dynamically building a .conf file by using "include" like statements within a script. i read over this which pointed me towards reading this and got me off on the right foot. however, some of the scripts ran through 'do' need to be able to pull variable values from the original script. an example will hopefully clarify:

genconf.pl

#!/usr/bin/perl -w use strict; my $domain = "lunarmedia.net"; do 'genheader'; do 'genlimit'; do 'genfooter';

###

genheader

print "<VirtualHost $domain>\n";

the genconf.pl file executes and runs the three external scripts. each of the external scripts just prints out a line of text. however, as you can see in genheader, a portion of the text requires some sort of knowledge about a variable set within the main script.

the only way i figured i could get around this is to try something like:

genconf.pl

#!/usr/bin/perl -w use strict; require genheader; require genlimit; require genfooter; my $domain = "lunarmedia.net"; &genHeader($domain); &genLimit; &genFooter;

###

genheader

sub genHeader { my $domain = shift; print "<VirtualHost $domain>\n"; }

but i dont believe that i am really understanding require because the magic just isnt happening with this code either. i think using the require or even a 'use' statement for a future module possibility(?) may be the way i want to go, however, i am not certain what i am missing in my required file to make it work. could someone enlighten me?

humbly -c

Replies are listed 'Best First'.
Re (tilly) 1: Passing variables to scripts rans through 'do'
by tilly (Archbishop) on Dec 02, 2001 at 19:54 UTC
    In your first link the best suggestion was from chromatic. Instead of thinking in terms of executing an external script, think in terms of calling a function. Both are methods of executing arbitrary code, but the advantage of a function is that it also has notions of how to pass arguments.

    The alternative is to use global variables to do your communication. Global variables to communicate between a mass of scripts that call each other haphazardly is a short road to insanity. While it is the answer that you asked for, I strongly recommend not doing that. Write a module if you want your files to actually do something.

    You may not have written a module before. If you haven't, it isn't as hard as people make it look. Take a look at Re (tilly) 1: Best way to fix a broken but functional program? for a random instance. Just save that template to a file whose name ends in ".pm", write a test script that uses it, then start adding, running, and testing. That template should serve you pretty well.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found