use strick & hash ref

T

Tom

Hi

I do not understand the error:

Cannot use string to reference has while use strict

This makes no sense.
The correct hash reference is i.e., per the manual


$newval = $hash{DATA};

This works fine and correctly polulates the hash and works in a tied hash
system, up until I uncomment use strict.

Can someone please point me to docs with the reason and corect way to
reference hashes.

Thanks,
Tom
 
D

Darin McBride

Tom said:
Hi

I do not understand the error:

Cannot use string to reference has while use strict

This makes no sense.
The correct hash reference is i.e., per the manual


$newval = $hash{DATA};

This works fine and correctly polulates the hash and works in a tied hash
system, up until I uncomment use strict.

Can someone please point me to docs with the reason and corect way to
reference hashes.

What you're doing is correct ... given the lack of context. But we'd
need more code to see what you're doing wrong. Try reproducing your
error with the smallest amount of code - that excersise alone may point
you to your problem. If not, then post the complete sample code that
we can use to run on our systems.

Thanks,
 
B

Brian McCauley

Tom said:
I do not understand the error:

Cannot use string to reference has while use strict

That is not a Perl error message. Please always post error messages
verbatim. Do not paraphrase.

There is, a very similar Perl error message:

Can't use string ("....") as a HASH ref while "strict refs" in use

Is that perhaps what you saw?
This makes no sense.

So why did you post it? :)
$newval = $hash{DATA};

This works fine and correctly polulates the hash

It would not be correct for the above line to populate the hash.
and works in a tied hash system, up until I uncomment use strict.

The above line would not (directly) cause the "Can't use string as a
HASH ref" error. If it looks like it is then there's something odd
going on. You say %hash is tied? What is it tied to - looks like
the tied hash implementation object is carp()ing.

Please generate a _minimal_ but _complete_ script to illustrate your
question.

Post it here verbatim.

See posting guidelines for further advice on helping people to help you.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Anno Siegel

Tom said:
Hi

I do not understand the error:

Cannot use string to reference has while use strict

This makes no sense.

No, it doesn't, not with your code below.
The correct hash reference is i.e., per the manual
^^^^
I don't like that "i.e.". Please don't show an arbitrary example, show
the code that gave you the error message.
$newval = $hash{DATA};

That code doesn't give you the error message you (vaguely) quote.
This works fine and correctly polulates the hash and works in a tied hash
system, up until I uncomment use strict.

Can someone please point me to docs with the reason and corect way to
reference hashes.

Start with "perldoc strict". If you can "use" it, you can also "perldoc"
it.

Anno
 
T

Tom

Thanks guys.

I'll try to debug and post tonight some code.
The script is 15K lines, so I'll have to reproduce the error in example.

The weird thing is, this seems to come and go, i.e.e not happen every run.

Tom
 
D

Darin McBride

Tom said:
Thanks guys.

I'll try to debug and post tonight some code.
The script is 15K lines, so I'll have to reproduce the error in example.

In the future, I would recommend *starting* with "use strict" rather
than attempting to fit it in later. That way, when you get the problem
after adding only 100 lines, you'll know the problem is related to
those 100 lines.
The weird thing is, this seems to come and go, i.e.e not happen every run.

What you're looking for is that you possibly have two variables with
the same name, one $hash, the other %hash. While this is perfectly
valid, you do need to be always aware of when you're using $hash->{...}
(using $hash) vs $hash{...} (using %hash). Thus, in general, I'd
suggest attempting to avoid this.
 
T

Tom

What you're looking for is that you possibly have two variables with
the same name, one $hash, the other %hash. While this is perfectly
valid, you do need to be always aware of when you're using $hash->{...}
(using $hash) vs $hash{...} (using %hash). Thus, in general, I'd
suggest attempting to avoid this.

Thanks, I think you are right, but I just got time to check it.
I did have it on, but had a problem and turned it off, then forgot to
uncomment it.

Thanks,
Tom
 
T

Tom

What you're looking for is that you possibly have two variables with
the same name, one $hash, the other %hash. While this is perfectly
valid, you do need to be always aware of when you're using $hash->{...}
(using $hash) vs $hash{...} (using %hash). Thus, in general, I'd
suggest attempting to avoid this.


Hi

O.K., this makes semse, but what does not make sense is, I was originally
only using $hash{....} and was unable to get the hash to save.
so, I swithed to this:

$nasstk->{DAILY} = join(":",@daily);

This is local, and assigned to tied hash:

$ndbhsh{$keyval} = \%$nasstk;


Works fine, but the hash is tied like this:

# Tie Database
tie(%ndbhsh,"MLDBM",$ndbfile,O_RDWR | O_CREAT) || die "Can't tie junk:
$!";

$nasstk = $ndbhsh{$keyval};


So, this is reverse order, here in order:

use strict;

my %ndbhsh;
# my $nbdhsh;
# my %nasstk = ();
my $nasstk;

my @dailty;
my $val1 = 1;
my $val2 = 2;

# Tie Database
tie(%ndbhsh,"MLDBM",$ndbfile,O_RDWR | O_CREAT) || die "Can't tie junk:
$!";

$nasstk = $ndbhsh{$keyval};

# Build Hash Entry
@daily = ();
push(@daily,$val1,$va2);


$nasstk->{DAILY} = join(":",@daily);

$ndbhsh{$keyval} = \%$nasstk;

untie %ndbhsh;

Now, I've tred just using %hash for all hash declarations and referencing
via $hash{...}, but when I did this, no data would make it into the tied
disk hash, i.e. data was undefined.

If I use all $hash->{...}, all I get are these:
Global symbol "$ndbhsh" requires explicit package name at ./fs.pl line 762.


