in reply to Re: Using a stored condition
in thread Using a stored condition

Thanks. It says:
Global symbol "$c" requires explicit package name at script.pl line 41 +. Global symbol "$d" requires explicit package name at script.pl line 41 +. Execution of script.pl aborted due to compilation errors.
Any idea on what could be wrong?

Replies are listed 'Best First'.
Re^3: Using a stored condition
by shmem (Chancellor) on Mar 02, 2008 at 22:55 UTC
    I guess it's a line like
    print "yeah\n" if $method2->($a,$b,$c,$d);

    in some scope where $c and $c aren't previously declared. Declare them with my.

    In case you wonder why $a and $b are missing from perl's complaint: Those variables are special. They are used in sort functions, so they exist anyways. It is discouraged to use them for any other purpose. See perlvar.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks. I was just trying the following simple way:
      my $aa=1,$bb=1,$cc=1,$dd=1; my $method1 = '(($aa && $bb)) || ($cc && $dd)'; print "yeah\n" if eval $method1; die $@ if $@;
      But it still keeps giving me the same bunch of errors. Am I doing something wrong here?
        Am I doing something wrong here?
        Yes. ETOOLAZY:
        my $aa=1,$bb=1,$cc=1,$dd=1;

        That's equivalent to

        my $aa=1; $bb=1; $cc=1; $dd=1;

        Only $aa is declared as lexical. Even parens don't work, since you can't declare an assignment - you declare variables.

        But that's lazy enough:

        my ($aa, $bb, $cc, $dd); $aa = $bb = $cc = $dd = 1;

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks... Its working now... I would like to go with the first method but could you show me how to execute multiple statements using it?

        If you have a small number of different conditions, you may wish to use a dispatch table.

        use strict; use warnings; my %condition = ( '(A&B)|(C&D)' => sub { my ($aa, $bb, $cc, $dd) = @_; return ($aa && $bb ) || ($cc && $dd ); }, 'A|(B&C&D)' => sub { my ($aa, $bb, $cc, $dd) = @_; return $aa || ($bb && $cc && $dd ); } ); my @flags = qw( 0 1 1 1 ); my @conditions_to_check = ( 'A|(B&C&D)', 'FOO', '(A&B)|(C&D)' ); for my $cond_name ( @conditions_to_check ) { if( exists $condition{$cond_name} ) { if ( $condition{$cond_name} ->(@flags) ) { print "Condition '$cond_name' is true\n" } else { print "Condition '$cond_name' is false\n" } } else { print "Condition '$cond_name' is not defined\n"; } } __DATA__ Condition 'A|(B&C&D)' is true Condition 'FOO' is not defined Condition '(A&B)|(C&D)' is true


        TGI says moo

        Stuff the expressions into an array and run a for() loop with that? But I'm beginning to suspect an XY problem here. What's X?

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Maybe you could write them out to a file and run perl on it.

        -Paul

Re^3: Using a stored condition
by grizzley (Chaplain) on Mar 03, 2008 at 08:53 UTC
    Because non of variables $a, $b, $c, $d was declared. The question is why $a and $b do not cause problems? The answer: because they are special variables used in sort function. And it is safer not to use them in sample codes because you can get results far from expected.