Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Lori713 (Pilgrim)
on Feb 06, 2004 at 16:07 UTC ( [id://327113]=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

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)

Replies are listed 'Best First'.
Re: Re: Re: Re-calling in a list of variables into different .pl's
by ysth (Canon) on Feb 06, 2004 at 16:45 UTC
    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;
      Thank you for the example. This really helps me to begin using it, and then tweaking it to make sure I understand the nuts and bolts. I am very grateful for your taking the time to give me an example that so closely relates to what I'm trying to do! :-D

      Lori

      I've used the example you provided, and it works beautimously. However, I'm wondering if there's a better way to avoid having to repeat these same variable names four times as you can see below (once in the main program and three times in the nc_library.pm package). I have about twenty-five in all, so it would be nice not to have to constantly synchronize them (not to mention the fact that I've also got to put them in HTML::Template for a TMPL_VAR loop thingy). Any ideas? Thanks!

      #my second_page.pl: #!/usr/local/bin/perl5_8 use strict; use warnings; use nc_pswd; use nc_library; use vars qw/ $titlebar $rpt_no $rpt_lbl $rpt_name $rpt_asofdt $project $proj_re +f $attr_dept $attr_descr $attr_award $attr_prog $attr_projpd $attr_f +yr $attr_budgpd $attr_fund $attr_rate $attr_subclass $attr_status $attr_spec $attr_equip $attr_resp $attr_pi $rpt_type $from_act_tbl $from_bud_tbl /; use HTML::Template; use DBI; use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); #remove for PRD my $CGI = CGI->new; #clear buffers and set up web page (required) $|=1; print $CGI->header; &call_vars; #and do other stuff.... # --------------------------------------------------------------- # #my nc_library.pm package nc_library; use strict; use warnings; use Exporter (); our @ISA = 'Exporter'; our ( $titlebar, $rpt_no, $rpt_lbl, $rpt_name, $rpt_asofdt, $project, $proj_ref, $attr_dept, $attr_descr, $attr_award, $attr_prog, $attr_projpd, $attr_fyr, $attr_budgpd, $attr_fund, $attr_rate, $attr_subclass, $attr_status, $attr_spec, $attr_equip, $attr_resp, $attr_pi, $rpt_type, $from_act_tbl, $from_bud_tbl ); our @EXPORT = qw/ call_vars commify dberror $CGI $spec_vars $titlebar $rpt_no $rpt_lbl $rpt_name $rpt_asofdt $project $proj_ref $attr_dept $attr_descr $attr_award $attr_prog $attr_projpd $attr_fyr $attr_budgpd $attr_fund $attr_rate $attr_subclass $attr_status $attr_spec $attr_equip $attr_resp $attr_pi $rpt_type $from_act_tbl $from_bud_tbl /; sub call_vars { use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); #remove for PRD my $CGI = CGI->new; my $spec_vars = param('spec_vars'); ( $titlebar, $rpt_no, $rpt_lbl, $rpt_name, $rpt_asofdt, $project, $proj_ref, $attr_dept, $attr_descr, $attr_award, $attr_prog, $attr_projpd, $attr_fyr, $attr_budgpd, $attr_fund, $attr_rate, $attr_subclass, $attr_status, $attr_spec, $attr_equip, $attr_resp, $attr_pi, $rpt_type, $from_act_tbl, $from_bud_tbl) = split /~/, $spec_vars; return; } sub commify #Formats numbers (puts in commas) { local $_ = sprintf "%.2f", shift @_; 1 while $_ =~ s/^(-?\d+)(\d\d\d)/$1,$2/; return $_; } 1;

      Lori

        The use vars in the main program should be unnecessary.

        You may want to just use a hash instead of separate variables.

        Other than that, you can get rid of the our() list by saying something like our @EXPORT; use vars @EXPORT=qw/ list of var names /;. You could do something with eval to get the split working, but I wouldn't bother (unless the var names are going to change a lot).

Log In?
Username:
Password:

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

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

    No recent polls found