on Code references

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,
 
R

rthangam

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,

you can use eval !! that should solve the problem:

#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;

use Data::Dumper;

# 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 = eval $code;
}

$a;

};

my @A = qw( 3 54 22);

## to compute the max of array
print $reduce_ref->( '($a > $b) ? $a : $b', @A);

__END__


Please check it
 
R

rthangam

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,

You can use eval

#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;

use Data::Dumper;

# 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 = eval $code;
}

$a;

};



my @A = qw( 3 54 22 12 100);

## to compute the max of array
print $reduce_ref->( '($a > $b) ? $a : $b', @A);

__END__
 
S

sharma__r

You can use eval

#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;

use Data::Dumper;

# 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 = eval $code;
}

$a;

};

my @A = qw( 3 54 22 12 100);

## to compute the max of array
print $reduce_ref->( '($a > $b) ? $a : $b', @A);

__END__


It works! But is possible to make it work without 'eval'?
 
A

A. Sinan Unur

(e-mail address removed) wrote in (e-mail address removed):
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 ...

Funny, it gives me:

C:\Temp> ttt
Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
(Missing operator before ?)
syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
\ttt.pl aborted due to compilation errors.

If I correct the last line:

print $reduce_ref->( sub { ($a > $b) ? $a : $b }, @A);

Then the script outputs 54.

The simple resolution of the problem aside, I am not sure why you want
to do this when you can use:

http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
S

sharma__r

(e-mail address removed) wrote in (e-mail address removed):














Funny, it gives me:

C:\Temp> ttt
Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
        (Missing operator before  ?)
syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
\ttt.pl aborted due to compilation errors.

If I correct the last line:

print $reduce_ref->( sub { ($a > $b) ? $a : $b }, @A);

Then the script outputs 54.

The simple resolution of the problem aside, I am not sure why you want
to do this when you can use:

http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/


Thanks for resolving that!

The error messages are differing maybe because of the OS differences.
I ran on Sun-Solaris Unix.

I am using the code from List::MoreUtils module, but was trying to use
it
as a code reference rather than a named subroutine.

Regards,
 
C

C.DeRykus

Thanks for resolving that!

The error messages are differing maybe because of the OS differences.
I ran on Sun-Solaris Unix.

I am using the code from List::MoreUtils module, but was trying to use
it
as a code reference rather than a named subroutine.

s/List::MoreUtils/List::Util/
 
E

Eric Pozharski

(e-mail address removed) wrote in (e-mail address removed):

Actually, you have reading trouble. You're supposed to copy-paste your
code and *anyway* provide *exact* error messages. Maybe you'll just go
back to killfile where you've came from?

*SKIP*
Funny, it gives me:

C:\Temp> ttt
Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
(Missing operator before ?)
syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
\ttt.pl aborted due to compilation errors.

Forget him. Look what I've found.

{62446:61} [0:0]$ perl -MO=Deparse -wle ' { $x ? $y : $z } @A '
Useless use of a variable in void context at -e line 1.
Useless use of a variable in void context at -e line 1.
Useless use of a variable in void context at -e line 1.
Name "main::y" used only once: possible typo at -e line 1.
Name "main::z" used only once: possible typo at -e line 1.
Name "main::x" used only once: possible typo at -e line 1.
Name "main::A" used only once: possible typo at -e line 1.
BEGIN { $^W = 1; }
BEGIN { $/ = "\n"; $\ = "\n"; }
{
$x ? $y : $z;
}
@A;
-e syntax OK

If I swap block(?) and array, then it becomes hash slice. But what is
this?

*CUT*
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top