Re: Passing a hash plus some strings to a subroutine
by Fletch (Bishop) on Dec 17, 2025 at 15:02 UTC
|
I'd maybe do something like this, or just name it $data and use $data->{'foo'} rather than having to
have an actual hash locally (unless you're worried about altering the original through the ref).
sub generate {
my( $record, $status, $_data ) = @_;
my %data = %{ $_data };
...
}
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |
|
|
Thanks. By the time the data gets that far into my script, there is no intention to change it. However, I think for readability, I'll go with the approach which creates the local hash.
| [reply] |
Re: Passing a hash plus some strings to a subroutine
by hippo (Archbishop) on Dec 17, 2025 at 15:25 UTC
|
It's just moaning about your use of individually shifting the args. This passes perlcritic --stern for me:
sub generate {
my ($record, $status, %data) = @_;
return;
}
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
| [reply] |
|
|
Why does perlcritic complain about this:
my %arg = @_;
Always unpack @_ first... | [reply] [d/l] |
|
|
$ cat x.pl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
sub foo {
my %arg = @_;
print "Woo!\n";
return;
}
foo (z => 3);
$ perlcritic --stern x.pl
x.pl source OK
$
This suggests that the cause of its complaint (if any) is in the code which you have not shown, such as if you have done something in the sub before that line and hence that line is not "first".
| [reply] [d/l] |
|
|
Re: Passing a hash plus some strings to a subroutine
by LanX (Saint) on Dec 17, 2025 at 16:11 UTC
|
It depends on ...
- which version of perl you are using
- how much you care about backwards compatibility
- if this semantic matters (passing a hashref and flat-copying
it)
update
but yeah, the real problem here is perlcritic enforcing old PBP rules here.
Not sure how it handles newer mechanisms.
| [reply] |
|
|
| [reply] |
|
|
| [reply] [d/l] |
|
|
Sorry, I'm the wrong one to ask about Perl::Critic - I don't use it.
I know that many companies expand/restrict the rule sets to their liking in order to have coherent internal style.
And PBP itself described its rules only as suggestions, IIRC.
Anyway, what Fletch showed you is the standard vanilla approach which will always work.
| [reply] |
Re: Passing a hash plus some strings to a subroutine
by haukex (Archbishop) on Jan 11, 2026 at 08:51 UTC
|
Perl::Critic is great and I use it for all my modules. However, it's only advice, and can be configured to the author's tastes. The intent of the RequireArgUnpacking rule, which is what you're seeing here, is to catch subroutines that use @_ throughout their code instead of unpacking it into local variables at the beginning of your sub. However, your code is doing that here, so the rule is simply being a little too trigger happy. This is exactly the reason you'll find ## no critic (RequireArgUnpacking) scattered throughout my code, and that's my advice for you in this case.
| [reply] [d/l] [select] |