Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Being more Assert-ive with Perl

by stvn (Monsignor)
on Sep 17, 2004 at 14:03 UTC ( [id://391780]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    open(FILE "<file.dat") || die "cannot open file";
    
  2. or download this
    sub insertQuarter {
        my ($coin) = @_;
    ...
        initializeGame();
        startGame();
    }
    
  3. or download this
    # just return a false value and allow the
    # calling code to deal with the false return
    ...
    # call stack information, set error variable,
    # and return a specific value.
    (defined($coin) && $coin == 25) || return errorHandler("You must inser
    +t 25 cents");
    
  4. or download this
    #!/usr/bin/perl
    
    ...
        'Assert'          => sub { eval { Assert($_)          } for (@nums
    +) },    
        'Assert2'         => sub { eval { Assert2($_)         } for (@nums
    +) },   
    });
    
  5. or download this
    Benchmark: timing 10000 iterations of Assert, Assert2, IfBlocks, IfSta
    +tement, UnlessBlocks, UnlessStatement...
             Assert:  4 wallclock secs ( 2.38 usr +  0.02 sys =  2.40 CPU)
    + @ 4166.67/s (n=10000)
    ...
    IfStatement     1299/s             52%          49%       3%          
    +--   -69%    -69%
    Assert          4167/s            388%         378%     231%        22
    +1%     --     -0%
    Assert2         4184/s            390%         380%     233%        22
    +2%     0%      --
    
  6. or download this
    sub onlyAcceptFooObjects {
        my ($foo) = @_;
    ...
            || die "This method only accepts Foo objects";
        # ...
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://391780]
Approved by Arunbear
Front-paged by dvergin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-25 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found