Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by Lori713 (Pilgrim)
on Feb 06, 2004 at 15:17 UTC ( [id://327095]=note: print w/replies, xml ) Need Help??


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

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

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
    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

      Thanks for your feedback. I read through and tried to understand as much as I could about Exporter (which I obviously didn't understand as well as I thought I did). One of my learning traits is that I learn by doing, not by reading. So, it's sometimes difficult to understand a lot of the PODs I read (especially since programming is relatively new to me). I usually try to read them over and over again, and several I have printed out so I can flip back and forth (i.e., CGI.pm, HTML::Template).

      I am not complaining, whining, or having a pity party of one, mind you. Since I learn by doing and not by reading (and I'm not a big fan of cargo-cult programming because that will invariably bite me in the arse when I have to go back and maintain this stuff), I sometimes get things confused. I like to try to figure out the basics of something as best I can by reading, then tinker with it to gain a better, fuller understanding of what it is I'm playing with. Of course, this means I'll probably post more questions that clearly indicate I have something bass-ackwards, and need to be flipped around or upside down so they're correct.

      Your explanation of what the Exporter module does, combined with what I read in the POD, makes it clearer to me now how Exporter works. Most PODS (rightly) assume a better, more well-rounded programming knowledge than I currently possess, and I look forward to feeling like a movie star when all those lightbulbs inevitably go off and I begin to pull it all together in my brain.

      :-D

      Thanks again for taking the time to flip me to the correct position!

      Lori (strapping herself more tightly into her harness for further flipping)

        To get you started, perhaps something like this would help (untested):
        #MyVars.pm package MyVars; use strict; use warnings; use Exporter (); our @ISA = 'Exporter'; our ($one,$two,$three,$four,$five); our @EXPORT = qw/$one $two $three $four $five call_variables/; 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; return; # nothing; this sub only sets the variables } 1; # in your script use MyVars; # declares variables (actually just aliases them to the on +es in the MyVars package) # to start with, they will be undefined (or at least should be treated + as such) call_variables(); # now you can say $fred = $one . $two;

Log In?
Username:
Password:

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

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

    No recent polls found