How can I have statement variables in Perl?

How to check that a variable has a specific value in Perl? Is there a command to stop script execution to search for some of these variables?

I wonder if I can use the pythonic insertion practice:

assert 0, (foo, bar) 

for debugging scripts in debugger mode?

+6
python debugging perl assertions
source share
6 answers

Quick Search CPAN Offers Carp :: Assert .

+10
source share

See Carp :: Assert :

 use Carp::Assert; $next_sunrise_time = sunrise(); # Assert that the sun must rise in the next 24 hours. assert(($next_sunrise_time - time) < 24*60*60) if DEBUG; # Assert that your customer primary credit card is active affirm { my @cards = @{$customer->credit_cards}; $cards[0]->is_active; }; 
+6
source share

Smart :: Comments are enjoyable.

+5
source share

There is a script in PerlMonks that introduces a quick assert method.

Speed ​​is important because Perl is interpreted, and any built-in checks affect performance (unlike simple C macros, for example)


I am not sure that these things will be directly used.


Ok! This is what I was looking for - Warning in PDF: Test-Tutorial.pdf . Test::Harness used to write Perl module tests.

+3
source share
 $var_to_check =~ /sometest/ or die "bad variable!"; 

I prefer to use such things in my code, and then use find and replace to get rid of them (in production code).

In addition, eval 'can be used to run code sections and capture errors and can be used to create exception handling functionality. If you claim that the value is not 0, perhaps you want to throw an exception and handle this case in a special way?

+1
source share
 if ( $next_sunrise_time > 24*60*60 ) { warn( "assertion failed" ); } # Assert that the sun must rise in the next 24 hours. 

You can do this if you do not have access to Perl 5.9, which is required for Carp :: Assert .

0
source share

All Articles