NAME

Struct::Conditional - A Conditional language within a perl struct.

VERSION

Version 1.01

SYNOPSIS

Quick summary of what the module does.

use Struct::Conditional;

my $c = Struct::Conditional->new();

my $struct = {
	for => {
		key => "countries",
		each => "countries",
		if => {
			m => "Thailand",
			key => "country",
			then => {
				"rank": 1
			}
		},
		elsif => {
			m => "Indonesia",
			key => "country",
			then => {
				rank => 2
			}
		},
		else => {
			then => {
				rank => null
			}
		},
		country => "{country}"
	}
};

$struct = $c->compile($struct, {
	countries => [
		{ country => "Thailand" },
		{ country => "Indonesia" },
		{ country => "Japan" },
		{ country => "Cambodia" },
	]
}, 1);

...

{
	countries => [
		{
			rank => 1,
			country => "Thailand"
		},
		{
			rank => 2,
			country => 'Indonesia'
		},
		{
			rank => undef
			country => 'Japan',
		},
		{
			rank => undef,
			country => 'Cambodia'
		}
	]
};

METHODS

new

Instantiate a new Struct::Conditional object. Currently this expects no arguments.

my $c = Struct::Conditional->new;

compile

Compile a struct containing valid Struct::Conditional markup into a perl struct based upon the passed params.

$c->compile($struct, $params); 

itterate

Itterate a perl struct that contains valid Struct::Conditional markup and return a perl struct based upon the passed params.

$c->itterate($perl_struct, $params);

Markup or Markdown

keywords

if, elsif, else

If, elsif and else conditionals are logical blocks used within Struct::Conditional. They are comprised of a minimum of four parts, the keyword, the expression, 'key' and 'then'. The expression can be any that are defined in the expression section of this document. The 'key' is the value in the params that will be evaluated and the 'then' is the response that is returned if the expression is true.

my $struct = {
	if => {
		m => "Thailand",
		key => "country",
		then => {
			rank => 1
		}
	},
	elsif => {
		m => "Indonesia",
		key => "country",
		then => {
			rank => 2
		}
	},
	else => {
		then => {
			rank => null
		}
	},
	country => "{country}"
};

$struct = $c->compile($struct, {
	country => "Thailand"
}, 1);

...

{
	country => "Thailand",
	rank => 1
}

You can also write this like the following:

my $struct = {
	if => {
		m => "Thailand",
		key => "country",
		then => {
			rank => 1
		},
		elsif => {
			m => "Indonesia",
			key => "country",
			then => {
				rank => 2
			},
			else => {
				then => {
					rank => null
				}
			}
		}
	},
	country => "{country}"
};

$struct = $c->compile($struct, {
	country => "Indonesia"
}, 1);

...

{
	country => "Indonesia",
	rank => 2
}

given

Given conditionals are logical blocks used within Struct::Conditional. They are comprised of a minimum of three parts, the keyword, 'when' and'key'. The 'when' can either be an array or a hash of expression that are defined in the expression section of this document. The 'key' is the value in the params that will be evaluated. You can optionally provide a default which will be used when no 'when' expressions are matched.

my $struct = {
	given => {
		key => "country",
		default => {
			rank => null
		},
		when => [
			{
				m => "Thailand",
				then => {
					rank => 1
				}
			},
			{
				m => "Indonesia",
				then => {
					rank => 2
				}
			}
		]
	},
	country => "{country}"
};

my $compiled = $c->compile($struct, {
	country => "Thailand"
}, 1);

...

{
	country => "Thailand"
	rank => 1
}

You can also write this like the following:

my $struct = {
	given => {
		key => "country",
		when => {
			Thailand => {
				rank => 1
			},
			Indonesia => {
				rank => 2
			},
			default => {
				rank => null
			}
		}
	},
	country => "{country}"
};

my $compiled = $c->compile($struct, {
	country => "Indonesia"
}, 1);

...

{
	country => "Indonesia"
	rank => 1
}

or

The 'or' keyword allows you to chain expression checks, where only one expression has to match.

