my $bf_1; { # Start a new block to avoid cluttering the environment my $sum_x = 0; $bf_1 = sub { # This block references $sum_x, # so it will be captured in the closure my ($operation, @args) = @_; # You didn't mean @ARGV; it's for command-line args if($operation eq 'Add') { $sum_x += $args[0]; } }; }