I am working on patching the Zoidberg Perl shell to fix some errors that modern Perl versions throw when testing. Note, I am not the original author nor am I criticizing him. I came upon an interesting problem that I would like a little history on.
The code below takes an array of inputs and for each strips off and ending sigil. Then it stores that sigil in a variable, if it didn't find it store 0 instead. The code that was in the original file looked like test A and must have worked until and including perl v5.8 given cpantesters results. Anyway the problem is that if the s/// isn't inside an of an "if" test, upon looping the $1 variable persists with the last value. Here are my tests. I was wondering if people could explain why this happens and or/why it used to work, I think I understand what is happening.
#!/usr/bin/perl
use strict;
use warnings;
my @a = qw/n a$ b@ c/;
my @b = @a;
my @c = @a;
print "Test A -- Doesn't work (c !-> 0)\n";
for (@a) {
s/([\$\@\%])$//;
my $arg = $1 || 0;
print $_ . " -> " . $arg . "\n";
}
print "\nTest B -- Works\n";
for (@b) {
my $arg;
if (s/([\$\@\%])$//) {;
$arg = $1;
}
$arg ||= 0;
print $_ . " -> " . $arg . "\n";
}
print "\nTest C -- Works, more clever\n";
for (@c) {
my $arg = s/([\$\@\%])$// ? $1 : 0;
print $_ . " -> " . $arg . "\n";
}