Is there anyway to do something like this in perl (convert array hash and use it in one line)?
sub func{
#from this
my %args = @_;
print $args{test};
#to this
print %{@_}{test};
}
Im using it for passing errors to callbacks like javascript use strict; use warnings;
sub error{
my $msg = shift;
print("ERROR: $msg\n");
}
sub parse_args{
my %args = @_;
return $args{args};
}
sub do_something{
my $args = parse_args(
args=>{@_},
optional_args=>["fail_cb"]
);
if(!0){
return(exists $args->{fail_cb} ? $args->{fail_cb}->(err=>"not good") : 0);
}
}
do_something(
fail_cb => sub{error(({@_})->{err});}
);