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

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}

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

        I personally believe ENOTLAZYENOUGH:

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

        (Which of course you knew, but may be worth pointing out for the benefit of the OP.)

        --
        If you can't understand the incipit, then please check the IPB Campaign.
Re^4: Using a stored condition
by legend (Sexton) on Mar 02, 2008 at 23:22 UTC
    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