Click to See Complete Forum and Search --> : ARGV perl problem


Gotenks
12-04-2000, 04:47 PM
Yes yes, I'm back again with yet another problem I can't figure out =D. Granted, it's probably something sitting right under my nose, but after an hour and a half of looking and not coming up with anything I thought I'd dump it off here for you fellas to take a look at. Ok, whenever I try to run this script I get a "Use of uninitialized value at ./merger.pl line 10." I will bold lines 9-11 for aesthetics.


#!/usr/bin/perl -w
#
# Original code: Scott
# Modifications/Improvements: Brett & YaRness(LNO)

use strict;

# Read in command-line args here.
my $argument = $ARGV[0];
chomp;

if (!$ARGV[0]) {
print "\nFormat: ./merger.pl <client>\n";
exit;
}

my $txtfile = "0";
my $txtfiledata = "0";

#Open up a fresh index and slap a header on it. (Only needed once per issue of course)
open (TITLES, ">index.html") | | die "Can't open index.html ($!)";
open (TITLEHEADER, "${argument}_titleheader.txt") | | die "Can't open ${argument}_titleheader.txt ($!)";
print TITLES <TITLEHEADER>;
close (TITLEHEADER) | | die "Can't close ${argument}_titleheader.txt ($!)";

#Open up the indexdata template along with our header adn footer here.
#No need to have them in the while() if we only need the i/o for close/open once each
open (IDATA, "${argument}_indexdata.txt") | | die "Can't open ${argument}_indexdata.txt ($!)";
open (HEADER, "${argument}_header.txt") | | die "Can't open ${argument}_header.txt ($!)";
open (FOOTER, "${argument}_footer.txt") | | die "Can't open ${argument}_footer.txt ($!)";

#Lets run through the .article files in this directory
while (defined($txtfile = glob("*") ) ) {
if ($txtfile =~ /^.*\.article$/) {

#Open up the .txt article and get her ready
open (TXTFILE, "$txtfile") | | die "Can't open $txtfile ($!)";
my @txtfiledata = <TXTFILE>;
my $newtxtfile = $txtfile;
$newtxtfile =~ tr/A-Za-z0-9//cd;

#Open a new file (using the stripped $newtxtfile), insert the standard header.
open (NEWTXTFILE, ">new_$newtxtfile.html") | | die "Can't open new_$newtxtfile ($!)";
print NEWTXTFILE <HEADER>;

#Lets spit the index listing into the new index.
print TITLES <IDATA>;

#Copy line for line of data from old txt article to new html article.
foreach $txtfiledata (@txtfiledata) {
chomp $txtfiledata;
print NEWTXTFILE "$txtfiledata".'<br>'."\n";
}

#Lets close off this article with a footer
print NEWTXTFILE <FOOTER>;

#Closeing the txt article, and the newly created html article.
close (TXTFILE) | | die "Can't close $txtfile ($!)";
close (NEWTXTFILE) | | die "Can't close new_$newtxtfile ($!)";
}
}

#Lets spit out the footer at the end of the index and wrap up this issue.
open (TITLEFOOTER, "${argument}_titlefooter.txt") | | die "Can't open ${argument}_titlefooter.txt ($!)";
print TITLES <TITLEFOOTER>;
close (TITLEFOOTER) | | die "Can't close ${argument}_titlefooter ($!)";

#Lets close up our global files.
close (TITLES) | | die "Can't close index.html ($!)";
close (HEADER) | | die "Can't close ${argument}_header.txt ($!)\n";
close (FOOTER) | | die "can't close ${argument}_footer.ext ($!)";
close (IDATA) | | die "Can't close ${argument}_indexdata ($!)";




Any help is much appreciated.

[This message has been edited by Gotenks (edited 04 December 2000).]

YaRness
12-04-2000, 04:55 PM
<edit> err, see next post, this may not apply now that you edited your post :P

hmm

try


my $argument = "$ARGV[0]";
print "\$argument is $argument\n";
exit;


then do "./merger somefilename"

it should print out "$argument is somefilename"

it sounds mostly like you aren't including a parameter when you execute the script, but it could be something else. if you are using this $argument (or whatever you will be using) to get a file name off the command line, then something similar to above ($argument = "$ARGV[0]"; ) will work nicely.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

[This message has been edited by YaRness (edited 04 December 2000).]

YaRness
12-04-2000, 04:57 PM
oh, i see. maybe you mean to do "chomp $argument

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

klamath
12-04-2000, 05:30 PM
my $argument = $ARGV[0];
chomp;


If you don't specifiy an argument to chomp(), it tries to do chomp($_) - but since $_ is not defined yet, you get a warning.


if (!$ARGV[0]) {
print "\nFormat: ./merger.pl <client>\n";
exit;
}


Why did you assign to $argument if you intend to continue referring to $ARGV[0] ? Also, you should use $0 rather than './merger.pl'.

------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)
Looking for an open source project to contribute to? Check out the Better Bulletin Board (http://bbb.sourceforge.net)

[This message has been edited by klamath (edited 04 December 2000).]