Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not pass special variables to subroutines directly #7

Closed
KES777 opened this issue May 21, 2022 · 3 comments
Closed

Do not pass special variables to subroutines directly #7

KES777 opened this issue May 21, 2022 · 3 comments

Comments

@KES777
Copy link

KES777 commented May 21, 2022

Why:

#!/usr/bin/env perl

sub test {
	'asdf' =~ m/.*/; # This destroys received arguments
	print ">>@_<<\n";

}

my $z =  'abcd';
$z =~ m/(ab)(cd)/;
test( $1, $2 );                # >> <<
test( my $x=$1, my $y=$2 );    # >>ab cd<<

How regular expression could be executed when sub is called? This could occur under debugger. For example some module define sub sub and used regex inside that sub

@tobyink
Copy link
Owner

tobyink commented Jun 7, 2022

I'm not sure why you submitted this as an issue for Exporter::Tiny as it seems unrelated.

But it is because @_ acts as an alias for the arguments passed to the function, not a copy. By doing m/.*/, you destroy $1 and $2, and @_ was only ever an alias of them, not a copy of them, so the values in it get destroyed too.

@tobyink tobyink closed this as completed Jun 7, 2022
@KES777
Copy link
Author

KES777 commented Jun 8, 2022

@tobyink
$1 is destroyed.

https://github.com/tobyink/p5-exporter-tiny/blob/master/lib/Exporter/Tiny.pm#L26 (please also fix other places)

Would be better if you pass "$1" instead $1

@KES777
Copy link
Author

KES777 commented Aug 4, 2022

I submitted because Exporter::Tiny uses $1 as parameter.

clrpackages pushed a commit to clearlinux-pkgs/perl-Exporter-Tiny that referenced this issue Aug 30, 2022
…2 to version 1.004000

1.004000	2022-08-26

 [ Documentation ]
 - Document that the `-lexical` export option is experimental.

 [ Packaging ]
 - No functional changes; repackage as stable.

1.003_002	2022-08-18

 [ Bug Fixes ]
 - Quote $1 when passing it as a parameter to a function
   Eugen Konkov++
   <tobyink/p5-exporter-tiny#7>

(NEWS truncated at 15 lines)
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Oct 1, 2022
Upstream changes:
1.004003        2022-09-30

 [ Bug Fixes ]
 - If exporting non-CODE items which happen to have the same name as
   exported CODE items, their export was being quietly blocked. These
   exports should now work.
   Diab Jerius++
   <tobyink/p5-exporter-tiny#9>
 - Using ! with a tag now works; it was previously documented as working
   but not implemented.
   Diab Jerius++
   <tobyink/p5-exporter-tiny#8>

1.004002        2022-09-16

 [ Test Suite ]
 - Fix for t/15nonhashvalue.t on old versions of Test::More which don't
   support `done_testing`.

1.004001        2022-09-09

 [ Bug Fixes ]
 - Fix handling of non-hashref references in import list.

1.004000        2022-08-26

 [ Documentation ]
 - Document that the `-lexical` export option is experimental.

 [ Packaging ]
 - No functional changes; repackage as stable.

1.003_002       2022-08-18

 [ Bug Fixes ]
 - Quote $1 when passing it as a parameter to a function
   Eugen Konkov++
   <tobyink/p5-exporter-tiny#7>

1.003_001       2022-07-21

 [ Bug Fixes ]
 - Correctly check Perl version is at least 5.37.2+ before supporting
   -lexical option.

1.003_000       2022-07-21

 - Updated: Provide a `-lexical` export option on Perl 5.37.2+.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants