On Tuesday 06 November 2007 00:40, Ab wrote:
> I have a package with the following contents.
> -------------------
> package abhinav::test;
>
> use strict;
> use warnings;
>
> sub test1
> {
>       return "\nHello World";
> }
>
> sub test2
> {
>       my ($include) = @_;
>       foreach my $row (@$include)
>               {
>               push (@$row, @$row[0] + 10);
                             ^^^^^^^^

That is properly written as:

                push (@$row, $row->[0] + 10);


>               }
>       return $include;
> }
>
> 1;
> -------------------
>
>
>
> Now, The thing I am trying to achieve is to call abhinav::test::test2
> on the runtime.
> ie, I am passing the value 'abhinav::test::test2' in a variable, and
> trying to exec in the code below, and this place I am failing.
> Can someone help me as to how to achieve this in runtime.
> Thanks
> -------------------
> use strict;
> use abhinav::test;
>
> my @arr = (['1','2'], ['3','4'], ['5','6'], ['7','8']);
>
> print("\n --- ORIGINAL ARRAY ---\n");
> foreach my $row (@arr) {
>       print(@$row);
>       print "\n";
> }
>
> print("\n --- THIS IS A STATIC CALL ---\n");
> my $arr = abhinav::test::test2([EMAIL PROTECTED]);
> foreach my $row (@$arr) {
>       print(@$row);
>       print "\n";
> }
>
>
> print("\n --- THIS IS A DYNAMIC CALL ---\n");
> my $dyna_sub = 'abhinav::test::test2';
> my @arr = (['1','2'], ['3','4'], ['5','6'], ['7','8']);
> my $sub_str = $dyna_sub . '(' . [EMAIL PROTECTED] . ')';

You are turning the reference to @arr into a string and it can't be 
turned back into a reference.


> my $arr = eval $sub_str;
> print ("\n -- ERRR -- $@ --\n");
>
> foreach my $row (@$arr) {
>       print(@$row);
>       print "\n";
> }

The proper way to do what you want is:

print "\n --- THIS IS A DYNAMIC CALL ---\n";
my $dyna_sub = \&abhinav::test::test2;
my @arr = ( [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] );
my $arr = $dyna_sub->( [EMAIL PROTECTED] );

foreach my $row ( @$arr ) {
    print @$row, "\n";
}



The way you want to do it should be:

print "\n --- THIS IS A DYNAMIC CALL ---\n";
my $dyna_sub = 'abhinav::test::test2';
my @arr = ( [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] );

my $sub_str = $dyna_sub . '( [EMAIL PROTECTED] )';
# Or
# my $sub_str = "$dyna_sub( [EMAIL PROTECTED] )";

# You shouldn't use eval()
# see: perldoc -q "How can I use a variable as a variable name"
my $arr = eval $sub_str;

print "\n -- ERRR -- $@ --\n" if $@;

foreach my $row ( @$arr ) {
    print @$row, "\n";
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to