|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
|
Re: [PLUG] Re: plug Digest, Vol 16, Issue 31
|
In the message dated: Mon, 27 Mar 2006 11:16:47 EST,
The pithy ruminations from Eric on
<Re: [PLUG] Re: plug Digest, Vol 16, Issue 31> were:
=> Quoting Jeff Watson <redgibson@gmail.com>:
=>
=> > I am having an issue with this part of my perl script.
=> >
=> > # eat output file, chop all lines but the load lines
=> > my $output = "c:\\TEST.out";
=> >
=> > if($ARGV[0]) {
=> > print $ARGV[0] . "\n";
=> > my $input = $ARGV[0];
^^^
=> > print $input
=> > }
=> > else {
=> > print "BAD INPUT: Missing parse target filename!";
I'd change this to:
print "BAD INPUT: Missing parse target filename!\n";
=> > exit(0);
=> > }
=> >
=> > # ASSUME $input = C:\test.chm' !!
=> > #open(MYFILE, 'c:\test.chm'); # THIS WORKS!!!
=> > open(MYFILE, $input); # THIS FAILES!! :-/
=> >
=> > while(<MYFILE>) #Drops out on this line becuase the file is not able to be
=> > opened?
=> > .......
=> >
=> >
=> > -Jeff Watson
=> > -RedGibson@gmail.com
=>
=> Jeff:
=>
=> I always code the open statements as:
=>
=> open MYFILE, "<$input" or die "Cannot open file $input" ;
Good idea. I'd suggest:
open MYFILE, "<$input" or die "Cannot open file $input: $!" ;
adding "$!" will give you the error message as returned by "open()"--this lets
you distinguish between things like "permission denied" and "no such file or
directory".
If you do make this change, and run the script, the error message _will_ tell
you (implicitly) what's going wrong. For example:
./myscript fubar # run the script, specifying "fubar" as the
# name of the input filename
fubar
Cannot open file : No such file or directory at myscript line 16.
Look closely...note that the filename is missing in the error statement...
Now, wasn't that filename assigned to $input within the decision block
following the test "if($ARGV[0])"? Yes, there was an assignment of "fubar" to
$input, and the single line of text (fubar) shows that...so, why was that
variable blank when the open() statement was executed?
Hint...how was the $input variable defined?
[SPOILER FOLLOWS]
According to the camel book (2nd ed., p. 108):
A _my_ or a _local_ declares the listed variables (in the case of
_my_, or the values of the listed global variables (in the case of
_local_), to be confined to the enclosing block...
So, you're assigning a value to $input, but that value only persists within the
block, and it disappears by the time you want to use $input in the open()
statement.
Mark
=> so when it cannot open the file it will tell you.
=>
=> I don't use perl in Windows a lot but I'd try double back-slash '\\'
=> in the file name because perl may be seeing the single backslash as
=> an escape for the next char when you pass it to the open function.
=>
=> Eric
=>
=>
-----
Mark Bergman Biker, Rock Climber, Unix mechanic, IATSE #1 Stagehand
http://wwwkeys.pgp.net:11371/pks/lookup?op=get&search=bergman%40merctech.com
I want a newsgroup with a infinite S/N ratio! Now taking CFV on:
rec.motorcycles.stagehands.pet-bird-owners.pinballers.unix-supporters
15+ So Far--Want to join? Check out: http://www.panix.com/~bergman
___________________________________________________________________________
Philadelphia Linux Users Group -- http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug
|
|