Damian Conway
and 1 contributors

NAME

Var::Mystic - Monitor your state, tracked in colour

VERSION

This document describes Var::Mystic version 0.000003

SYNOPSIS

    use Var::Mystic;

          my $untracked = 'Changes to this variable are not tracked';

    track my $tracked   = 'Changes to this variable are tracked';


    $untracked = 'new value';    # Variable updated silently

    $tracked   = 'new value';    # Change reported on STDERR


    # Can track any type of scoped variable declaration...

    track our   @array;
    track state %hash;

    # Can also track variables after they're declared...

    track $scalar;

    track @array;
    track $array[ $index ];   # Just track this one array element

    track %hash;
    track $hash{ $key };      # Just track this one hash entry

DESCRIPTION

This module allows you to track changes to the values of individual variables, reporting those changes to STDERR.

INTERFACE

The module adds a new keyword: track. When you place that keyword in front of a variable or variable declaration (my, our, or state), then any subsequent changes to that variable are reported on STDERR.

If the Term::ANSIColor module is installed, these reports are printed in glorious technicolor.

Permanent vs lexical tracking

Normally, once you start tracking a particular variable, it is tracked until it ceases to exist, even if you start tracking it half way through a program or in an inner scope.

However, if you only want to track a variable in a particular lexical scope, you can specify that by adding a secondary keyword after the track keyword:

    track      $var;    // Variable is tracked for the rest of its existence

    track here $var;    // Variable is tracked only in the current lexical scope

Adding a here can be useful when you suspect that your problem is occurring within a particular block, because then you don't have to wade through hundreds of other reports from everywhere else the variable is subsequently used.

Disabling all tracking

If the module is loaded via:

    no Var::Mystic;

the track keyword is still added...but as a silent no-op.

This is useful when you have added tracking to multiple variables and then think you've solved the problem. Rather than removing every track keyword, you can just change the use Var::Mystic to no Var::Mystic, until another problem is encountered.

Legacy interface

The module previously supplied another keyword: mystic.

The declaration:

    mystic $var;

was equivalent to:

    track my $var;

This keyword is still provided for backwards compatibility, but will not be maintained and may be removed in a future release.

DIAGNOSTICS

Every change to a tracked variable is reported to STDERR in the following format:

    #line LINE FILE
    $VARNAME = NEW_VALUE

CONFIGURATION AND ENVIRONMENT

Var::Mystic requires no configuration files or environment variables.

DEPENDENCIES

This module depends on the Keyword::Declare, Data::Dx, and Variable::Magic modules.

The module's test suite depends on the Test::Effects module.

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

This module requires Perl 5.14 or later, and does not work under the 5.20 release of Perl (due to issues in the regex engine that were not resolved until Perl 5.22)

The module uses the "magic" feature of Perl variables (via the Variable::Magic module), so it is constrained by the limitations of the built-in mechanism. The two most obvious of those limitations are:

  • When tracking an entire array, magic only applies to "array-oriented" actions, so only these actions can be reported. Most significantly, that means that any assignment to a single element of the array:

        $array[$index] = $newvalue;     // No report generated

    will not be reported.

    The workaround here is to explicitly track that array element:

        track $array[$index] = $newvalue;     // Report generated
  • When an entry is deleted from a tracked hash:

        delete $hash{$key};

    the change will only sometimes be reported in non-void contexts. Whether void-context deletions are reported depends on which version of Perl you are using, and how it was compiled.

    The obvious workaround here is to ensure that any deletions you definitely want to track are performed in a non-void context:

        scalar delete $hash{$key};

No bugs have been reported.

Please report any bugs or feature requests to bug-var-mystic@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Damian Conway <DCONWAY@CPAN.org>

LICENCE AND COPYRIGHT

Copyright (c) 2020, Damian Conway <DCONWAY@CPAN.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.