Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Invalid Reference Use - warning Noob

by jorba (Sexton)
on Oct 15, 2017 at 19:10 UTC ( [id://1201408]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to construct a moose class that represents a single record retrieved from the db. I'm getting a problem which I'm pretty sure is from the attempt to assign values to a hash "property" of the class. But I could be wrong ;o)

package AXRecord; # Our libraries use lib 'C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities'; use AXControl; use AXSQL; use Moose; use DBI; # Attributes has 'Name' => (is => 'rw', isa => 'Str', required => 1); has 'Fields' => (is => 'ro', isa => 'Hash'); has 'FieldCount' => (is => 'ro', isa => 'Num'); has 'Changed' => (is=>'rw', isa => 'Boolean'); has 'Where' => (is => 'rw', isa => 'Str'); has 'ControlObject' => (is => 'rw', isa => 'Object', required => 1); has 'Keys' => (is => 'rw', isa=>'Array'); # Contains a single record sub BUILD # Constructor { my $self; $self = shift; if (defined $self->Where) { $self->Select(); } } # Insert the record sub Insert { } #Delete using the keys in the record sub Delete { } #Update using the keys and values in the record sub Update { } #Save the record sub Save { } sub Select { my ($SQLStr, $Cnt, $sql, @Values, $self, $Col, @Flds, $i); $self = shift; $SQLStr = "SELECT * FROM " . $self->Name . ' ' . $self->Where; $sql = AXSQL->new(ControlObject => $self->ControlObject, SQLString + => $SQLStr); #construct a hash using the metadata and the data from the actual +table $self->Fields = {}; @Values = $sql->Fetch(); #Get field Names $sql = AXSQL->new(ControlObject => $self->ControlObject, SQLString + => "show fields from '" . $self->Name . "'"); @Flds = $sql->fetch(); # Construct the hash for ($i..$#Flds) { $self->Fields{$Flds[$i]} = $Values[$i]; } $self->Keys = (); $sql = AXSQL->new(ControlObject => $self->ControlObject, SQLString + => "SELECT column_name FROM information_schema.`key_column_usage` WH +ERE table_name = '" . $self->name . "' order by ordinal_position"); while (($Col) = $sql.fetch()) { push $self->Keys, $Col; } $Cnt = $self->Fields; $self->FieldCount = $Cnt; $self->Changed = -1; }

The error messages I am getting all seem to be caused by what I'm doing wrong in this statement

$self->Fields{$Flds[$i]} = $Values[$i];

All of the declaration errors are about variables that work fine before that line. The hash Fields is a property of the class and I'm trying to populate it with fieldname/value pairs Here are the error messages (moose warnings excluded)

C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities>perl -w AXRecord.pm push on reference is experimental at AXRecord.pm line 83. syntax error at AXRecord.pm line 76, near "->Fields{" Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 79. Global symbol "$sql" requires explicit package name (did you forget to + declare " my $sql"?) at AXRecord.pm line 80. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 80. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 80. Global symbol "$Col" requires explicit package name (did you forget to + declare " my $Col"?) at AXRecord.pm line 81. Global symbol "$sql" requires explicit package name (did you forget to + declare " my $sql"?) at AXRecord.pm line 81. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 83. Global symbol "$Col" requires explicit package name (did you forget to + declare " my $Col"?) at AXRecord.pm line 83. Global symbol "$Cnt" requires explicit package name (did you forget to + declare " my $Cnt"?) at AXRecord.pm line 86. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 86. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 87. Global symbol "$Cnt" requires explicit package name (did you forget to + declare " my $Cnt"?) at AXRecord.pm line 87. Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at AXRecord.pm line 88. syntax error at AXRecord.pm line 90, near "}" AXRecord.pm has too many errors. C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities>

Replies are listed 'Best First'.
Re: Invalid Reference Use - warning Noob
by choroba (Cardinal) on Oct 15, 2017 at 19:16 UTC
    Fields is an accessor, i.e. a method. You can't treat a method call as a hash. But the method returns a hash reference, so you can dereference it:
    #!/usr/bin/perl use warnings; use strict; { package MyStruct; use Moose; has fields => ( is => 'rw' ); __PACKAGE__->meta->make_immutable; } my $s = 'MyStruct'->new(fields => { a => 0, b => 1 }); $s->fields->{b}++; print $s->fields->{b};

    Update: Another option is to use traits:

    #!/usr/bin/perl use warnings; use strict; { package MyStruct; use Moose; has fields => ( is => 'rw', isa => 'HashRef', traits => ['Hash'], handles => { set_fields => 'set', get_fields => 'get', }, ); __PACKAGE__->meta->make_immutable; } my $s = 'MyStruct'->new(fields => { a => 0, b => 1 }); $s->set_fields(b => $s->get_fields('b') + 1); print $s->get_fields('b');

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      That has solved the problem. many thanx J.
Re: Invalid Reference Use - warning Noob
by Corion (Patriarch) on Oct 15, 2017 at 19:13 UTC

    Did you maybe mean to access the hash reference?

    $self->Fields->{ ... }
Re: Invalid Reference Use - warning Noob
by Mr. Muskrat (Canon) on Oct 16, 2017 at 18:16 UTC

    It looks like you are trying to write your own object-relational mapping (ORM) tool. Do yourself (and your employer) a favor and check out DBIx::Class before you go any further down that rabbit hole. (The Grue dwells in the darkness.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1201408]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-04-18 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found