lonewolf28 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Fellow Monks, I was playing around with OO perl and i wanted to test if i could replicate __str__ operator method in python with Perl OO. So i created a trivial code to test it out. The code doesn't throw any error, however, i don't get any output either.
#!/usr/bin/perl use v5.22; use overload '""' => 'stringify'; use Data::Dumper; package oo_test{ sub new{ my $class = shift; my $self = {}; bless $self, $class; return $self; } sub getter{ my $self = shift; while( my $line = <DATA> ){ chomp $line; $self->{$.} = $line; } return $self; } sub stringify{ my $self = shift; return join(" ", values %$self ); } }; package main; my $test = oo_test->new(); $test->getter(); print $test->stringify; __DATA__ Hi How Are You
I'm sure that i'm making some mistakes. I'd like to admit that my OO perl is bit shaky.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Operator overloading question
by BrowserUk (Patriarch) on Oct 26, 2015 at 03:41 UTC | |
by lonewolf28 (Beadle) on Oct 26, 2015 at 13:17 UTC | |
|
Re^2: Operator overloading question
by shmem (Chancellor) on Oct 26, 2015 at 06:02 UTC | |
by lonewolf28 (Beadle) on Oct 26, 2015 at 13:19 UTC |