I came across this line a lot of times in perl modules but I could not figure out what this exactly means.
my ($self, %myInputs) = @_;
Kindly explain me the statement so that I can proceed.
my keyword in Perl declares the listed variable to be local to the enclosing block in which it is defined. The purpose of my is to define static scoping. This can be used to use the same variable name multiple times but with different values.
@ is used for an array. In a subroutine or when you call a function in Perl, you may pass the parameter list. In that case, @_ is can be used to pass the parameter list to the function: sub Average{ # Get total number of arguments passed. $ n = scalar(@_); $sum = 0; foreach $item (@_){ # foreach is like for loop...
The arrow operator ( -> ) is an infix operator that dereferences a variable or a method from an object or a class. The operator has associativity that runs from left to right. This means that the operation is executed from left to right.
The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }
Im guessing that is one of the first lines in a class method function. That line parses @_
which is the list of the function arguements, and extracts the first param which is always a reference to the object into $self
and extracts the rest of them into a hash %myInputs
. This ofcourse assumes that the function is called with the arguements in hash format, like in the below Perl/Tk function
$mw->Button(-text => "RIGHT", -command => sub { exit })
->pack(-side => 'right', -fill => 'both');
my ($self, %myInputs) = @_;
Not all functions receive the first argument $self
. In fact, by convention, only the ones invoked using the arrow operator do ->
; invoking with ->
implicitly sends a special argument referring to the object. All functions and methods in perl are declared the same way (using keyword sub
). Only invocation determines whether or not the function is a method.
The my ($foo, $bar) = ( $x, $y );
is called parallel assignment. That's all that is going on here!
Observe a hash can be initialized from an array in Perl.
my @foo = qw/ foo bar baz quz /;
my %hash = @foo;
print $hash{foo}; # outputs bar
Because you're assigning to the hash %myInputs
, the hash is explicitly assigned all inputs that are not the implicitly sent (because you're pulling that one off into $self
). But be careful, it wouldn't make much sense to do the following right?
my @foo = qw/ foo bar baz /;
my %hash = @foo;
print $hash{baz} # what is this set too??
For the same reason, it also doesn't make much sense to invoke your function with an un-even amount of arguments! Both will generate a warning.
In Perl @_;
is a Global Array Special Variables
Similar as @ARGV
The array contain the command-line arguments intended for the script.
So in my ($self, %myInputs) = @_;
@_
would represent the argument of the Variable $
in the Hash-Variables %
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With