exempting a piece of perl code from strict pragma

Viewed 77

I have a legacy piece of perl code that uses perl DBI with constructs like

$db->bind_param(1, $some_blob, {TYPE => SQL_BLOB});

where SQL_BLOB is a bareword. I would like to use strict pragma in the same file, but it then complains about the bareword. ('Bareword "SQL_BLOB" not allowed while "strict subs" in use') Can I somehow exempt this line from strict checking?

2 Answers

While you can indeed turn off the strict pragma, that's not going to fix your problem. You're going to just pass the value "SQL_BLOB" as a type, but bind_param isn't going to recognize it.

You need to add an import:

use DBI qw(:sql_types);

If you're already useing DBI, then add :sql_types to the things you import from it.

The strict pragma is lexical

The effect of this pragma is limited to the current file or scope block.

and it can also be turned off within a scope. So

use strict;

...

{
    no strict 'subs';

    $db->bind_param(1, $some_blob, {TYPE => SQL_BLOB});
}

# strict is back on

Edit

However, the above will only tolerate the bareword while bind_param still won't know what that (integer constant) is. This is solved by import-ing such constant(s), by using the :sql_types import tag; see DBI Constants. That is anyway a far superior way to satisfy the strict.

Thanks to Andy Lester for bringing this up, see their answer

Related