in reply to Using a stored condition

You could try any of these:
my $method1 = '(($a && $b)) || ($c && $d)'; my $method2 = sub { my ($a,$b,$c,$d) = @_; (( $a && $b )) || ($c && $d +) }; }; sub method3 { ... } print "yeah\n" if eval $method1; die $@ if $@; print "yeah\n" if $method2->($a,$b,$c,$d); print "yeah\n" if method3(...);

-Paul

Replies are listed 'Best First'.
Re^2: Using a stored condition
by legend (Sexton) on Mar 02, 2008 at 22:40 UTC
    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?
      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?
        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?
      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.