How do you define a "strict" pragma within a module?

Usually, when I build a data structure in Perl, I have to declare it from %leaf

before %root

, so I went into a module that would allow me to build from
$seed

before $seed->trunk->branch->leaf

.

It's not difficult with routines AUTOLOAD

and new

. My question for SO is how to determine if a "strict" pragma is being used so that the module operates in a different mode that requires the variables to be "declared" before use, so I don't accidentally assign a value $seed->drunk

when I use strict - suppose the module is called branch

and this is valid syntax for using the module

$seed->declare('trunk');
$seed->trunk(new branch);

$seed->trunk->declare('leaf');
$seed->trunk->leaf("value");

      

How to determine if a strict pragma is in effect in a calling program from a module?

This might not be possible - in that case, I would have to use a static variable to handle module-independent pragmas.

EDITED / POSTSCRIPT:

I coded the original version that does not check for "strictness" or implements the "declare" routine, and realized that the autoloader would not provide a simple enough user syntax if it works by reference, so I wrote it to test for the first parameter and assign the value passed to element in the hash table associated with the object, otherwise, if there was no parameter, it would return the value of the specified element.

So, I am posting the code for the branch module to satisfy your curiosity. Mind you, I have not yet performed a strictness check.

package branch;

sub new
{
    my $type = shift;
    my $self = { };
    bless $self, $type;
    return $self;
}

sub DESTROY
{
    my $self = shift;
    %$self = undef;
}

sub AUTOLOAD
{
    my $self = shift;
    my $value = shift;

    my $sub = $AUTOLOAD;
    my ($type, $PROGRAM) = ($sub =~ /(.*)::(.*)/);

    if( $value ne undef )
    {
        $$self{$PROGRAM} = $value;
        return $value;
    }
    return $$self{$PROGRAM};
}

1;

      

+3


source to share


2 answers


Well, the first thing that would be, what strict? Strict has three sub-pragmas, with their own behavior and bits to check. use strict 'refs'

does not allow you to play lines; use strict 'vars'

does not allow you to access global variables unconditionally, but use strict 'subs'

disables simple words outside of multiple situations. use strict

equates to all three, but none of them really seems close enough to what you are asking it to stand for copying.

So, to answer your question somewhat directly: element [8]

in the list returned caller($i)

is returning the compilation hint bits in effect for the caller of $i

th level. If you look in strict.pm

, you can see the bits that are set by each subrag and check them at the caller level, which corresponds to the code that is actually calling your method.



But back to the starting point, you probably shouldn't be doing this, because that's not that strict. You should either take a parameter in the constructor of the objects that decides if they should behave strictly or not, or if you really need a lexical pragma instead of what follows your objects, you should write your own using the information in perlpragma as a tutorial ... All perls since 5.10 support arbitrary custom pragmas using hash ponytails %^H

that are displayed as a [10]

caller information item .

+6


source


You seem to be confused with the definition of a strict pragma.

If a module is using strict, it does not impose anything on the user of the module. Even if you want to extend the package by subclassing or patching monkeys in additional methods.

use strict

applies only to the file in which it is used. (Or, if used in a pair of curly braces, it only applies up to the closing parenthesis.) So if you expand a package, just do it in a separate file and none of the pragmas applied in the original module will apply to your code. ...




However, it is rarely a good idea not to use strict

. There are occasional tasks where it might be helpful to disable it in a small amount, but the problem you are describing is not one of them.

In particular, if you are creating a deeply nested structure, you do not need to declare every level. Demonstration:

use strict;
use warnings;
use Data::Dumper;

my $root;
$root->{trunk}{branch}{leaf} = 42;

print Dumper($root);

      

+2


source







All Articles