Suppose I have five variables ($one, $two, $three, $four and $five) that I use in several different programs (I actually have about 25 variables that I'm tracking). I'd like to figure out a way to call these in each time I run certain programs while I'm doing the use strict thing so I don't have to type them in over and over (plus their values are being set by a split on an array and I occassionally need to change the order of things, so it's a PITA to change the index numbers on every instance).

I was thinking maybe a using a subroutine to call them in, but I can't figure out how to do that. Here's what I'm thinking:

#!/usr/local/bin/perl5_8 use strict; use CGI; use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); my $CGI = CGI->new; $|=1; print $CGI->header; #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
This would be the sub:
sub call_variables { #pick up a string I've passed from previous program via 'spec_vars' my $spec_vars = param('spec_vars'); my @variables_array = split /~/, $spec_vars; my $one = $variables_array[0]; my $two = $variables_array[1]; my $three = $variables_array[2]; my $four = $variables_array[3]; my $five = $variables_array[4]; return ($one, $two, $three, $four, $five); }
BTW, it would be nice to figure out a way to iterate over the index numbers, but I think I can figure that out later.

Would it be better to create a small .txt file, read that in, and then try to get it to spit it out each time I run &call_variables? Should I create a little package that contains all these?

I'm sure there's severals ways to do this. I read the section on subroutines in the Llama book but nothing seem to address the concept of just "reading in" lines into the main program and returning those values to the program (because it only returns the last value from what I understand, plus it doesn't allow me to just use the variables without "my"ing them again).

Thanks for any light you can shed!

Lori


In reply to Re-calling in a list of variables into different .pl's by Lori713

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.