Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Dereferencing in blessed object

by haukex (Archbishop)
on Feb 26, 2021 at 09:20 UTC ( [id://11128822]=note: print w/replies, xml ) Need Help??


in reply to Dereferencing in blessed object

What is causing the lack of dereferencing or what else could I try to debug this problem?

The issue with my ($self, $disp, %v) = @_; and my ($value, %vars) = @_; is the same as Re^7: Preparing data for Template. Building an SSCCE to understand it better is a great approach, but since the point of it is also to debug, make sure you pull out all the stops: In this case, Use strict and warnings, use useful variable names, and don't repeat variable names. See also the Basic debugging checklist.

Here's another way to think about the issue: @_ contains the subroutine arguments, so the first line of reftest can be thought of as my ($value, %vars) = ('testing', $vars);. Drop the $value and you get my %vars = $vars; - there's no dereferencing happening here. In effect, it's the same as my %vars = ( $vars => undef );, which you can see when you dump \%vars with e.g. Data::Dump: you'll see { "HASH(0x...)" => undef }, the hashref was stringified since hash keys are strings.

Also, variable naming/scoping is biting you as well: $$v{'testpage'} aka $v->{testpage} is accessing a scalar $v, which is not defined in sub process, and so apparently you've got a $v somewhere higher up in the code that is either uninitialized and being autovivified here, or it's already a hashref, or, in the worst case, you're not using strict. And note that if $$v{'testpage'} is giving you undef, "<h1>".$testpage."</h1>\n" would have given you warnings accordingly.

As before, you need to either explicitly dereference the hashref via my %hash = %$hashref;, although that makes a (shallow) copy of the hash, or you can save on memory and just use the hashref as-is, which is what you're trying to do in $$v{'testpage'}, but to do that, you should've written my ($self, $disp, $v) = @_; instead.

Just for completeness:

The only difference is that the subroutine is a method in a blessed object...could this really make a difference?

No, method calls don't affect this.

Replies are listed 'Best First'.
Re^2: Dereferencing in blessed object
by Bod (Parson) on Feb 27, 2021 at 22:19 UTC

    Thanks haukex - that's extremely helpful.

    or, in the worst case, you're not using strict.

    The main script has use strict; but it seems that does then apply to the module it calls...I know that now!

    So now I've added use strict; I get a whole bunch of Global symbol requires explicit package name errors even though they all have my or our declarations. I tracked this down to the declaration happening later in the module than methods that are throwing the error.

    It also highlighted another error with this:

    $db =~ s/_.+?_/_$dbname_/;
    The variable $dbname is declared but in the regexp it is looking for $dbname_. I have solved this for now with:
    my $temp = "_$dbname"."_"; $db =~ s/_.+?_/$temp/;
    But - I feel sure there is a more elegant way!

      Use braces:

      $db =~ s/_.+?_/_${dbname}_/;

      🦛

        Or, use a backslash, because TIMTOWTDI:
        $db =~ s/_.+?_/_$dbname\_/;
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Use braces:

        That was my first thought - but I tried it and it didn't work...
        So...plenty of use of Ctrl+Z and I realised what I did wrong!

        $db =~ s/_.+?_/_{$dbname}_/;

        Not quite the same!
        Thanks hippo

      The main script has use strict; but it seems that does then apply to the module it calls...I know that now!

      The scope of strict and many other pragmas like warnings is lexical: if you put it at the top of a file, its scope is only that file, and does not extend to any files included with do, require, or use.

      But - I feel sure there is a more elegant way!

      Though I concur with hippo that braces are easiest here, in the spirit of TIMTOWTDI note there's also the /e modifier: $db =~ s/_.+?_/"_$dbname"."_"/e;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found