varhash question

Discussion in 'Perl Misc' started by Jeremy Billones, Oct 25, 2005.

  1. 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..."
    Jeremy Billones, Oct 25, 2005
    #1
    1. Advertising

  2. Jeremy Billones

    Brian Wakem Guest

    Jeremy Billones wrote:

    > 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.




    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Wakem, Oct 25, 2005
    #2
    1. Advertising

  3. In article <>, Brian Wakem <> wrote:
    >Jeremy Billones wrote:
    >
    >> 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" ?


    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..."
    Jeremy Billones, Oct 25, 2005
    #3
  4. Jeremy Billones wrote:
    > 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
    Jürgen Exner, Oct 25, 2005
    #4
  5. Jeremy Billones

    Brian Wakem Guest

    Jeremy Billones wrote:


    >>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.



    It's a hash.

    http://www.google.co.uk/search?q=perl tutorial hash


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Wakem, Oct 25, 2005
    #5
  6. Jeremy Billones <> wrote:
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 25, 2005
    #6
  7. Jeremy Billones

    Ian Wilson Guest

    Jeremy Billones wrote:
    > In article <>, Brian Wakem <> wrote:
    >
    >>Jeremy Billones wrote:
    >>
    >>
    >>>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" ?

    >
    >
    > 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.
    >


    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.
    Ian Wilson, Oct 25, 2005
    #7
  8. In article <>,
    Tad McClellan <> wrote:
    >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.)

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

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


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

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

    >
    >Why not?


    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..."
    Jeremy Billones, Oct 25, 2005
    #8
  9. Jeremy Billones

    Guest

    (Jeremy Billones) wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Oct 25, 2005
    #9
  10. Jeremy Billones

    Guest

    Jeremy Billones wrote:
    > 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.
    , Oct 25, 2005
    #10
  11. 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..."
    Jeremy Billones, Oct 25, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    573
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    634
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    927
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    403
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    818
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page