public
Created

Using Scalar Ties with Moose!

  • Download Gist
Lab_Moosy_ScalarTies.pm
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
=pod
==============================================================================
@nodebunny
 
==============================================================================
=cut
 
package Lab::ScalarTie_Lab;
 
use Moose;
use namespace::autoclean;
use IO::Scalar;
 
our $_STRING = "_STARTER TEXT_";
 
has 'string_ref' => ( is => 'rw', isa => 'ScalarRef' );
 
has 'string' => ( is => 'rw', isa => 'Str',
trigger => sub{
my($self,$str,$old_str) = @_;
$self->string_buffer($str);
#not using $old_str anywhere
});
has 'ioscalar' => (
is => 'rw',
isa => 'Maybe[IO::Scalar]',
default => sub{ return IO::Scalar->new( \$_STRING ) },
 
);
=pod
==============================================================================
Core
==============================================================================
=cut
 
sub BUILD{
my($self) = @_;
$self->string_ref(\$_STRING);
}
=pod
==============================================================================
String Buffer
==============================================================================
=cut
around 'string' => sub{
my($orig,$self,$string) = @_;
return $_STRING unless $string;
$self->string_buffer($string);
return $self->$orig($_STRING);
};
sub string_buffer{
my($self,$str) = @_;
my $ios = $self->ioscalar();
print $ios $str;
}
 
END{
print STDERR ("UNLOADED! ".__PACKAGE__);
}
 
__PACKAGE__->meta->make_immutable;
 
 
1;
__END__
lab_main.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#!/usr/bin/perl -s
use strict;
use warnings;
 
use CGI;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
 
use Lab::ScalarTie_Lab;
 
=pod
==============================================================================
Main
==============================================================================
=cut
 
$| = 1;
$, = " ";
 
my $cgi = CGI->new();
#print headers here to get rid of the 500 for web output
print $cgi->header('text/html');
 
my $lab = Lab::ScalarTie_Lab->new( "string" => "<span style='color:red'>This is my string from new!</span>" );
my $ios = $lab->ioscalar();
print $ios "<span style='color:blue'>This is a newly added String!</span>";
print $ios "Can you do that again please?";
print $lab->string();
 
 
print "<pre>",$cgi->escapeHTML( Dumper( $lab )) ,"</pre>";
print "<pre>",$cgi->escapeHTML( Dumper( $lab->ioscalar() )) ,"</pre>";
 
 
 
exit;
 
1;
__END__

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.