in reply to When is strict 'refs triggered

It's not possible for us to run your example, as you didn't provide the code to populate the database.

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.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: When is strict 'refs triggered
by redtux (Sexton) on May 22, 2020 at 14:46 UTC
    In the original code the sub routine using %actionsw does work (just dropped sequence and reran a few times)

    DB creation

    DB create line (in psql)

    sudo su postgres

    /usr/pgsql-11/bin/psql template1

    CREATE USER usevideo blah etc;

    CREATE DATABASE usevideos WITH OWNER = usevideo ENCODING = 'UTF8' LC_COLLATE = 'en_GB.UTF-8' LC_CTYPE = 'en_GB.UTF-8' TABLESPACE = pg_default CONNECTION LIMIT = -1;

    The db connection line is

    use DBI qw(:sql_types); my $dbh=DBI->connect('dbi:Pg:dbname=usevideos;host=192.168.1.3','dbuser','dbpass',{AutoCommit=>1,RaiseError=>1,PrintError=>0});