[PerlNET] CIty (Mix Perl & C#)
Program.cs
using MixPerl;
namespace CityClient
{
class Program
{
static void Main(string[] args)
{
City myCity1 = new City("SmallVille", 4);
City myCity2 = new City("Crowdedburg", 40);
myCity1.PrintCityInfo(true);
myCity2.Population = 500000;
myCity2.PrintCityInfo(false);
}
}
}
City.pm
use strict;
use namespace "System";
use PerlNET qw(AUTOCALL );
#Consturctor
=for interface
[interface: pure]
static City(str name, Double population);
=cut
#Properties
=for interface
str Name;
Double Population;
=cut
#Methods
=for interface
void PrintCityInfo(Boolean pretty);
=cut
#Constructor definition
sub new
{
my $self = shift;
my ($name, $pop) = @_;
my $city = {Name => $name, Population => $pop};
bless $city, $self;
return $city;
}
#City Name as readonly property
sub Name
{
my ($self, $val) = @_;
if (defined $val)
{
return; #do nothing, ignore the given value
}
else
{
$self->{Name}; #Get City Name
}
}
#Population as read write property
#City Name as readonly property
sub Population
{
my ($self, $val) = @_;
if (defined $val)
{
$self->{Population} = $val; #Set Population
}
else
{
$self->{Population}; #Get Population
}
}
##Print City Info
sub PrintCityInfo
{
my($self, $val) = @_;
if ($val eq PerlNET::true)
{
print "------------------\n";
}
print "City of $self->{Name} with " .
" population of $self->{Population} \n";
if ($val eq PerlNET::true)
{
print "------------------\n\n";
}
}