I followed everything form examples out there, except in all the examples,
it never shows the correct way to declare everything.

Can someone please help?

Also, what is the difference between these two different ways of declaring
hashes?

Thanks,
Tom
 
T

Tom

use strict;

my %ndbhsh;
# my $nbdhsh;
# my %nasstk = ();
my $nasstk;

my @dailty;
my $val1 = 1;
my $val2 = 2;

# Tie Database
tie(%ndbhsh,"MLDBM",$ndbfile,O_RDWR | O_CREAT) || die "Can't tie junk:
$!";

$nasstk = $ndbhsh{$keyval};

# Build Hash Entry
@daily = ();
push(@daily,$val1,$va2);


$nasstk->{DAILY} = join(":",@daily);

$ndbhsh{$keyval} = \%$nasstk;

untie %ndbhsh;

Now, I've tred just using %hash for all hash declarations and referencing
via $hash{...}, but when I did this, no data would make it into the tied
disk hash, i.e. data was undefined.

If I use all $hash->{...}, all I get are these:
Global symbol "$ndbhsh" requires explicit package name at ./fs.pl line 762.


I followed everything form examples out there, except in all the examples,
it never shows the correct way to declare everything.

Can someone please help?

Also, what is the difference between these two different ways of declaring
hashes?

Thanks,
Tom

So, by the way, it only works when both of the following are declared:
my %nasstk = ();
my $nasstk;

Which I do not understand.
 
T

Tom

So, by the way, it only works when both of the following are declared:
my %nasstk = ();
my $nasstk;

Which I do not understand.


I only wish I knew what the hell I keep doing to make this work, then not
work, then work again, when "use strict" is included.
It works all the time with it off.
Too strange.
I have diffed files during working and non, and nothing is different.
 
U

Uri Guttman

T> I only wish I knew what the hell I keep doing to make this work, then not
T> work, then work again, when "use strict" is included.
T> It works all the time with it off.
T> Too strange.
T> I have diffed files during working and non, and nothing is different.

is this mod_perl? if so, it may be running cached versions which have
strict off. have you tested your code with perl -c? that is always a
good thing to do when cleaning up strict errors.

uri
 
T

Tom

Uri Guttman said:
T> I only wish I knew what the hell I keep doing to make this work, then not
T> work, then work again, when "use strict" is included.
T> It works all the time with it off.
T> Too strange.
T> I have diffed files during working and non, and nothing is different.

is this mod_perl? if so, it may be running cached versions which have
strict off. have you tested your code with perl -c? that is always a
good thing to do when cleaning up strict errors.


I'm not sure what that is, but I'll try -c tomorrow night.
Thanks,
Tom
 
T

Tom

This is too much.

One time (or even several runs) I run the program, it runs fine.
The next time, ater no changes, it gets this:

Can't use string ("") as a HASH ref while "strict refs" in use at ./fs.pl
line 2288.

Thus is plain BS.
I have searched documentation and the net for two weeks now and nothing that
devuldges what is causing this.

I have followed eample code to the tee.

Any ideas at all?

Thanks,
Tom
 
G

Gunnar Hjalmarsson

Tom said:
One time (or even several runs) I run the program, it runs fine.
The next time, ater no changes, it gets this:

Can't use string ("") as a HASH ref while "strict refs" in use at
./fs.pl line 2288.

Would you mind letting us know which of the lines you posted is line 2288?
Thus is plain BS.

No comments at this time.
 
T

Tom

Uri Guttman said:
T> I only wish I knew what the hell I keep doing to make this work, then not
T> work, then work again, when "use strict" is included.
T> It works all the time with it off.
T> Too strange.
T> I have diffed files during working and non, and nothing is different.

is this mod_perl? if so, it may be running cached versions which have
strict off. have you tested your code with perl -c? that is always a
good thing to do when cleaning up strict errors.


Everything checks OK.
I'm following examples to a tee, as well.
http://jobs.perl.org
 
B

Ben Morrow

Tom said:
Can't use string ("") as a HASH ref while "strict refs" in use at ./fs.pl
line 2288.

Thus is plain BS.

Err... no. The Computer Is Always Right. ;)

Assumming this is line 2288, replace it with
$nasstk or die "No entry for $keyval";
ref $nasstk or die "Weird entry for $keyval: $nasstk";
$nasstk->{DAILY} = join ":", @daily;

Ben
 
U

Uri Guttman

T> I only wish I knew what the hell I keep doing to make this work,
T> then not work, then work again, when "use strict" is included. It
T> works all the time with it off. Too strange. I have diffed files
T> during working and non, and nothing is different.

T> Everything checks OK.
T> I'm following examples to a tee, as well.

and can you answer my questions? and examples are not always correct.

uri
 
T

Tom

Assumming this is line 2288, replace it with
$nasstk or die "No entry for $keyval";
ref $nasstk or die "Weird entry for $keyval: $nasstk";
$nasstk->{DAILY} = join ":", @daily;

Ben

I accidently sent this directly to Ben, sorry.

Hi Ben

Here is the result:

No entry for ENTRY1 at ./fs.pl line 2291.

Of course...doh!
I have absolutley no idea what happened to it.
It really was there, honest :)
This problem has been coming and going so much, I really thought there was
more to it than this simple cause.
I basically spent all day on this, which I had wanted to spend making more
progress and doing some other things.

Thank you so much for taking the time you did to help!
Tom
 
T

Tom

No entry for ENTRY1 at ./fs.pl line 2291.

I also don't know how I missed protecting against this.
I did it in three other big subs, but not two others.

Thanks again Ben!

Tom
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top