Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my $str = str::new('<encode this later>');
- my $html = "<html>$str</html>";
- print $html; # <html><encode this later></html>
- print $html->encode; # <html><encode this later></html>
- #!/usr/bin/perl
- use strict;
- use warnings;
- use 5.010;
- use Data::Dumper; $Data::Dumper::Sortkeys=1;
- my $str1 = str::new('foo');
- my $str2 = str::new('bar');
- my $good1 = "$str1 $str2";
- my $good2;
- $good2 = $good1;
- my($good3, $good4);
- $good3 = "$str1 a";
- $good4 = "a $str1";
- my($bad1, $bad2, $bad3);
- $bad1 = "a $str1 a";
- $bad2 = "$str1 $str2";
- $bad3 = "a $str1 a $str2 a";
- say Dumper { GOOD => [$good1, $good2, $good3], BAD => [$bad1, $bad2, $bad3] };
- $bad1 = ''."a $str1 a";
- $bad2 = ''."$str1 $str2";
- $bad3 = ''."a $str1 a $str2 a";
- say Dumper { BAD_GOOD => [$bad1, $bad2, $bad3] };
- package str;
- use Data::Dumper; $Data::Dumper::Sortkeys=1;
- use strict;
- use warnings;
- use 5.010;
- use Scalar::Util 'reftype';
- use overload (
- '""' => &stringify,
- '.' => &concat,
- );
- sub new {
- my($value) = @_;
- bless((ref $value ? $value : $value), __PACKAGE__);
- }
- sub stringify {
- my($str) = @_;
- #say Dumper { stringify => @_ };
- if (reftype($str) eq 'ARRAY') {
- return join '', @$str;
- }
- else {
- $$str;
- }
- }
- sub concat {
- my($s1, $s2, $inverted) = @_;
- #say Dumper { concat => @_ };
- return new( $inverted ? [$s2, $s1] : [$s1, $s2] );
- }
- 1;
- $VAR1 = {
- 'BAD' => [
- 'a foo a',
- 'foo bar',
- 'a foo a bar a'
- ],
- 'GOOD' => [
- bless( [
- bless( [
- bless( do{(my $o = 'foo')}, 'str' ),
- ' '
- ], 'str' ),
- bless( do{(my $o = 'bar')}, 'str' )
- ], 'str' ),
- $VAR1->{'GOOD'}[0],
- bless( [
- $VAR1->{'GOOD'}[0][0][0],
- ' a'
- ], 'str' )
- ]
- };
- $VAR1 = {
- 'BAD_GOOD' => [
- bless( [
- '',
- bless( [
- bless( [
- 'a ',
- bless( do{(my $o = 'foo')}, 'str' )
- ], 'str' ),
- ' a'
- ], 'str' )
- ], 'str' ),
- bless( [
- '',
- bless( [
- bless( [
- $VAR1->{'BAD_GOOD'}[0][1][0][1],
- ' '
- ], 'str' ),
- bless( do{(my $o = 'bar')}, 'str' )
- ], 'str' )
- ], 'str' ),
- bless( [
- '',
- bless( [
- bless( [
- bless( [
- bless( [
- 'a ',
- $VAR1->{'BAD_GOOD'}[0][1][0][1]
- ], 'str' ),
- ' a '
- ], 'str' ),
- $VAR1->{'BAD_GOOD'}[1][1][1]
- ], 'str' ),
- ' a'
- ], 'str' )
- ], 'str' )
- ]
- };
Add Comment
Please, Sign In to add comment