my $struct = {
	if => {
		m => "Thailand",
		key => "country",
		then => {
			rank => 1,
			country => "{country}"
		},
		or => {
			key => "country",
			m => "Maldives",
			or => {
				key => "country",
				m => "Greece"
			}
		}
	},
};


my $compiled = $c->compile($struct, {
	country => "Greece"
}, 1);

...

{
	country => "Greece"
	rank => 1
}

and

The 'and' keyword allows you to chain expression checks, where only all expression has to match.

my $struct = {
	if => {
		m => "Thailand",
		key => "country",
		then => {
			rank => 1,
			country => "{country}"
		},
		and => {
			key => "season",
			m => "Summer",
		}
	}
};


my $compiled = $c->compile($struct, {
	country => "Thailand",
	season => "Summer"
}, 1);

...

{
	country => "Thailand",
	rank => 1
}

expressions

m

Does the params key value match the provided regex value.

{
	key => $param_key,
	m => $regex,
	then => \%then
}

im

Does the params key value match the provided regex value case insensative.

{
	key => $param_key,
	im => $regex,
	then => \%then
}

nm

Does the params key value not match the provided regex value.

{
	key => $param_key,
	nm => $regex,
	then => \%then
}

inm

Does the params key value not match the provided regex value case insensative.

{
	key => $param_key,
	inm => $regex,
	then => \%then
}

eq

Does the params key value equal the provided value.

{
	key => $param_key,
	eq => $equals,
	then => \%then
}

ne

Does the params key value not equal the provided value.

{
	key => $param_key,
	ne => $equals,
	then => \%then
}

gt

Is the params key value greater than the provided value.

{
	key => $param_key,
	gt => $greater_than,
	then => \%then
}

lt

Is the params key value less than the provided value.

{
	key => $param_key,
	lt => $greater_than,
	then => \%then
}

placeholders

All parameters that are passed into compile can be used as placeholders within the struct. You can define a placeholder by enclosing a key in braces.

{
	placeholder => "{param_key}"
}

loops

for

each

Expects key to reference a array in the passed params. It will then itterate each item in the array and build an array based upon which conditions/expressions are met.

my $struct = {
	for => {
		key => "countries",
		each => "countries",
		country => "{country}"
	}
};

$struct = $c->compile($struct, {
	countries => [
		{ country => "Thailand" },
		{ country => "Indonesia" },
		{ country => "Japan" },
		{ country => "Cambodia" },
	]
}, 1);

...

{
	countries => [
		{
			country => "Thailand"
		},
		{
			country => 'Indonesia'
		},
		{
			country => 'Japan',
		},
		{
			country => 'Cambodia'
		}
	]
};

keys

Expects key to reference a hash in the passed params. It will then itterate keys in the hash and build an hash based upon which conditions/expressions are met.

my $struct = {
	for => {
		key => "countries",
		keys => 1,
		country => "{country}"
	},
};

$struct = $c->compile($struct, {
	countries => {
		1 => { country => "Thailand" },
		2 => { country => "Indonesia" },
		3 => { country => "Japan" },
		4 => { country => "Cambodia" },
	}
}, 1);

...

{
	1 => { country => "Thailand" },
	2 => { country => "Indonesia" },
	3 => { country => "Japan" },
	4 => { country => "Cambodia" },
}

===================================================

my $struct = {
	for => {
		key => "countries",
		keys => "countries",
		country => "{country}"
	},
};

$struct = $c->compile($struct, {
	countries => {
		1 => { country => "Thailand" },
		2 => { country => "Indonesia" },
		3 => { country => "Japan" },
		4 => { country => "Cambodia" },
	}
}, 1);

...

{
	countries => {
		1 => { country => "Thailand" },
		2 => { country => "Indonesia" },
		3 => { country => "Japan" },
		4 => { country => "Cambodia" },
	}
}

AUTHOR

LNATION, <email at lnation.org>

BUGS

Please report any bugs or feature requests to bug-struct-conditional at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Struct-Conditional. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Struct::Conditional

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

This software is Copyright (c) 2020->2021 by LNATION.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)