On Mon, 11 Mar 2002 17:56:41 +0800, allan wrote:

>i need to (i think) pass a hash as a reference to a sub
>routine for ultimately letting that sub return a string.
>that sub is placed in another file which i require beforehand.
>i know the hash is ok
>i know i can pass an require plain scalars
>but is it possible to pass a hash or any reference in the
>middle of an eval?

Yes you can. I think you don't need to, but you can. You can reference
any variable in scope, whatever it contains. As:

        my $string = "You see?";
        my $ref = \$string;
        eval 'print $$ref';

>the closest i have come to succes is something not fully
>working as below:
>
>my %lookup_menu = (
>    1 => HOME,
>    2 => PARTNERS,
>);
>
>
>require 'relevant_file';
>$out = eval($sub_routine . "(\\%lookup_menu)");
>print $out;

Well, you don't need the eval. You could use a symbolic reference to the
sub:

        $sub_routine->(\%lookup_menu);

but it would be better still if the variable contains a reference to the
sub to call, instead of the name of the sub.

        $sub_rountine = \&foo ;  # instead of "foo".

The latter will get past "strict", too.

-- 
        Bart.

Reply via email to