Hi guys,
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.
Use of uninitialized value $_ in string at ./test.pl line 32.
Usage: ./test.pl --domain companydomain.com --client company --job-code 12345 --help.
Here is my 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-acces
+s_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();
}
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...
Global symbol "$conf" requires explicit package name at ./test.pl line 59.
Global symbol "$conf" requires explicit package name at ./test.pl line 60.
Global symbol "$conf_bn" requires explicit package name at ./test.pl line 60.
Global symbol "$ltime" requires explicit package name at ./test.pl line 64.
Execution of ./test.pl aborted due to compilation errors.
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. :)
Thanks in advance!
Cheers,
JP
-
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.