tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
35
Task/Object-serialization/Perl/object-serialization-1.pl
Normal file
35
Task/Object-serialization/Perl/object-serialization-1.pl
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
package Greeting;
|
||||
sub new {
|
||||
my $v = "Hello world!\n";
|
||||
bless \$v, shift;
|
||||
};
|
||||
sub stringify {
|
||||
${shift()};
|
||||
};
|
||||
};
|
||||
{
|
||||
package Son::of::Greeting;
|
||||
use base qw(Greeting); # inherit methods
|
||||
sub new { # overwrite method of super class
|
||||
my $v = "Hello world from Junior!\n";
|
||||
bless \$v, shift;
|
||||
};
|
||||
};
|
||||
{
|
||||
use Storable qw(store retrieve);
|
||||
package main;
|
||||
my $g1 = Greeting->new;
|
||||
my $s1 = Son::of::Greeting->new;
|
||||
print $g1->stringify;
|
||||
print $s1->stringify;
|
||||
|
||||
store $g1, 'objects.dat';
|
||||
my $g2 = retrieve 'objects.dat';
|
||||
|
||||
store $s1, 'objects.dat';
|
||||
my $s2 = retrieve 'objects.dat';
|
||||
|
||||
print $g2->stringify;
|
||||
print $s2->stringify;
|
||||
};
|
||||
25
Task/Object-serialization/Perl/object-serialization-2.pl
Normal file
25
Task/Object-serialization/Perl/object-serialization-2.pl
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use MooseX::Declare;
|
||||
|
||||
class Greeting {
|
||||
use MooseX::Storage;
|
||||
with Storage('format' => 'JSON', io => 'File');
|
||||
has string => (is => 'ro', default => "Hello world!\n");
|
||||
}
|
||||
class Son::Of::Greeting extends Greeting {
|
||||
has string => (is => 'ro', default => "Hello from Junior!\n");
|
||||
}
|
||||
|
||||
my $g1 = Greeting->new;
|
||||
my $s1 = Son::Of::Greeting->new;
|
||||
|
||||
print $g1->string;
|
||||
print $s1->string;
|
||||
|
||||
$g1->store('object1.json');
|
||||
my $g2 = Greeting->load('object1.json');
|
||||
|
||||
$s1->store('object2.json');
|
||||
my $s2 = Son::Of::Greeting->load('object2.json');
|
||||
|
||||
print $g2->string;
|
||||
print $s2->string;
|
||||
Loading…
Add table
Add a link
Reference in a new issue