Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

FATAL ERROR: Can't use string ("HASH(0x875fffc)") as a HASH ref while "strict refs" in use at

by earlati2 (Beadle)
on Jun 17, 2008 at 08:59 UTC ( [id://692444]=perlquestion: print w/replies, xml ) Need Help??

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

FATAL ERROR: Can't use string ("HASH(0x875fffc)") as a HASH ref while "strict refs" in use at
hi, have problem pointing a hash inside the $selh hash of an object

my problem is this:

my %shared_data = ( 'dtLastRcvMsg' => time, 'dummy2' => 33, 'dummy' => 5 ); my $p1 = \%shared_data; mylog( "P1: ($p1) " . Dumper( $p1 ));
--------------------------------------------------- ==> this is ok ( print the right hash ) --------------------------------------------------- 2008.6.17 10:38:29 pid=10663 P1: (HASH(0x875fffc)) $VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213691909 }; ---------------------------------------------------
$self->{'shared_data'} = $p1; my $p2 = $self->{'shared_data'}; mylog( "P2: ($p2)" . Dumper( $p2 ));
after I assign the reference of hash to $self->{'shared_data'} and then to $p2 I get a different set of data here wath the Dumper show me: 2008.6.17 10:38:29 pid=10663 P2: (HASH(0x875fffc))$VAR1 = 'HASH(0x875fffc)'; The ref. address seems the same (0x875fffc) but the data inside seems lost What I'm wrong in thsi code ? Any suggestions on that ? Regards and thank for the help .

Replies are listed 'Best First'.
Re: FATAL ERROR: Can't use string ("HASH(0x875fffc)") as a HASH ref while "strict refs" in use at
by BrowserUk (Patriarch) on Jun 17, 2008 at 09:16 UTC

    You're either not showing us all the relevant code, or you have subtly changed it as you've entered it here, because what you;ve posted works as expected:

    use Data::Dumper; sub mylog{ print @_ } my %shared_data = ( 'dtLastRcvMsg' => time, 'dummy2' => 33, 'dummy' => 5 ); my $p1 = \%shared_data; mylog( "P1: ($p1) " . Dumper( $p1 )); $self->{'shared_data'} = $p1; my $p2 = $self->{'shared_data'}; mylog( "P2: ($p2)" . Dumper( $p2 )); __END__ c:\test\691728>..\junk.pl P1: (HASH(0x18245bc)) $VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213694057 }; P2: (HASH(0x18245bc))$VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213694057 };

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      you are right. I make a simpler program just to reproduce the problem.

      See the code belowe ( File Test1.pm )

      #!/usr/bin/perl -w # package Test1; use strict; use warnings; use utf8; use CGI; use HTTP::Lite; use HTTP::Request; use LWP::UserAgent; use XML::Simple; use XML::DOM; use Tie::File::AsHash; use IO::Socket; use Sys::Hostname; use IPC::Shareable; use Data::Dumper; my ( $fileTie ) = '/var/log/www/benzopmvutil.dat'; my ( $log_filename ) = '/var/log/www/test1.dat'; my %conf_data = ( 'test1' => 'abc' , 'test2' => '123'); my $test = Test1->new( \%conf_data ); # ================================================ # ================================================ sub new { my $that = shift; my $params = shift; my $class = ref($that) || $that; my $self = {}; bless($self, $class); tie %$self, 'Tie::File::AsHash', $fileTie, split => '=' or die "P +roblem tying %$self: $!"; $self->{counter} += 1; if( $self->{counter} > 9999 ) { $self->{counter} = 1; } $self->{last_call} = scalar localtime; my $s1 = Dumper( $self ); my %ipcshare_options = ( key => 'benz', create => 1, exclusive => 0, mode => 0666, destroy => 0, size => IPC::Shareable::SHM_BUFSIZ() ); my %shared_data = ( 'dtLastRcvMsg' => time, 'dummy2' => 33, 'dummy' => 5 ); my ( $p1, $p2 ); # 1° test : OK $p1 = \%shared_data; $self->mylog( "P1: ($p1) " . Dumper( $p1 )); # 2° test : OK $p2 = $p1; $self->mylog( "P2: ($p2)" . Dumper( $p2 )); # 3° test : NOT-OK $self->{'shared_data'} = $p1; $p2 = $self->{'shared_data'}; $self->mylog( "P2: ($p2)" . Dumper( $p2 )); $p2->{'dtLastRcvMsg'} = time; # tie $self->{shared_data}, 'IPC::Shareable', { %ipcshare_options } + or tie %shared_data, 'IPC::Shareable', { %ipcshare_options } or die "[BenzoPmvUtil::init] shreable tie FAILED ( $ +! ) "; my $ppp = ( tied %shared_data ); # mylog( "TIED: ($!) " . Data::Dumper( \$ppp )); mylog( "SHMEM-ID: " . $ppp->{_shm}->{_id} ); #mylog( "IPC::Shareable::SHM_BUFSIZ: " . IPC::Shareable::SHM_BUFSI +Z() ); return $self; } # ============================================= sub mylog { my $self = shift; my( $strmsg ) = @_; my( $stmp, $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $i +sdst ); if( ! defined($log_filename)) { return }; ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = +localtime(time); $year += 1900; $mon += 1; open( F, ">>$log_filename" ) or open( F, ">$log_filename" ) or war +n "..Non posso aprire il file $log_filename ($!) \n"; print F "[$year.$mon.$mday $hour:$min:$sec] pid=$$ $strmsg) \n"; warn "[$year.$mon.$mday $hour:$min:$sec] pid=$$ $strmsg \n"; close F; }

      =======================================

      The output is the one below

      [enzo@P0101222 benzopmv]$ ./Test1.pm [2008.6.17 13:49:32] pid=27534 P1: (HASH(0x8755f48)) $VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213703372 }; [2008.6.17 13:49:32] pid=27534 P2: (HASH(0x8755f48))$VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213703372 }; [2008.6.17 13:49:32] pid=27534 P2: (HASH(0x8755f48))$VAR1 = 'HASH(0x87 +55f48)'; Can't use string ("HASH(0x8755f48)") as a HASH ref while "strict refs" + in use at ./Test1.pm line 85, <$fh> line 95.

      As you can see , it seem that when I assign the hash reference to an element of another hash I then I read it back, it's value it's corrupted.

      BTW : my revision of perl is the 5.6.1 and I run it on red-hat 7.3 and slackware 8 Belowe the full status of my perl installation, my be it should be usefull

      [enzo@P0101222 cgi-bin]$ perl -V Summary of my perl5 (revision 5.0 version 6 subversion 1) configuratio +n: Platform: osname=linux, osvers=2.4.21-1.1931.2.393.entsmp, archname=i386-lin +ux uname='linux bugs.devel.redhat.com 2.4.21-1.1931.2.393.entsmp #1 s +mp thu aug 14 14:47:21 edt 2003 i686 unknown ' config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dcc=gcc - +Dcf_by=Red Hat, Inc. -Dcccdlflags=-fPIC -Dinstallprefix=/usr -Dprefix +=/usr -Darchname=i386-linux -Dvendorprefix=/usr -Dsiteprefix=/usr -Uu +sethreads -Uuseithreads -Uuselargefiles -Dd_dosuid -Dd_semctl_semun - +Di_db -Di_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Dinc_ver +sion_list=5.6.0/i386-linux 5.6.0' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultipl +icity=undef useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef Compiler: cc='gcc', ccflags ='-fno-strict-aliasing -I/usr/local/include', optimize='-O2 -march=i386 -mcpu=i686', cppflags='-fno-strict-aliasing -I/usr/local/include' ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.3 2.96-11 +3)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=4 alignbytes=4, usemymalloc=n, prototype=define Linker and Libraries: ld='gcc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -ldl -lm -lc -lcrypt -lutil perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl. +a Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynami +c' cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: Built under linux Compiled at Aug 18 2003 16:08:31 @INC: /usr/lib/perl5/5.6.1/i386-linux /usr/lib/perl5/5.6.1 /usr/lib/perl5/site_perl/5.6.1/i386-linux /usr/lib/perl5/site_perl/5.6.1 /usr/lib/perl5/site_perl/5.6.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.6.1/i386-linux /usr/lib/perl5/vendor_perl/5.6.1 /usr/lib/perl5/vendor_perl

        What you expect Perl to do when you print a memory reference?

        >perl -e"print {}" HASH(0x225228)

        That you're hiding the print inside of Tie::File::AsHash doesn't change anything.

        It still works for me? Assuming that the relevant part of the program (going by the comments) is these 3 statements:

        my %shared_data = ( 'dtLastRcvMsg' => time, 'dummy2' => 33, 'dummy' => 5 ); my ( $p1, $p2 ); # 1° test : OK $p1 = \%shared_data; $self->mylog( "P1: ($p1) " . Dumper( $p1 )); # 2° test : OK $p2 = $p1; $self->mylog( "P2: ($p2)" . Dumper( $p2 )); # 3° test : NOT-OK $self->{'shared_data'} = $p1; $p2 = $self->{'shared_data'}; $self->mylog( "P2: ($p2)" . Dumper( $p2 ));

        This is the output I get from them:

        c:\test\691728>..\junk.pl ..Non posso aprire il file /var/log/www/test1.dat (No such file or dir +ectory) print() on closed filehandle F at c:\test\junk.pl line 105. [2008.6.17 13:39:5] pid=2032 P1: (HASH(0x1e74f40)) $VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213706345 }; ..Non posso aprire il file /var/log/www/test1.dat (No such file or dir +ectory) print() on closed filehandle F at c:\test\junk.pl line 105. [2008.6.17 13:39:5] pid=2032 P2: (HASH(0x1e74f40))$VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213706345 }; ..Non posso aprire il file /var/log/www/test1.dat (No such file or dir +ectory) print() on closed filehandle F at c:\test\junk.pl line 105. [2008.6.17 13:39:5] pid=2032 P2: (HASH(0x1e74f40))$VAR1 = { 'dummy2' => 33, 'dummy' => 5, 'dtLastRcvMsg' => 1213706345 };

        I had to disable a few things because I didn't have the relevant modules, but nothing that would affect those 3 outputs. For reference, here's the exact code I ran to get the above output:

        I still suspect that you are simplifying the code for demonstration purposes and failing to demonstrate the problem.

        Your not by any chance trying to share references between processes are you? Because that doesn't work!


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        I've never used Tie::File::AsHash, but it appears to treat a file as a hash of arrays. You are trying to store a hash reference in it, and it's converting that to a string, HASH(0x875fffc). You would need to either convert it into an array before storing it, then convert back to a hash before using it; or else serialize it, perhaps using Storable.
Re: FATAL ERROR: Can't use string ("HASH(0x875fffc)") as a HASH ref while "strict refs" in use at
by rovf (Priest) on Jun 17, 2008 at 09:17 UTC
    Could it be that you haven't initialized $self properly? When I prefix your code with
    $self={};
    I get the same hash printed in both cases.
    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-29 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found