Hello, dear.
I want to share one very simple idea and its implementation in Perl.
The idea is this: there is a certain function that transfers not only the parameters, but also the attributes of these parameters. What could it be? For example, the command and the desired query string. Or the action and the quality of his povtroeny.
')
You can use several options. The most simple and user-friendly form, as I think, should be the solution when visually a property of a specific object is attached to the same object. Something like OOP, only easier, of course.
What can be used for this? Hashes The object here will act as a key, and the attribute will be hash values. Moreover, the program may even not know anything about the existence of objects and their properties - all this can and should be intercepted dynamically.
Let's start with a simple one key, one parameter.
Essence: there are several functions inside another, external. The user calls a function from the external (in our case, it is simple) and passes some parameter to it.
sub Simple {
# ,
#
our %hash = ( @_ );
# $sub - . ,
my ( $sub ) = keys( %hash );
#
eval $sub or print @!;
# ,
sub hello {
my ( $name ) = values( %hash );
printf " Hello, %s!", $name;
}
sub good_beye {
my ( $name ) = values( %hash );
printf " Good Beye, %s!", $name;
}
}
&Simple( hello => 'Alex' );
The second case is a bit more complicated.
The program does not know how much and what data was transferred to it.
The bottom line: the program can greet a person several times in a row. Moreover, with several people, even I would say, with an infinite number of completely different people. And hello with each as many times as she indicated. Names as objects; number of greetings for each name as parameters.
sub Hello {
# ,
my %hash = ( @_ );
#
my @names = keys( %hash );
# -
my @repeat = values( %hash );
#
for( my $i=0; $i<@names; $i++ ) {
$repeat[$i] ||= 1;
for( my $j=0; $j<$repeat[$i]; $j++ ) {
printf " Hello, %s!\n", $names[$i];
}
}
}
&Hello( sasha => 3, sveta => 10, Vasya );