<?xml version="1.0" encoding="windows-1252"?>
<node id="148730" title="Magic Status Variable" created="2002-03-01 18:56:08" updated="2005-08-11 08:45:12">
<type id="1748">
sourcecode</type>
<author id="85567">
particle</author>
<data>
<field name="doctext">
&lt;code&gt;
package MagicStatus;
require 5.6.0;
use Tie::Scalar;
our @ISA = qw( Tie::Scalar );
(our $Version) = '$Revision: 1.2 $' =~ /([\d.]+)/;

sub mywarn {warn @_};

sub TIESCALAR 
{
    my $class   = shift;
	my $watch   = $_[0] || undef;
	my $val     = $_[1] || '';
    my $method  = $_[2] || \&amp;mywarn;
    my $message = $_[3] || undef;
    my $self = { 
    	WATCH   =&gt; $watch, 
        VAL     =&gt; $val, 
        METHOD  =&gt; $method, 
        MESSAGE =&gt; $message,
    };
    return bless $self, $class;
}

sub FETCH { shift-&gt;{VAL} }

sub STORE 
{
    my $self   = shift;
    $self-&gt;{VAL} = shift;
	my $value = defined $self-&gt;{WATCH} ? $self-&gt;{WATCH} : 'undef' ;
	my $message;
    if( defined $self-&gt;{MESSAGE} ) 
    { 
    	$message = $self-&gt;{MESSAGE} 
    }
    else
    {
		$message = "MagicStatus(" . $value . ") at ". 
			(caller)[1] . ", " . 
			(caller)[0] . ", " . 
			(caller)[2] . "\n";
    }
	defined $self-&gt;{WATCH} 
		? $self-&gt;{VAL} eq $self-&gt;{WATCH} 
        	&amp;&amp; do{ &amp;{ $self-&gt;{METHOD} }($message) }
		: defined $self-&gt;{VAL} 
        	|| do{ &amp;{ $self-&gt;{METHOD} }($message) }
    ;
}

1;
__END__

=head1 NAME

MagicStatus - Scalar variable that B&lt;warn&gt;s on a specified value

=head1 SYNOPSIS

  use MagicStatus;

  my $oops = sub { print "oops!\n"; warn shift };

  tie my $status, 'MagicStatus', undef, 0, $oops, "look what i found!\n";
  # value to watch for is set to undef, 
  # initial value is set to 0, 
  # method is set to $oops
  # message is set to "look what i found!\n"

  $status = 1; 
  # status is now 1

  $status = system($command, @args);
  # status contains the return code from $comand

  $status = undef; 
  # warns with "oops!\nlook what i found!\n"

  $status = 'magic!';
  # status is now 'magic!'

=head1 DESCRIPTION

This module allows you to tie a scalar variable that will B&lt;warn&gt; when it is assigned a value you wish to watch. One use might be to track and debug the return status from a function.

=over 4

=item C&lt;WATCH&gt;

Use the WATCH hash key to specify the value to watch for. If the variable is set to this value, the METHOD coderef is called. If no value is specified for the WATCH key, it defaults to I&lt;undef&gt;.

=item C&lt;VAL&gt;

Use the VAL hash key to specify the initial value for the scalar variable. If no initial value is set, it defaults to I&lt;''&gt;. 

=item C&lt;METHOD&gt;

Use the METHOD hash key to specify a coderef to call if the WATCH value is encountered. If no value for the METHOD key is set, it defaults to B&lt;\&amp;mywarn&gt;, which is a wrapper around B&lt;warn&gt;.

=item C&lt;MESSAGE&gt;

Use the MESSAGE hash key to specify a message to send to the METHOD coderef. If no value is set, it defaults to C&lt;MagicStatus(WATCH) at FILE, FUNCTION, LINE&gt; (where FILE is the filename, FUNCTION is the function name, and LINE is the line number where the WATCH value was set.

=back

=head1 BUGS

None known so far. Please let me know if you find any.

=head1 AUTHOR

particle

=head1 COPYRIGHT

Copyright 2002 particle. All rights reserved.

This library is free software, you may redistribute it and/or modify it under the same terms as Perl itself.

=head1 SEE ALSO 

perl(1), Tie::Scalar(3pm).

=cut
&lt;/code&gt;</field>
<field name="codedescription">
MagicStatus.pm allows you to tie a scalar variable that will &lt;strong&gt;warn&lt;/strong&gt; when it is assigned a value you wish to watch. One use might be to track and debug the return status from a function.&lt;p&gt;

since it's taking more than a month for me to get my CPAN id, i'll post here first. all comments are welcome.&lt;p&gt;

&lt;strong&gt;Update:&lt;/strong&gt; added [rjray]'s suggestions, released as Revision 1.1&lt;p&gt;

&lt;strong&gt;Update:&lt;/strong&gt; added [abaxaba]'s suggestion (silly me, i should have caught that!)&lt;p&gt;</field>
<field name="codecategory">
Miscellaneous</field>
<field name="codeauthor">
/msg particle</field>
</data>
</node>
