in reply to When is strict 'refs triggered
Moreover, the code is not the same. In the "works" case, you're using $transseqchk->{transchk} as the key in %actionsw, but it contains a hash reference, so the if condition is false and the code dereference is not executed.
Update: If you're curious how I discovered it, here's the code I used. I mocked the database as I guessed it's irrelevant to the question.
#!/usr/bin/perl use warnings; use strict; my $dbh = bless {}, 'My::Mock'; sub My::Mock::selectrow_array { {cmd => 'transref_seq_add'} } sub works { my %transseqchk; my $transref_chksql = 'blah'; my $transseqchk->{transchk}= ( $dbh->selectrow_array($transref_chksql) )[0]; $transseqchk->{transref_seq_add}{cmd} = sub{ my $encode_transref_seqmsql = 'blah'; $dbh->do($encode_transref_seqmsql); }; warn "CMD: $transseqchk->{transchk}->{cmd}."; if ($transseqchk->{transchk}{cmd}){ warn "DOES $transseqchk->{transchk}->{cmd}."; &{$transseqchk->{transchk}->{cmd}}; } } sub doesnt { my $transref_chksql = 'blah'; my $transseqchk->{transchk} = ( $dbh->selectrow_array($transref_chksql) )[0]; my %actionsw = ( transref_seq_add => { cmd => sub { my $encode_transref_seqmsql = 'blah'; $dbh->do($encode_transref_seqmsql); } } ); warn "KEY: $transseqchk->{transchk}."; warn "CMD: $actionsw{ $transseqchk->{transchk} }->{cmd}."; if ($actionsw{ $transseqchk->{transchk} }->{cmd}){ warn "DOESNT $actionsw{$transseqchk->{transchk}}->{cmd}."; &{$actionsw{$transseqchk->{transchk}}->{cmd}}; } } eval { works(); 1 } or warn "w: $@"; eval { doesnt(); 1 } or warn "n: $@";
Output:
CMD: transref_seq_add. at ./1.pl line 18. DOES transref_seq_add. at ./1.pl line 20. w: Can't use string ("transref_seq_add") as a subroutine ref while "st +rict refs" in use at ./1.pl line 21. KEY: HASH(0x563da0230d58). at ./1.pl line 35. Use of uninitialized value in concatenation (.) or string at ./1.pl li +ne 36. CMD: . at ./1.pl line 36.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: When is strict 'refs triggered
by redtux (Sexton) on May 22, 2020 at 14:46 UTC | |
by choroba (Cardinal) on May 22, 2020 at 14:54 UTC |