Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm in the process of writing a Perl script which will set up the relevant Oracle Environmental Variables for any of our local databases. I created hash tables for each of these instances and then hit a snag. Instead of duplicating the code for each hash table I thought I would just have the one piece of code with the relevant hash table name as a variable. I can find no way to make this work. Is this possible & if so how? I've searched the various Perl books I have and can find no reference to this, can anyone help? The code supplied is not complete I was just ensuring that a variable name for a hash table would work when I hit this problem.
#!/usr/bin/perl -w # # ############################################################### # Extra Modules should be declared here. # # All these modules MUST be found on the server attempting to # # call this script. # ############################################################### # use strict ; # Enforce private + variables use lib '/home/rcruickshank/Perl/Perl_Modules' ; # Where ACC_Vario +us lives use Cwd ; # Current Working + Directory Module use Mail::Sender; # Email module # ############################################################### # ACC_Various is an in-house written Perl module and MUST be # # found in the current path if this script is to work. The # # use lib 'xxxxxxxxxx' command should be used to point to the # # path that this module can be found on if this is not the # # case. (Obviously this code MUST preceed the use ACC_Various # # call!) # ############################################################### # use ACC_Various qw(end_it mail mail_log update_report) ; # ############################################################### # HASH TABLES ############################################################### my %ISWTEST = ( "TERM" => "vt220", "EDITOR" => "vi", "SECTRAN_DIR" => "/home/interface/sectran", "ORACLE_SID" => "ISWTEST" , "ORACLE_HOME" => "/isw/tpp/oracle/test/8.1.7" , "LD_LIBRARY_PATH" => "/isw/tpp/oracle/test/8.1.7/lib:/usr/lib/:/usr/ +ucblib" , ) ; # my %ISWTEST_PATH = ( "TEST1" => "/usr/local/bin:", "TEST2" => "/usr/bin:", "TEST3" => "/usr/ucb:" , "TEST4" => "/etc:" , "TEST5" => "/isw/tpp/oracle/test/1.6.1/idst/bin:" , "TEST6" => "/isw/tpp/oracle/test/1.6.1/idst/forms60/mesg:", "TEST7" => "/isw/appwork/test/finance/guifmxs:" , "TEST8" => ".:" , "TEST9" => "/isw/tpp/oracle/test/8.1.7/bin:" , "TEST10" => "/isw/tpp/oracle/test/8.1.7/dbs", ) ; # my %ISWLIVE = ( "TERM" => "vt220", "EDITOR" => "vi", "SECTRAN_DIR" => "/home/interface/sectran", "ORACLE_SID" => "ISWLIVE" , "ORACLE_HOME" => "/isw/tpp/oracle/live/8.1.7" , "LD_LIBRARY_PATH" => "/isw/tpp/oracle/live/8.1.7/lib:/usr/lib/:/usr/ +ucblib" , ) ; # my %ISWLIVE_PATH = ( "LIVE1" => "/usr/local/bin:", "LIVE2" => "/usr/bin:", "LIVE3" => "/usr/ucb:" , "LIVE4" => "/etc:" , "LIVE5" => "/isw/tpp/oracle/live/1.6.1/ids/bin:" , "LIVE6" => "/isw/tpp/oracle/live/1.6.1/ids/forms60/mesg:", "LIVE7" => "/isw/appwork/live/finance/guifmxs:" , "LIVE8" => ".:" , "LIVE9" => "/isw/tpp/oracle/live/8.1.7/bin:" , "LIVE10" => "/isw/tpp/oracle/live/8.1.7/dbs", ) ; # my %ISWTEACH = ( "TERM" => "vt220", "EDITOR" => "vi", "SECTRAN_DIR" => "/home/interface/sectran", "ORACLE_SID" => "ISWTEACH" , "ORACLE_HOME" => "/isw/tpp/oracle/general/8.1.7" , "LD_LIBRARY_PATH" => "/isw/tpp/oracle/general/8.1.7/lib:/usr/lib/:/u +sr/ucblib" , ) ; # my %ISWTEACH_PATH = ( "LIVE1" => "/usr/local/bin:", "LIVE2" => "/usr/bin:", "LIVE3" => "/usr/ucb:" , "LIVE4" => "/etc:" , "LIVE5" => "/isw/tpp/oracle/general/1.6.1/ids/bin:" , "LIVE6" => "/isw/tpp/oracle/general/1.6.1/ids/forms60/mesg:", "LIVE7" => "/isw/appwork/general/finance/guifmxs:" , "LIVE8" => ".:" , "LIVE9" => "/isw/tpp/oracle/general/8.1.7/bin:" , "LIVE10" => "/isw/tpp/oracle/general/8.1.7/dbs" ) ; # ############################################################### # Set up variables # ############################################################### # Date and time variables for headings in Emails/files etc. # ############################################################### # my ($min, $hour, $day, $mon, $year) = (localtime) [1,2,3,4,5] ; # my $date = sprintf("%02d/%02d/%04d", $day, $mon +=1, $year += + 1900); my $ren_date = sprintf("%04d%02d%02d", $year, $mon, $day) ; my $extract_date = sprintf("%04d%02d%02d" ,$year, $mon, $day) ; my $ptime = "$hour:$min" ; my $filedate = undef ; my $mday = undef ; my $mtime = undef ; my $fdate = undef ; # ################################ # Parameters # ################################ # my $DB_name = undef ; # ##################### # Current timestamp # ##################### # my $now = time ; # ################## # Cut-off dates # ################## # my $cdate = undef ; my $cday = undef ; my $cmon = undef ; my $cyear = undef ; # ################## # Start date # ################## # my $sdate = undef ; my $sday = undef ; my $smon = undef ; my $syear = undef ; my $p_sdate = undef ; # ############################################################### # Files & Directories # ############################################################### # my $logfile = 'ACC_ORACLE_ENV.log' ; # ############################################################### # mail_log variables. # ############################################################### # my $script = 'ACC_ORACLE_ENV' ; my $domain = 'mailhost' ; my $from = 'The Wonderful World Of Perl' ; my $subject = "URGENT - $script Failed!" ; my $msg = "This should change to reflect what is happening." ; my $attachments = undef ; # ###################################### # Email recipient lists # ###################################### # my $to = 'ronniec@it.aberdeen.net.uk' ; # ######################################## # Messages used by SUB end_it # ######################################## # my $null = "none" ; my $text = "Unable to write the following to $logfile :-\n" ; my $topline = "The error message is -\n" ; # ############################################################## # BOOLEAN VARIABLES # ############################################################## # my $mail_msg = 1 ; my $PARAMETER_ERROR = 0 ; # ############################################################### # COMMON OR GARDEN SCALAR VARIABLES # ############################################################### # my $param_no = 0 ; my $key = undef ; my $value = undef ; # ############################################################### # PROCESSING # ############################################################### # print "\n\t\t<***** SOR $script *****>\n" ; # ############################################################### # Check for Parameters # ############################################################### # $param_no = scalar @ARGV ; # if ($param_no == 1) { # One parameter has been supplied # $DB_name = $ARGV[0] ; if ($DB_name =~ /[ISWLIVE ISWTEST ISWTEACH]/) { print "\n\tProcessing $DB_name Environment.\n\n" ; } else { print "\n\tThink again muppett!\n" ; $msg = "$DB_name is not a database known to this script!" ; &end_it($mail_msg,$to,$from,$subject,$msg,$topline) ; } # } else { print "\n\t<***** There MUST be a parameter supplied! *****>" ; print "\n\n\t<***** $script Failed to run. *****>\n\n" ; $msg = "DB name parameter not supplied" ; &end_it($mail_msg,$to,$from,$subject,$msg,$topline) ; } # while (($key,$value) = each %$DB_name) { print "\tKey :: $key\n" ; print "\tValue :: $value\n" ; } # print "\n\t\t<***** EOR $script *****>\n" ;

This results in the following error -
<***** SOR ACC_ORACLE_ENV *****>
Processing ISWTEST Environment.
Can't use string ("ISWTEST") as a HASH ref while "strict refs" in use at xxrc_ne w_profile.pl line 224.
Removing the "use Strict" allows the script to run to completion but none of the key/value pairs are printed so I don't think it is actually working. Cheers, Ronnie

In reply to hash name as a variable? by Ronnie

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-19 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found