Problems with fields 
Author Message
 Problems with fields

I've run into problems with the Fields module.  Here's some test code:

        #!perl -w
        use strict;

        package Blat;
        use vars '%FIELDS';                     # or -w complains
        use fields qw/foo bar/;

        sub new
        {       bless [0, 1, 2];

        package main;

        my Blat $rb = new Blat;
        print '$rb is a reference to ', ref $rb, "\n";
        print 'Sum of %$rb is ', $rb->{foo} + $rb->{bar}, "\n";

        sub go_bang()
        {       print "Now inside go_bang\n";
                print '$rb is a reference to ', ref $rb, "\n";
                print 'Indexes: ', $rb->[1] + $rb->[2], "\n";
                $rb->{Baz} = 3;
                print 'Hash keys: ', $rb->{foo} + $rb->{bar};


The code in package Blat just sets up a small module with some fields
and a 'new' method.  The first three lines after we switch back to
package main show that the fields work as expected.

Function go_bang() is where it all starts to fall apart.  Inside the
function, you can access the fields of the Blat object using numeric
indexes, but not using hash keys.  The line

        $rb->{Baz} = 3;

should be a compile-time error because Blat objects don't have a Baz
field, but it compiles silently.  Instead, it dies at run time,

        Can't coerce array into hash at line 22.

If you comment this line out, the next line, which uses valid field
names, will die with the same error.

I realise that I could make the test code behave more or less as
expected by making Blat::new return a blessed pseudo-hash, but two
important advantages of fields would be lost: compile-time checking of
field names, and increased speed.

What's going on?  My guess is that Perl is forgetting about the
Blatness of $rb when it enters the function.

I'm using ActiveState's latest and greatest on NT:

        This is perl, version 5.005_02 built for MSWin32-x86-object
        (with 1 registered patch, see perl -V for more detail)

        Copyright 1987-1998, Larry Wall

        Binary build 509 provided by ActiveState Tool Corp.
        Built 13:37:15 Jan  5 1999

[Licence and FAQ information omitted]

Markus Laker,
Contax Ltd.

Sun, 29 Jul 2001 03:00:00 GMT  
 Problems with fields
[posted & mailed]

:       sub new
:       {       bless [0, 1, 2];
:       }

        Make this:

        sub new {
            my Blat $self = bless [0, 1, 2], shift;
            return $self;


BSD:  A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts.  Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.)  The full chemical name is "Berkeley Standard Distribution".

Tue, 31 Jul 2001 03:00:00 GMT  
 Problems with fields


>    Make this:

>    sub new {
>        my Blat $self = bless [0, 1, 2], shift;
>        return $self;
>    }

Tried that: makes no difference.  The code still bombs out with

        Can't coerce array into hash at line 23.

The fields work perfectly outside &go_bang; inside the function, they
mysteriously stop working.  That suggests there may be a bug in
pseudo-hashes -- which are, after all, a new and experimental feature.


Tue, 31 Jul 2001 03:00:00 GMT  
 [ 3 post ] 

 Relevant Pages 

1. Mail::Mailer problem - to: field twice, subject: field ignored

2. HELP.... problem splitting fields

3. problem searching fielded data

4. Putative bug inheriting private fields in fields/base pragma in 5.8.0

5. Highlit fields in 'format' fields

6. _value_ -1 in the first field of GLOBAL multi-field arrays

7. Curses Module - How to manage fields fields/values

8. Iterate through ADO RS Fields (Field Collection)

9. split on comma seperated fields, with quoted fields allowed

10. Updating HTML Output Fields Based On Input Fields

11. Database Field Manipulation Problem

12. Problem with use fields and something other than qw()


Powered by phpBB® Forum Software