Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re-calling in a list of variables into different .pl's

by Lori713 (Pilgrim)
on Feb 06, 2004 at 14:10 UTC ( [id://327068]=perlquestion: print w/replies, xml ) Need Help??

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

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

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 14:26 UTC
    I've no idea what you want to do. &call_variables doesn't expose any variables to the outside - all it does is return values. A shorter way of writing call_variables is:
    sub call_variables { split /~/ => scalar param 'spec_vars' }
    (assuming you call it in list context).

    In general, if you want to share code, one uses use, or require and sometimes do. You may want to make use of the Exporter module to export names from the called package to the calling package.

    Abigail

      I've no idea what you want to do. &call_variables doesn't expose any variables to the outside - all it does is return values.

      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:

      #!/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
      This would be in the myVars.pm file:
      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]; }
      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?
      $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

        How well did you read the Exporter manual page? It nowhere suggest that a mere use Exporter; peeks inside your brain and knows which tokens to export.

        For starters, you use the Exporter module from the module you want to *drumroll* export from. So, myVars, in your case. Then you need to subclass your exporting module to be a subclass of Exporter - that's what the @ISA line is doing in the SYNOPSIS of the manual page. Finally, you need to tell Exporter what you want to export, that's where you use @EXPORT, @EXPORT_OK and/or %EXPORT_TAGS.

        Please study the manual pages (for instance perlmod), a book or a tutorial about writing modules.

        Abigail

Re: Re-calling in a list of variables into different .pl's
by calin (Deacon) on Feb 06, 2004 at 14:48 UTC
    Best solution: use a global hash.

    Otherwise, just declare them at the top of your program, and make call_variables update them. Don't put my $one = ... inside the function because that would create lexicals in the scope of the function body (not what you want).

    With hash:

    # [at the top, before subroutine definitions and code] my %my_vars; # [...] sub call_variables { #pick up a string I've passed from previous program via 'spec_vars' my $spec_vars = param('spec_vars'); @my_vars{qw/one two three four five/} = split /~/, $spec_vars; }

    With globals (file-scoped lexicals, not dynamic variables):

    # [at the top, before subroutine definitions and code] my ($one, $two, $three, $four, $five); # [...] sub call_variables { #pick up a string I've passed from previous program via 'spec_vars' my $spec_vars = param('spec_vars'); ($one, $two, $three, $four, $five) = split /~/, $spec_vars; }

    If you don't want to declare them, you have to use fully scoped package variable names such as $::one under use strict. The variables will be dynamic, of course.

    Update: replaced @my_vars{one, two, three, four, five} with @my_vars{qw/one two three four five/}

Re: Re-calling in a list of variables into different .pl's
by hardburn (Abbot) on Feb 06, 2004 at 14:21 UTC

    I'm not entirely sure what you're asking for, but I suspect you can benefit from array slices:

    sub call_variables { my $spec_vars = param('spec_vars'); return (split /~/, $spec_vars)[0,1,2,3,4]; }

    ----
    I wanted to explore how Perl's closures can be
    manipulated, and ended up creating an object
    system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Re-calling in a list of variables into different .pl's
by arden (Curate) on Feb 06, 2004 at 14:26 UTC
    Lori, actually you need to declare the variables outside of the subroutine with my and then you can use them inside it. You'd need something like my($one, $two, $three, $four, $five); before you call the subroutine, then drop the "my"'s from inside the subroutine.

    Another option would be to have the variables exported by creating your own package that gets the variables and putting use myvariables; at the beginning of your code, but that would require more work (although it might be "The Right Way"™ to do it).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found