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

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
  • Comment on Re^2: FATAL ERROR: Can't use string ("HASH(0x875fffc)") as a HASH ref while "strict refs" in use at
  • Select or Download Code

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

    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.

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

      Sorry, I didn't undestand what you mean .

      The code I have posted previously has the only purpose to show the problem, so if it should seems somewhere strange , don't care to much.

      BTW : I have just discoverred that if I remove the tie directive tie %$self, 'Tie::File::AsHash', $fileTie, split => '=' or die "Problem tying %$self: $!";

      then the program work well, also the 3th print show the contents of the hash.

      I don't known if that is also wath you mean in your comment when you said

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

      mybe that the tie of blessed $self corrupt some data the pointer

      in that case I will use another hash for the persistence of data

        Sorry, I was presuming you knew Tie::File::AsHash was an interface to a file.

        $self->{'shared_data'} = $p1; means "put $p1 in the file".

        $p2 = $self->{'shared_data'}; means "get $p2 from the file".

        Thats an error, you're supposed to write
        use Tie::File::AsHash; tie my %hash, 'Tie::File::AsHash', 'filename', split => ':' or die "Problem tying %hash: $!";
        no dereferencing nonsense.
Re^3: 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 12:48 UTC

    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:

    #!/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 " +Problem 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; }

    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.
Re^3: FATAL ERROR: Can't use string ("HASH(0x875fffc)") as a HASH ref while "strict refs" in use at
by sgifford (Prior) on Jun 17, 2008 at 15:19 UTC
    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.

      I suppose the error was that when I write a reference to a tied hash the reference isteslf loose its object, it's just simply became a string whith an hex address.

      I used separate hash both to store data on file with Tie::File::AsHash and to share with process using IPC::Shareable.

      As said by some of you the reference are not managed, so I must take care of it.

      Now everything works well. Thank to everybody

      You' re right.

      I was wrong just because I think that Tie should manage also inner hash. Now it's all more clear and I fix my program so it now works as expected.

      Thanks to everyone.

        sub ab { $hash = { abc => 'cde', }; return "$hash is the value"; } $str = ab(); with $str how can i access the has of abc and its value. Since, its hash ref in a string..