perlquestion
juanpablo
<p>Hi guys,</p>
<p>I'm BASH guy actually and I think this is my fifth time writing a Perl script but I'm more than willing to learn this language. In fact, I could see I will be the happiest guy if I have completely learned this. :) Anyway, I'm currently writing a script that will easily add new virtual hosts to httpd.conf. However, I am always getting the "uninitialized value $_" error when starting the script.</p>
<p>Use of uninitialized value $_ in string at ./test.pl line 32.</p>
<p>Usage: ./test.pl --domain companydomain.com --client company [--job-code 12345] [--help].</p>
Here is my code:
<code>
#!/usr/bin/perl
use strict;
use warnings;
use POSIX 'strftime';
use File::Basename;
use File::Copy;
use File::Find::Rule;
use Getopt::Long;
# Variables.
my $dir = '/etc/httpd/conf';
my $filename = 'httpd.conf';
my @object = File::Find::Rule
->file()
->name($filename)
->in($dir);
my $conf = "$_";
my $conf_bn = basename($conf);
my $conf_mtime = (stat($conf))[9];
my $date = strftime '%m%d%Y', localtime;
my $backup = "$conf.$date";
my $backup_bn = basename("$conf.$date");
my $ltime = localtime();
my $domain = undef;
my $client = undef;
my $job_code = undef;
# Display usage.
sub usage {
die "Usage: $0 --domain companydomain.com --client company [--job-code 12345] [--help].\n";
}
&usage() unless @ARGV > 2;
GetOptions (
'domain=s' => \$domain,
'client=s' => \$client,
'job-code:i' => \$job_code,
'help' => &usage()
) or &usage();
sub add_vhost {
if (-e $conf) {
open (CONF, ">>$conf") || die "ERROR: Unable to open $conf_bn.\n";
print CONF "\n# Domain: $domain";
print CONF "# Client: $client";
print CONF "# Job Code: $job_code";
print CONF "# Date: $ltime";
print CONF "<VirtualHost *:1111>";
print CONF " ServerAdmin admin@$domain";
print CONF " DocumentRoot /var/www/html/$domain";
print CONF " ServerName $domain";
print CONF " ServerAlias www.$domain";
print CONF " ErrorLog /var/log/httpd/$domain/$domain-error_log";
print CONF " CustomLog /var/log/httpd/$domain/$domain-access_log common";
print CONF "</VirtualHost>";
print CONF "# End of configuration for $domain.";
close CONF;
if ($? != 0) {
die "ERROR: $!.\n";
}
}
}
foreach (@object) {
# my $conf = "$_";
# my $conf_bn = basename($conf);
# my $conf_mtime = (stat($conf))[9];
# my $date = strftime '%m%d%Y', localtime;
# my $backup = "$conf.$date";
# my $backup_bn = basename("$conf.$date");
# my $ltime = localtime();
# Backup of old configuration.
copy ($conf, $backup) || die "ERROR: Unable to create a backup. $!.\n";
if ($? == 0) {
utime $conf_mtime, $conf_mtime, $backup || die "ERROR: Unable to preserve stats."; # Update time using old file's mtime.
if ($? == 0) {
print "OK: $backup_bn has been created.\n";
}
}
# New configuration update.
&add_vhost();
}
</code>
<p>Since I always get the error, I tried switching the place of the variables above to the body of foreach and when I do that, I'm getting these errors...</p>
<p>Global symbol "$conf" requires explicit package name at ./test.pl line 59.</p>
<p>Global symbol "$conf" requires explicit package name at ./test.pl line 60.</p>
<p>Global symbol "$conf_bn" requires explicit package name at ./test.pl line 60.</p>
<p>Global symbol "$ltime" requires explicit package name at ./test.pl line 64.</p>
<p>Execution of ./test.pl aborted due to compilation errors.</p>
<p>I'm stumped and I couldn't find anything on Google that would help resolve this. By the way, I'm just new here at PerlMonks. :)</p>
<p>Thanks in advance!</p>
<p>Cheers,</p>
<p>JP</p>