S
sharma__r
Hi Perl gurus,
I am having trouble with calling subroutines using a reference
variable.
#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;
# anonymous subroutine stored in a reference variable
my $reduce_ref = sub (&@) {
my $code = shift;
no strict 'refs';
return shift unless @_ > 1;
use vars qw($a $b);
my $caller = caller;
local(*{$caller."::a"}) = \my $a;
local(*{$caller."::b"}) = \my $b;
$a = shift;
foreach (@_) {
$b = $_;
$a = &{$code}();
}
$a;
};
my @A = qw( 3 54 22);
## to compute the max of array
print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
__END__
## this gives the following error:
# Use of uninitialized value in numeric gt (>) at ...
# Odd number of elements in anonymous hash at ...
# Use of uninitialized value in anonymous hash ({}) at ...
# Not a CODE reference at ...
Had I used a named subroutine then the things work fine.
What could be the problem here?
with thanks,
I am having trouble with calling subroutines using a reference
variable.
#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;
# anonymous subroutine stored in a reference variable
my $reduce_ref = sub (&@) {
my $code = shift;
no strict 'refs';
return shift unless @_ > 1;
use vars qw($a $b);
my $caller = caller;
local(*{$caller."::a"}) = \my $a;
local(*{$caller."::b"}) = \my $b;
$a = shift;
foreach (@_) {
$b = $_;
$a = &{$code}();
}
$a;
};
my @A = qw( 3 54 22);
## to compute the max of array
print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
__END__
## this gives the following error:
# Use of uninitialized value in numeric gt (>) at ...
# Odd number of elements in anonymous hash at ...
# Use of uninitialized value in anonymous hash ({}) at ...
# Not a CODE reference at ...
Had I used a named subroutine then the things work fine.
What could be the problem here?
with thanks,