[26396] in Perl-Users-Digest
Perl-Users Digest, Issue: 8567 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 25 14:05:21 2005
Date: Tue, 25 Oct 2005 11:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 25 Oct 2005 Volume: 10 Number: 8567
Today's topics:
FAQ 8.44 How do I tell the difference between errors fr <comdog@pair.com>
Re: varhash question (Jeremy Billones)
Re: varhash question <jurgenex@hotmail.com>
Re: varhash question <no@email.com>
Re: varhash question <scobloke2@infotop.co.uk>
Re: varhash question <tadmc@augustmail.com>
Re: varhash question (Jeremy Billones)
Re: varhash question xhoster@gmail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 25 Oct 2005 16:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@pair.com>
Subject: FAQ 8.44 How do I tell the difference between errors from the shell and perl?
Message-Id: <djlkvl$3gm$1@reader2.panix.com>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.
--------------------------------------------------------------------
8.44: How do I tell the difference between errors from the shell and perl?
(answer contributed by brian d foy, "<bdfoy@cpan.org>"
When you run a Perl script, something else is running the script for
you, and that something else may output error messages. The script might
emit its own warnings and error messages. Most of the time you cannot
tell who said what.
You probably cannot fix the thing that runs perl, but you can change how
perl outputs its warnings by defining a custom warning and die
functions.
Consider this script, which has an error you may not notice immediately.
#!/usr/locl/bin/perl
print "Hello World\n";
I get an error when I run this from my shell (which happens to be bash).
That may look like perl forgot it has a print() function, but my shebang
line is not the path to perl, so the shell runs the script, and I get
the error.
$ ./test
./test: line 3: print: command not found
A quick and dirty fix involves a little bit of code, but this may be all
you need to figure out the problem.
#!/usr/bin/perl -w
BEGIN {
$SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; };
$SIG{__DIE__} = sub{ print STDERR "Perl: ", @_; exit 1};
}
$a = 1 + undef;
$x / 0;
__END__
The perl message comes out with "Perl" in front. The BEGIN block works
at compile time so all of the compilation errors and warnings get the
"Perl:" prefix too.
Perl: Useless use of division (/) in void context at ./test line 9.
Perl: Name "main::a" used only once: possible typo at ./test line 8.
Perl: Name "main::x" used only once: possible typo at ./test line 9.
Perl: Use of uninitialized value in addition (+) at ./test line 8.
Perl: Use of uninitialized value in division (/) at ./test line 9.
Perl: Illegal division by zero at ./test line 9.
Perl: Illegal division by zero at -e line 3.
If I don't see that "Perl:", it's not from perl.
You could also just know all the perl errors, and although there are
some people who may know all of them, you probably don't. However, they
all should be in the perldiag manpage. If you don't find the error in
there, it probably isn't a perl error.
Looking up every message is not the easiest way, so let perl to do it
for you. Use the diagnostics pragma with turns perl's normal messages
into longer discussions on the topic.
use diagnostics;
If you don't get a paragraph or two of expanded discussion, it might not
be perl's message.
--------------------------------------------------------------------
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
------------------------------
Date: Tue, 25 Oct 2005 15:12:50 -0000
From: billones@radix.net (Jeremy Billones)
Subject: Re: varhash question
Message-Id: <11lsiritgcklbd3@corp.supernews.com>
In article <3s6vhsFmjmtoU1@individual.net>, Brian Wakem <no@email.com> 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..."
------------------------------
Date: Tue, 25 Oct 2005 15:18:17 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: varhash question
Message-Id: <Z0s7f.16177$hP6.9971@trnddc05>
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
------------------------------
Date: Tue, 25 Oct 2005 16:22:28 +0100
From: Brian Wakem <no@email.com>
Subject: Re: varhash question
Message-Id: <3s70tkFmqs2nU1@individual.net>
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
------------------------------
Date: Tue, 25 Oct 2005 15:32:57 +0000 (UTC)
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: Re: varhash question
Message-Id: <djlj79$6f3$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>
Jeremy Billones wrote:
> In article <3s6vhsFmjmtoU1@individual.net>, Brian Wakem <no@email.com> 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.
------------------------------
Date: Tue, 25 Oct 2005 10:31:48 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: varhash question
Message-Id: <slrndlsjv4.fl6.tadmc@magna.augustmail.com>
Jeremy Billones <billones@radix.net> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 25 Oct 2005 16:37:07 -0000
From: billones@radix.net (Jeremy Billones)
Subject: Re: varhash question
Message-Id: <11lsnpj3qcejt45@corp.supernews.com>
In article <slrndlsjv4.fl6.tadmc@magna.augustmail.com>,
Tad McClellan <tadmc@augustmail.com> 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..."
------------------------------
Date: 25 Oct 2005 17:04:07 GMT
From: xhoster@gmail.com
Subject: Re: varhash question
Message-Id: <20051025130407.700$b2@newsreader.com>
billones@radix.net (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
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8567
***************************************