Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am building a simple Mason application to manage the addresses for a friend's organization. The platform is RH 9, Apache 2, Mason, Perl 5.8, and PostGres.

I've run across the error from the title in an odd and frustrating manner. In writing the edit-record function, I created a large form, each element of which I named, for consistency, the same name as the corresponding db field. The user makes whatever edits they wish, and the form get submitted to a second file (submit.html) which in turn hands \%ARGS to the subroutine Green::modifyRec, like so:

submit.html:

[...] if ((my $foo = Green::updateRec(\%ARGS))) { [...]
Green.pm:
sub updateRec { $new = shift @_; [...]
So far, so good, or so I thought. Initially, the troubling line was this one, which returned the error above, specifically as "Can't use string ("precinct") as HASH ref in line 96":
$sth->execute($new->{first}, $new->{middle}, $new->{last}, $new +->{address}, $new->{city}, $new->{state}, $new->{zip}, $new->{email}, + $new->{county}, $new->{precinct}, $new->{region}, $new->{home}, $new +->{other1}, $new->{other2}, $new->{work}, $new->{ext}, $new->{occupat +ion}, $new->{id});
I figured I had a problem with the word itself -- though it's kind of nonintuitive to have it fuss about a string in the MIDDLE of a line -- so I first renamed it, and then removed it entirely.

No joy; it was clearly being vexed by "precinct" 's existence in the HASH on a basic level, not because of something being done here. Additionally, removing this line and simply trying to address the hash in a simple debugging line like this:

print STDERR "\n SHAZAM!!!! $new->{middle} \n";
Still produces the error. Removing "precinct" from the original form, of course, keeps it from complaining about that particular string, but it moves on to another one.

It seems clear that I'm doing something wrong on a basic level; the code is not complex, but I can't for the life of me figure it out. It does seem likely that either (1) someone will slap me with the answer quickly or (2) I'll realize the answer as soon as I press "submit" on this post. I really don't care which.

Thanks in advance.

Chet Farmer

Replies are listed 'Best First'.
Re: Ah, the old "Can't use string ("string") as a HASH ref"
by etcshadow (Priest) on Oct 22, 2003 at 22:42 UTC
    Basically what you claim to be doing shouldn't produce this error. You'd be getting this error if you were calling
    Green::updateRec(%A­RGS)
    (no backslash in front of $ARGS). So a few possibilities:
    • are you calline Green::updateRec in two different places? (the one you've shown us is correct, but the other is wrong, and you're confusing the error messages?)
    • is this "code" maybe being written into a string which will get eval'd later on? If so, could this be an interpolated string, such that "\%ARGS" is interpolated as "%ARGS" (the backslash merely escaping the percent)?
    One thing I can tell is that you are clearly either passing %ARGS as a hash (not a reference) or you are doing something between the
    sub updateRec { $new = shift @_;
    somewhere along the lines of
    $new = (%$new);
    Or, in other words: loading $new with one of the keys in the hash %$new. It'd be a lot easier to make this mistake in your parameter passing, though... so I'd look there first.

    One other question: are you really not using strict?

    sub updateRec { my $new = shift @_;

    ------------
    :Wq
    Not an editor command: Wq
Re: Ah, the old "Can't use string ("string") as a HASH ref"
by antirice (Priest) on Oct 22, 2003 at 22:53 UTC

    It sounds as if you aren't passing a reference to %ARGS to the subroutine but instead %ARGS itself. Observe:

    #!/usr/bin/perl -wl use strict; my %hash = (something => 1,precinct => 2); sub test { my $new = shift; print $new->{'precinct'}; } eval { print "as ref:"; test(\%hash); }; print $@ ? "Died: $@$/":"No error$/"; eval { print "as hash:"; test(%hash); }; print $@ ? "Died: $@$/":"No error$/"; __END__ as ref: 2 No error as hash: Died: Can't use string ("precinct") as a HASH ref while "strict refs" +in use at test.pl line 8.

    Of course, I may be wrong. In either case, I hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Ah, the old "Can't use string ("string") as a HASH ref"
by bobn (Chaplain) on Oct 22, 2003 at 22:36 UTC

    what happens if you just print $new:

    print STDERR "\n SHAZAM!!!! $new \n";
    By any chance, does it equal "precinct"?

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.