varhash question

J

Jeremy Billones

I know very little about perl. (Just wanted to get that out of the way :)

I've been handed a script that, at first, does essentially the following:

$tmpldir="(directory with template files)"
$scriptdir="(some directory)"
opendir (DIR,$tmpldir)
while (defined($file=readdir DIR))
open (FILE, "$tmpldir/$file")
($newfile,$ext) = split/\./, $file)
$newfile = $newfile . "(new ext)"
open (NEWFILE, "> $varhash{'scriptdir'}/$newfile")

The problem is that whenever the script calls $varhash, it generates
null output.

print "$scriptdir" gives me the expected output
print "$varhash{'scriptdir'}" gives me nothing

I can't go through the entire script and replace all the uses of varhash,
I have to make it work.

What should I look to do?

Thanks in advance.

--
Jeremy Billones
"It's a place used the world over where people can come together to bitch about
movies and share pornography together." This is a much more sophisticated idea
of the Net than we find in high-tech cyberthrillers, where the Net is a place
that makes your computer beep a lot. - Roger Ebert on "Jay & Silent Bob..."
 
B

Brian Wakem

Jeremy said:
I know very little about perl. (Just wanted to get that out of the way :)

I've been handed a script that, at first, does essentially the following:

$tmpldir="(directory with template files)"
$scriptdir="(some directory)"
opendir (DIR,$tmpldir)
while (defined($file=readdir DIR))
open (FILE, "$tmpldir/$file")
($newfile,$ext) = split/\./, $file)
$newfile = $newfile . "(new ext)"
open (NEWFILE, "> $varhash{'scriptdir'}/$newfile")

The problem is that whenever the script calls $varhash, it generates
null output.

print "$scriptdir" gives me the expected output
print "$varhash{'scriptdir'}" gives me nothing


Well %varhash is never defined or populated, so putting it in a path to a
file is a bit pointless. Why aren't you just opening
"$scriptdir/$newfile" ?

Why do you expect $varhash{'scriptdir'} to have a value? Give it a value if
you want it to have one.
 
J

Jeremy Billones

Well %varhash is never defined or populated, so putting it in a path to a
file is a bit pointless. Why aren't you just opening
"$scriptdir/$newfile" ?

I dunno. I put in a call to the tech rep for the company that gave us
this script, but they haven't called back.
Why do you expect $varhash{'scriptdir'} to have a value? Give it a value if
you want it to have one.

Brian, let me back up a bit. I have no idea what varhash is. I presumed
it was a procedure call of some sort that took the variable scriptdir
and did something to it (maybe converting it or something) before throwing
it into the command to move the file.

If it's not a reserved word, then I'm really lost.

--
Jeremy Billones
"It's a place used the world over where people can come together to bitch about
movies and share pornography together." This is a much more sophisticated idea
of the Net than we find in high-tech cyberthrillers, where the Net is a place
that makes your computer beep a lot. - Roger Ebert on "Jay & Silent Bob..."
 
J

Jürgen Exner

Jeremy said:
I know very little about perl. (Just wanted to get that out of the
way :)

I've been handed a script that, at first, does essentially the
following:

$tmpldir="(directory with template files)"
$scriptdir="(some directory)"
opendir (DIR,$tmpldir)
while (defined($file=readdir DIR))
open (FILE, "$tmpldir/$file")
($newfile,$ext) = split/\./, $file)
$newfile = $newfile . "(new ext)"
open (NEWFILE, "> $varhash{'scriptdir'}/$newfile")

The problem is that whenever the script calls $varhash, it generates
null output.

If you had
use warnings;
use strict;
in your script, then chances are high that at least you would have gotten a
warning or error message telling you what is going on.
print "$scriptdir" gives me the expected output

Ok, good.
print "$varhash{'scriptdir'}" gives me nothing

Well, impossible to tell for sure, but at least in the little snippet you
provided the hash %varhash isn't declared anywhere let alone the value for
$varhash{scriptdir}. Anyway $varhash{scriptdir} has absolutely no
relationship to $scriptdir in the first place. Those are two totally
separate and independant variables.
I can't go through the entire script and replace all the uses of
varhash, I have to make it work.

Then first of all you should find out what the hash %varhash is used for.

jue
 
T

Tad McClellan

Jeremy Billones said:
I know very little about perl. (Just wanted to get that out of the way :)

I've been handed a script


You will need to learn a bit about Perl if you plan
to modify a Perl program.

that, at first, does essentially


"essentially" is very often not good enough when discussing code,
as the devil is often in the details.

If you can post a short and complete program that we can run, you
are pretty much guaranteed that someone will be able to solve
your problem.

Have you seen the Posting Guidelines that are posted here frequently?

the following:

$tmpldir="(directory with template files)"


syntax error, therefore this is not a valid Perl program.

$scriptdir="(some directory)"
opendir (DIR,$tmpldir)


You should check the return value from opendir() to see if
you actually got what you asked for.

while (defined($file=readdir DIR))
open (FILE, "$tmpldir/$file")

