a, b, - and c?

G

~greg

Why does this work...
use strict;
$|=1;
sub Test
{
(*a,*b) = @_;
print "$a\n$b\n";
}
my $A = 'this is';
my $B = 'a test';
Test(\$A,\$B);
(prints:
this is
a test)

....but this doesn't?
use strict;
$|=1;
sub Test
{
(*a,*c) = @_;
print "$a\n$c\n";
}
my $A = 'this is';
my $C = 'a test';
Test(\$A,\$C);
(Variable "$c" is not imported at ... line...
Global symbol "$c" requires explicit package name at ... line ...)

?

(Something to do with 'sort()' maybe?)
 
S

Sisyphus

~greg said:
Why does this work...
use strict;
$|=1;
sub Test
{
(*a,*b) = @_;
print "$a\n$b\n";
}
my $A = 'this is';
my $B = 'a test';
Test(\$A,\$B);
(prints:
this is
a test)

...but this doesn't?
use strict;
$|=1;
sub Test
{
(*a,*c) = @_;
print "$a\n$c\n";
}
my $A = 'this is';
my $C = 'a test';
Test(\$A,\$C);
(Variable "$c" is not imported at ... line...
Global symbol "$c" requires explicit package name at ... line ...)

?

(Something to do with 'sort()' maybe?)

Yep - in that $a and $b are special (as you probably know).
So, as soon as you name the variable(s) something other than 'a' or 'b', you
need to take extra steps.

This does the right thing:

use strict;
use warnings;
$|=1;
sub Test
{
our ($x, $y);
(*x,*y) = @_;
print "$x\n$y\n";
}
my $A = 'this is';
my $C = 'a test';
Test(\$A,\$C);

Cheers,
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top