in reply to Re: Re-calling in a list of variables into different .pl's
in thread Re-calling in a list of variables into different .pl's
Exactly my problem. I'll try to further (and hopefully better) explain what I want to do.
I have a section of code that I'm using in several programs. The section looks exactly like what is inside the sub I described above (no variation nor different values assigned, etc). If it were possible, I would copy and paste that section into every program it needs to go into. Of course, while you're running programs you can't very well say, "Hold on, I need to copy and paste that text into the program before you run it!" I'm using the same 25 lines of code (get the param, split the array, assign the variables their $array values), so I was looking for a way to somehow automagically insert that section each time.
I read the POD about Exporter. If I understand Abigail-II and arden correctly, Exporter will let me put my repeating section of code in a .pm package that I use or require. However, it seems like I'll still need to declare all the variables (my $one, my $two) near the beginning of each program that will be calling the sub. Do I have that right?
So, I would end up with something like:
This would be in the myVars.pm file:#!/usr/local/bin/perl5_8 use strict; use Exporter; use myVars; #this would be my own myVars.pm use CGI; use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); my $CGI = CGI->new; $|=1; print $CGI->header; my ($one, $two .... $twenty-five); #call my variables that are always the same &call_variables; #do other stuff using these called-in variables #without having to declare "my $one" and "my $two" again $fred = $one . $two; #or some such silliness
That means that the above new stuff would be the same as if I had typed in this section into a program instead of calling the sub - is that right?sub call_variables { #pick up a string I've passed from previous program via 'spec_vars' my $spec_vars = param('spec_vars'); return (split /~/, $spec_vars)[0,1,2,3,4 .. 25]; }
$one = $spec_vars[0]; $two = $spec_vars[1]; etc.
In the meantime, I'll be reading up on use and require to better understand the difference.
Lori
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re-calling in a list of variables into different .pl's
by Abigail-II (Bishop) on Feb 06, 2004 at 15:33 UTC | |
by Lori713 (Pilgrim) on Feb 06, 2004 at 16:07 UTC | |
by ysth (Canon) on Feb 06, 2004 at 16:45 UTC | |
by Lori713 (Pilgrim) on Feb 09, 2004 at 14:20 UTC | |
by Lori713 (Pilgrim) on Feb 10, 2004 at 20:43 UTC | |
by ysth (Canon) on Feb 10, 2004 at 21:34 UTC | |
|