You should always, yes *always*, check the return value from open():

open (FILE, "$tmpldir/$file") or
die "could not open '$tmpldir/$file' $!";

($newfile,$ext) = split/\./, $file)
$newfile = $newfile . "(new ext)"
open (NEWFILE, "> $varhash{'scriptdir'}/$newfile")


You are looking something up in the %varhash, but you have
never put anything into that hash.

The problem is that whenever the script calls $varhash,


You cannot "call" a variable, you call subroutines.

There *is no* $varhash in your code!

There is, however, an access of a value in the hash named %varhash.

it generates
null output.


Seems sensible, since that is exactly what %varhash contains.

Did you mean to put something into %varhash before looking in %varhash?

I can't go through the entire script and replace all the uses of varhash,


Why not?

I have to make it work.


Errr, yes.

What should I look to do?


Post a short and complete program that we can run that demonstrates
the problem.
 
I

Ian Wilson

Jeremy said:
I dunno. I put in a call to the tech rep for the company that gave us
this script, but they haven't called back.




Brian, let me back up a bit. I have no idea what varhash is. I presumed
it was a procedure call of some sort that took the variable scriptdir
and did something to it (maybe converting it or something) before throwing
it into the command to move the file.

If it's not a reserved word, then I'm really lost.

In your script, varhash is the name of a variable of a type known as an
associative array (or "hash"). You can read about this by typing this
command: perldoc perldata

An oversimplified way of explaining this might be:
A normal array stores values indexed by an integer
$myarray[3] = 'foo';
An associative array can store values indexed by a string key
$myhash{'name'} = 'fred';

Hashes are a very useful and widely used data type in Perl.

Somewhere earlier in your script, values are probably assigned to that
variable:
$varhash{'notscriptdir'} = '/foo/bar/baz';
Except that in your case the key 'scriptdir' is never assigned a value.
 
J

Jeremy Billones

Have you seen the Posting Guidelines that are posted here frequently?

I have not. (I need to figure out what penance I'm going to have to
do once this episode is concluded.)
You should always, yes *always*, check the return value from open():

Oh, that's the least of the sins this script is committing :)

Not My Script.
Post a short and complete program that we can run that demonstrates
the problem.

Between the fact that I don't "own" the code, and the fact that I'm
pretty sure I can't legally go into detail on what it's being used
for, I'm tyring to do the best I can to provide what information I
can.

I do appreciate all the help the various posters have provided. Knowing
that varhash is Just Another Variable and not a reserved word helps me
quite a bit. Now to try and find where (if anywhere) that variable
was initially defined.

Thanks, all.

--
Jeremy Billones
"It's a place used the world over where people can come together to bitch about
movies and share pornography together." This is a much more sophisticated idea
of the Net than we find in high-tech cyberthrillers, where the Net is a place
that makes your computer beep a lot. - Roger Ebert on "Jay & Silent Bob..."
 
X

xhoster

I know very little about perl. (Just wanted to get that out of the way
:)

I've been handed a script that, at first, does essentially the following:

Interesting. What kind of job do you have? Tomorrow will you be handed a
nuclear reactor to run? Did you have to excise a brain tumor yesterday?
....

I can't go through the entire script and replace all the uses of varhash,

Well, if you can't do the obvious thing to fix the problem, then what
exactly *can* you do? If we will be laboring under bizarre irrational
constraints, it would help to know in advance what those constraints are.
I have to make it work.

What should I look to do?

As "work" seems to be undefined, define "work" to mean whatever it is that
the script currently does. QED


Xho
 
U

usenet

Jeremy said:
Knowing > that varhash is Just Another Variable and not a reserved
word helps me quite a bit.

I suspect that 'varhash' is a hash of all the program control
parameters. It is probably defined and populated as one of the first
things your script does (but it might not be at the top of the script,
since it may be part of an "init" subroutine, and the sub may be near
the end of the script). You will probably find a section of code that
looks something like this:

$varhash{'logfile'} = "/var/log/whatever.log";
$varhash{'userid'} = "fred";
$varhash{'password'} = "secret";
$varhash{'scriptdir'} = "/home/fred/perl";

etc.

Though it may use a different syntax like this:
%varhash = qw(
logfile /var/log/whatever.log
userid fred
password secret
scriptdir /home/fred/perl
);

or there are (many) other ways to populate a hash.
 
J

Jeremy Billones

I've discovered that a major portion of the problem is a conflict within
Cygwin as to how it accesses files and filenames, which is propogated
all the way through the application (which assumes a UNIX file layout
and which cygwin doesn't quite support completely).

*Those* scripts I think I can edit.

Again, thanks all for putting up with (yet another) Perl newbie.

--
Jeremy Billones
"It's a place used the world over where people can come together to bitch about
movies and share pornography together." This is a much more sophisticated idea
of the Net than we find in high-tech cyberthrillers, where the Net is a place
that makes your computer beep a lot. - Roger Ebert on "Jay & Silent Bob..."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top