views:

143

answers:

5

How can I check that a variable has a specific value in Perl? Is there a command to stop a script's execution to look up some of it's variables?

I wonder if I can use the Pythonic practice of inserting:

    assert 0, (foo, bar)

to debug scripts in a debuger-less way?

+2  A: 

There is a script at PerlMonks that introduces a fast assert method.

Speed is important since Perl is interpreted and any inline checks will impact performance (unlike simple C macros for example)


I am not sure if these things are going to be directly usable.


Ok! This is what i was looking for -- PDF Warning: Test-Tutorial.pdf. The Test::Harness is used for writing Perl module tests.

nik
+6  A: 

A quick CPAN search suggests Carp::Assert.

Telemachus
+1 for typing in more characters faster than I did. I am going to edit the URL to be version agnostic though.
Sinan Ünür
I was going to comment on your answer about the photo finish. As for the URL, I constantly forget that, so thanks.
Telemachus
+3  A: 

See Carp::Assert.

Sinan Ünür
+3  A: 

Smart::Comments are nice.

zoul
Smart::Comments++ When used with the -ENV switch, it's a fantastic tool for this sort of thing. Much better than having to strip all the tests out before going to production, as someone else suggested.
RET
+1  A: 
$var_to_check =~ /sometest/ or die "bad variable!";

I tend to throw things like this in my code, and later use a find and replace to get rid of them (in production code).

Also, 'eval' can be used to run a section of code and capture errors and can be used to create exception handling functionality. If you are asserting that a value is not 0, perhaps you want to throw an exception and handle that case in a special way?

Ape-inago