I have working code (attached) that implements what I am trying to do (some code commented out for submitting this question). But it only works if I declare/initialize lots of variables then use those variables in further declarations. I'm trying to use all literals to make the data type easier to maintain (by others) over time. I have RTFM and tried a as many combinations of references and pointers as I can think of.
I have been at it for 4 days and any PM wisdom you have I will cherish.
Here is the output from a run:links.pl: LINKS ================================================= += $$aref[ 0] = HASH(0x1005d030) System Apache requires 3 links --slinks is ARRAY(0x1005cfe8) --system is Apache HASH(0x1005cdf0) FOLDER LINK: /etc/httpd/SSL/ -> /usr/local/private/QA/apache/SSL/ HASH(0x1005ce98) FILE LINK: /etc/httpd/conf/httpd.conf -> /usr/local/private/QA/a +pache/conf/httpd.conf HASH(0x1005cf40) FILE LINK: /etc/httpd/conf.d/httpd.conf -> /usr/local/private/QA +/apache/conf.d/nagios.conf $$aref[ 1] = HASH(0x1005d1b0) System Logrotate requires 1 link --slinks is ARRAY(0x1005d168) --system is Logrotate HASH(0x1005d0c0) FILE LINK: /etc/logrotate.d/nagios -> /usr/local/private/QA/logr +otate/nagios $$aref[ 2] = HASH(0x1005d330) System Mysql requires 1 link --slinks is ARRAY(0x1005d2e8) --system is Mysql HASH(0x1005d240) FOLDER LINK: /var/lib/mysql/ -> /usr/local/private/QA/mysql/ $$aref[ 3] = HASH(0x1005d4b0) System Nagios requires 1 link --slinks is ARRAY(0x1005d468) --system is Nagios HASH(0x1005d3c0) FOLDER LINK: /usr/local/nagios/ -> /usr/local/private/QA/nagios/ $$aref[ 4] = HASH(0x1005d6d8) System NagioSQL requires 2 links --slinks is ARRAY(0x1005d690) --system is NagioSQL HASH(0x1005d540) FOLDER LINK: /etc/nagios/ -> /usr/local/private/QA/nagios/etc/ HASH(0x1005d5e8) FOLDER LINK: /etc/nagiosql/ -> /usr/local/private/QA/nagios/etc.na +giosql/ $$aref[ 5] = HASH(0x10057880) System Odbc requires 1 link --slinks is ARRAY(0x10057838) --system is Odbc HASH(0x1005d768) FILE LINK: /etc/odbc.ini -> /usr/local/private/QA/odbc/odbc.ini $$aref[ 6] = HASH(0x10057a00) System Php requires 1 link --slinks is ARRAY(0x100579b8) --system is Php HASH(0x10057910) FILE LINK: /etc/php.ini -> /usr/local/private/QA/php/php.ini $$aref[ 7] = HASH(0x10057c28) System Postfix requires 2 links --slinks is ARRAY(0x10057be0) --system is Postfix HASH(0x10057a90) FILE LINK: /etc/postfix/main.cf -> /usr/local/private/QA/postfix +/main.cf HASH(0x10057b38) FOLDER LINK: /etc/postfix/SSL/ -> /usr/local/private/QA/postfix/SS +L/ $$aref[ 8] = HASH(0x10057da8) System Syslog-NG requires 1 link --slinks is ARRAY(0x10057d60) --system is Syslog-NG HASH(0x10057cb8) FOLDER LINK: /usr/local/syslog-ng/ -> /usr/local/private/QA/syslog +-ng/ links.pl: LITERALS ================================================= += $$aref[ 0] = REF(0x10052398) Not a HASH reference at ./links.pl line 160.
And this is the code:
#!/usr/bin/perl -w # use strict; # declare "unalterable" variables (THESE ARE UPPERCASE) my $NAME = $0; $NAME =~ s,.*/,,; my $SPACE = $NAME; $SPACE =~ s/./ /g; my $DIRE = $0; $_ = $0; $DIRE = $1 if ( m ,(.+)/(.+), ); if ( $DIRE eq $NAME ) { $DIRE = "."; } if ( $DIRE eq "" ) { $DIRE = "."; } $DIRE = `(cd "$DIRE"; echo \${PWD})`; chomp($DIR +E); # my $CONF = "$DIRE/install.conf"; my $todo = ''; my $verb = -1; my $html = -1; our $StrList; our $StrInst; our $StrRemo; our $IslHome; our $NSystem; our @Literals; # require $CONF; $todo = '--list'; $StrList = $todo; # for debug ######################################## # 0 my %h00 = ( "--link" => "/etc/httpd/SSL", "--fold" => "/ +usr/local/private/QA/apache/SSL", ); my %h01 = ( "--link" => "/etc/httpd/conf/httpd.conf", "--file" => "/ +usr/local/private/QA/apache/conf/httpd.conf", ); my %h02 = ( "--link" => "/etc/httpd/conf.d/httpd.conf", "--file" => "/ +usr/local/private/QA/apache/conf.d/nagios.conf", ); my @a00 = ( \%h00, \%h01, \%h02, ); my %H00 = ( "--system", 'Apache', "--slinks", \@a00,); # 1 my %l03 = ( "--link" => "/etc/logrotate.d/nagios", "--file" => "/ +usr/local/private/QA/logrotate/nagios", ); my @a01 = ( \%l03, ); my %H01 = ( "--system", 'Logrotate', "--slinks", \@a01,); # 2 my %l04 = ( "--link" => "/var/lib/mysql", "--fold" => "/ +usr/local/private/QA/mysql", ); my @a02 = ( \%l04, ); my %H02 = ( "--system", 'Mysql', "--slinks", \@a02,); # 3 my %l05 = ( "--link" => "/usr/local/nagios", "--fold" => "/ +usr/local/private/QA/nagios", ); my @a03 = ( \%l05, ); my %H03 = ( "--system", 'Nagios', "--slinks", \@a03,); # 4 my %l06 = ( "--link" => "/etc/nagios", "--fold" => "/ +usr/local/private/QA/nagios/etc", ); my %l07 = ( "--link" => "/etc/nagiosql", "--fold" => "/ +usr/local/private/QA/nagios/etc.nagiosql", ); my @a04 = ( \%l06, \%l07, ); my %H04 = ( "--system", 'NagioSQL', "--slinks", \@a04,); # nagiosql is + "debian" centric, it uses debians locations - symlinks to the rescue +! # 5 my %l08 = ( "--link" => "/etc/odbc.ini", "--file" => "/ +usr/local/private/QA/odbc/odbc.ini", ); my @a05 = ( \%l08, ); my %H05 = ( "--system", 'Odbc', "--slinks", \@a05,); # 6 my %l09 = ( "--link" => "/etc/php.ini", "--file" => "/ +usr/local/private/QA/php/php.ini", ); my @a06 = ( \%l09, ); my %H06 = ( "--system", 'Php', "--slinks", \@a06,); # 7 my %l10 = ( "--link" => "/etc/postfix/main.cf", "--file" => "/ +usr/local/private/QA/postfix/main.cf", ); my %l11 = ( "--link" => "/etc/postfix/SSL", "--fold" => "/ +usr/local/private/QA/postfix/SSL", ); my @a07 = ( \%l10, \%l11, ); my %H07 = ( "--system", 'Postfix', "--slinks", \@a07,); # 8 my %l12 = ( "--link" => "/usr/local/syslog-ng", "--fold" => "/ +usr/local/private/QA/syslog-ng", ); my @a08 = ( \%l12, ); my %H08 = ( "--system", 'Syslog-NG', "--slinks", \@a08,); # master array my @LINKS = ( \%H00, \%H01, \%H02, \%H03, \%H04, \%H05, \%H06, \%H07, \%H08, ); my @LITERALS = ( \[ "--system", 'Apache', "--slinks", \( \[ "--link" => "/etc/httpd/SSL", "-- +fold" => "/usr/local/private/QA/apache/SSL", ], \[ "--link" => "/etc/httpd/conf/httpd.conf", "-- +file" => "/usr/local/private/QA/apache/conf/httpd.conf", ], \[ "--link" => "/etc/httpd/conf.d/httpd.conf", "-- +file" => "/usr/local/private/QA/apache/conf.d/nagios.conf", ], ),], \[ "--system", 'Logrotate', "--slinks", \( \[ "--link" => "/etc/logrotate.d/nagios", "-- +file" => "/usr/local/private/QA/logrotate/nagios", ], ),], \[ "--system", 'Mysql', "--slinks", \( \[ "--link" => "/var/lib/mysql", "-- +fold" => "/usr/local/private/QA/mysql", ], ),], \[ "--system", 'Nagios', "--slinks", \( \[ "--link" => "/usr/local/nagios", "-- +fold" => "/usr/local/private/QA/nagios", ], ),], \[ "--system", 'NagioSQL', "--slinks", \( \[ "--link" => "/etc/nagios", "-- +fold" => "/usr/local/private/QA/nagios/etc", ], \[ "--link" => "/etc/nagiosql", "-- +fold" => "/usr/local/private/QA/nagios/etc.nagiosql", ], ),], \[ "--system", 'Odbc', "--slinks", \( \[ "--link" => "/etc/odbc.ini", "-- +file" => "/usr/local/private/QA/odbc/odbc.ini", ], ),], \[ "--system", 'Php', "--slinks", \( \[ "--link" => "/etc/php.ini", "-- +file" => "/usr/local/private/QA/php/php.ini", ], ),], \[ "--system", 'Postfix', "--slinks", \( \[ "--link" => "/etc/postfix/main.cf", "-- +file" => "/usr/local/private/QA/postfix/main.cf", ], \[ "--link" => "/etc/postfix/SSL", "-- +fold" => "/usr/local/private/QA/postfix/SSL", ], ),], \[ "--system", 'Syslog-NG', "--slinks", \( \[ "--link" => "/usr/local/syslog-ng", "-- +fold" => "/usr/local/private/QA/syslog-ng", ], ),], ); ######################################## $ARGV[0] = '--help' if ( $#ARGV == -1 ); ### ($todo, $verb, $html) = &Parse($NAME, "links", \@ARGV); ### &LinksRM($NAME, $SPACE, $verb, $html, \@LITERALS) if ( $todo eq $S +trRemo ); ### &LinksIN($NAME, $SPACE, $verb, $html, \@LITERALS) if ( $todo eq $S +trInst ); print "$NAME: LINKS ============================================= +=====\n"; &LinksLS($NAME, $SPACE, $verb, $html, \@LINKS) if ( $todo eq $StrList +); print "$NAME: LITERALS ============================================= +=====\n"; &LinksLS($NAME, $SPACE, $verb, $html, \@LITERALS) if ( $todo eq $StrLi +st ); ### print "$NAME: Literals ========================================= +=========\n"; ### &LinksLS($NAME, $SPACE, $verb, $html, \@Literals) if ( $todo eq $S +trList ); ######################################## sub LinksLS() { my $name = shift; my $space = shift; my $verb = shift; my $html = shift; my $aref = shift; my $aref_ndx; # index into "master" links array my $hash_ref; my $inner_hash_ref; my $sskey; my $system; my $slinks; my $nlinks; my $s; my $file; my $fold; my $link; for $aref_ndx (0..$#{$aref} ) { print '$$aref[ ' . $aref_ndx . '] = ' . $$aref[$aref_ndx] . "\n"; $hash_ref = $$aref[$aref_ndx]; $system = $hash_ref->{"--system"}; $slinks = $hash_ref->{"--slinks"}; $nlinks = @{$slinks}; $s = ($nlinks == 1) ? "" : "s"; print 'System ' . $system . " requires " . $nlinks . " link$s\ +n"; foreach $sskey ( keys %$hash_ref ) { print "\t\t" . $sskey . " is " . + $hash_ref->{$sskey} . "\n"; } undef $file; undef $fold; undef $link; foreach $inner_hash_ref ( @{$slinks} ) { print "\t" . $inner_hash_ref . "\n"; $link = $inner_hash_ref->{"--link"}; $file = $inner_hash_ref->{"--file"}; $fold = $inner_hash_ref->{"--fold"}; if ( not defined $link ) { die "$name: for $system --link is undfined, aborting!\ +n"; } elsif ( not defined $file and not defined $fold ) { die "$name: for $system both --file and --fold are und +fined, aborting!\n"; } elsif ( not defined $file ) { print "\tFOLDER LINK: $link/ -> $fold/\n"; } elsif ( not defined $fold ) { print "\tFILE LINK: $link -> $file\n"; } } } }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |