Thursday, January 02, 2020

Re: perl popularity inside openbsd community? (Re: Suggestion: Replace Perl ...)

What if you want named parameters? (i.e. sending a hash as your
argument)

sub m4
{
my $self = shift;
my %args = @_;

# and then optionally
my ($arg1, $arg2, $arg3) = @args{qw/arg1 arg2 arg3/};

# or you can just use $args{arg1}, etc...
}


On Thu, Jan 02, 2020 at 09:12:42PM +0100, Marc Espie wrote:
> sub f
> {
> my ($arg1, $arg2) = @_;
>
> ... code
>
> }
>
> - three styles of parameter grab for methods:
>
>
> sub m1
> {
> my $self = shift;
> }
>
> No other parameter.
>
> sub m2
> {
> my ($self, $p1, $p2) = @_;
> }
>
> when getting all parameters (no check on the number usually)
>
>
> sub m3
> {
> my $self = shift;
> ...
> do_something_with(@_);
> }
>
> for functions with unlimited parameters after the first one

No comments:

Post a Comment