Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have variable assertions in Perl?

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?

like image 588
Alex Avatar asked Jun 21 '09 12:06

Alex


People also ask

What does $_ mean in Perl?

There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used.

What is assert in Perl?

The \G assertion in Perl allows you to continue searching from the point where the last match occurred.

What is my in Perl?

my keyword in Perl declares the listed variable to be local to the enclosing block in which it is defined. The purpose of my is to define static scoping. This can be used to use the same variable name multiple times but with different values.


2 Answers

A quick CPAN search suggests Carp::Assert.

like image 62
Telemachus Avatar answered Oct 01 '22 03:10

Telemachus


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's primary credit card is active
affirm {
    my @cards = @{$customer->credit_cards};
    $cards[0]->is_active;
};
like image 41
Sinan Ünür Avatar answered Oct 01 '22 03:10

Sinan Ünür