Mailing List Archive

Subroutine arguments as aliases (was: Benchmarking a 'no-snails' world (was: Re: PSC #049 2022-01-07))
On the subject of passing aliases to subroutines, I will note there is a longstanding wart with passing regexp capture variables.

our $CONFIG = 'logging level 5';

sub foo {
if ($CONFIG =~ /logging level (\d+)/) { say "set logging to $1" }
say "string is $_[0]";
}

$_ = 'hello';
/(.+)/ or die;
foo($1);

The contents of @_ get unexpectedly changed by what looks like a read-only regexp operation on some other variable. This gotcha does lead to bugs in real code. I think it would be better for perl to take a copy of $1, etc, when passing them to subroutines, rather than passing an alias. After all, that's the behaviour we expect for other magic variables like $_.



This email and any files transmitted with it are CONFIDENTIAL and are intended solely for the use of the individual(s) or entity to whom they are addressed. Any unauthorised copying, disclosure or distribution of the material within this email is strictly forbidden. Any views or opinions presented within this email are solely those of the author and do not necessarily represent those of PGIM Limited, QMA Wadhwani LLP or their affiliates unless otherwise specifically stated. An electronic message is not binding on its sender. Any message referring to a binding agreement must be confirmed in writing and duly signed. If you have received this email in error, please notify the sender immediately and delete the original. Telephone, electronic and other communications and conversations with PGIM Limited, QMA Wadhwani LLP and/or their associated persons may be recorded and retained. PGIM Limited and QMA Wadhwani LLP are authorised and regulated by the Financial Conduct Authority. PGIM Limited (registered in England No. 3809039) has its registered office at Grand Buildings, 1-3 Strand, Trafalgar Square, London WC2N 5HR and QMA Wadhwani LLP (registered in England No. OC303168) has its registered office at 9th Floor, Orion House, 5 Upper St. Martin's Lane, London, England, WC2H 9EA.

Please note that your personal information may be stored and processed in any country where we have facilities or in which we engage service providers. If you provide personal information to us by email or otherwise, you consent to the transfer of that information to countries outside of your country of residence and these countries may have different data protection rules than your country.

To learn about our privacy policies, please use this link<https://www.pgim.com/disclaimer/privacy-center> to read the Privacy Notices.
Re: Subroutine arguments as aliases (was: Benchmarking a 'no-snails' world (was: Re: PSC #049 2022-01-07)) [ In reply to ]
On Fri, Jan 21, 2022 at 6:58 PM Ed Avis <ed.avis@pgim.com> wrote:

> On the subject of passing aliases to subroutines, I will note there is a
> longstanding wart with passing regexp capture variables.
>
> our $CONFIG = 'logging level 5';
>
> sub foo {
> if ($CONFIG =~ /logging level (\d+)/) { say "set logging to $1" }
> say "string is $_[0]";
> }
>
> $_ = 'hello';
> /(.+)/ or die;
> foo($1);
>
> The contents of @_ get unexpectedly changed by what looks like a read-only
> regexp operation on some other variable. This gotcha does lead to bugs in
> real code. I think it would be better for perl to take a copy of $1, etc,
> when passing them to subroutines, rather than passing an alias. After all,
> that's the behaviour we expect for other magic variables like $_.
>

It actually isn't. Passing $_ around can lead to similar issues due to its
superglobal nature. The only protection is the implicit localization done
by foreach/map/grep when using it, most commonly bugs arise from using
while-readline which clobbers $_ and does not localize it, as with regular
assignment.

-Dan