Bernhard Bodenstorfer
Bernhard Bodenstorfer

Reputation: 960

perltex to globally use strict

To enforce clean and explicit code, I customarily

use strict;

when programming Perl. I would like to keep this habit in perltex, too.

So where should I put this use strict; statement so that it governs all successive \perldo, \perlnewcommand, \perlnewenvironment, \perlrenewcommand and \perlrenewenvironment invocations in the perltex input file?

The following perltex example file runs without raising an error:

\documentclass[12pt]{article}

\usepackage{perltex}

    \perldo{
        my $scalar = "ok";
        our @array = qw( array is fine );
        %HASH = (
            subject => "hash",
            result => "perfect"
        );
        use strict;
    }

    \perlnewcommand\printscalar{
        return $scalar;
    }

    \perlnewcommand\printarray{
        return join ", ", @array;
    }

    \perlnewcommand\printhash{
        return join ", ", map { sprintf "%s = %s", $_, $HASH{$_} } keys %HASH;
    }

\begin{document}
    Scalar: \printscalar

    Array: \printarray

    Hash: \printhash
\end{document}

It produces something similar to

typeset output text

That no error is raised shows that use strict; in the top \perldo argument is disregarded in the definition of \printscalar. The result also shows that the setting of $scalar was not known there any more because of the my. To avoid such mistakes, I would wish to receive an error

Global symbol "$scalar" requires explicit package name

whenever I forget to specify my or our when introducing a new variable.

A workaround to my problem is to include the statement

use strict;

in all \perldo, ... commands, and this can be done using macros. Nevertheless I wonder whether there is no possibility to avoid such re-statements.

Upvotes: 3

Views: 141

Answers (1)

zdim
zdim

Reputation: 66964

The perltex CTAN documentation gives in section 3.2.3 a list of the loaded modules and pragmas. These include use strict;.

The docs are a little unclear on when this is the default, but it appears to be under --nosafe. Then this option should be the way to toggle it and get those defaults loaded.

Did you try setting it only in the first command used (like \perldo)? That may well be enough.

The --permit option allows "features" described in Opcode module to be specified, what is done using the module Safe. While I don't see how to directly make use of this, the discussion under Safe::reval method may be helpful.

I don't have the module installed here and can't try. I hope that this is of some use.

As the final resort, why not contact the author? You may have revealed a bug (in documentation), since the observed behavior seems to conflict the docs. Also, this may well be feasible to add.

Upvotes: 1

Related Questions