Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

assertions ? how do they work..

by rootcho (Pilgrim)
on Nov 24, 2013 at 09:57 UTC ( [id://1064108]=perlquestion: print w/replies, xml ) Need Help??

rootcho has asked for the wisdom of the Perl Monks concerning the following question:

hi guys,
Did exactly what the docs says i.e. :
sub assert(&) : assertion { &{$_[0]}() } use assertions;

but what I'm getting back is :
Invalid CODE attribute: assertion ...

Also the explanation in the docs is a bit unclear.. From my undestanding I should be able to declare any sub to be assertion, if I specify :assertion attribute, right ?
Then it should be optimised away during compilation unless there is active tag (declared via "use assertions 'tag' ") in the current lexical scope which matches tags provided via command line -A=... ?
What I'm missing ?

Replies are listed 'Best First'.
Re: assertions ? how do they work..
by salva (Canon) on Nov 24, 2013 at 12:32 UTC
    assertions was an experimental feature which was available for some time on the perl 5.9 development branch but finally removed before 5.10 came out.
      I'm with 5.4.12.. I thought that they are standard part of Perl as of 5.9 !! Documentation is misleading then :)
      I'm interested in two things mainly.. and over the years have constanly fiddled with and never came up with satisfing solution to two problems :
      1.1 required arguments passed to subs
      1.2 requried params when creating objects
      1.3 clean debuging

      2. In all of those cases I want to be able to prune those checks in production.
      It seemed assertion may be the most clean way to do those... but Perl never had assertions..
      Oooo ... and third .. do less typing :)
Re: assertions ? how do they work..
by Athanasius (Archbishop) on Nov 24, 2013 at 12:21 UTC

    Hello rootcho,

    I hadn’t used the assertions module before, but playing around with it, in light of the advice in the 2007 thread how to use attributes?, I came up with this:

    #! perl use strict; use warnings; use Attribute::Handlers; sub assertion : ATTR{'CODE'}; no warnings; sub assert(&) : assertion { &{$_[0]}() } use warnings; use assertions 'foo'; assert { print "asserting 'foo'\n" }; { use assertions qw( foo bar ); assert { print "asserting 'foo' and 'bar'\n" }; } { use assertions qw( bar ); assert { print "asserting only 'bar'\n" }; } { use assertions '_ && bar'; assert { print "asserting 'foo' && 'bar'\n" }; } assert { print "asserting 'foo' again\n" };

    Output:

    22:14 >perl 783_SoPW.pl asserting 'foo' asserting 'foo' and 'bar' asserting only 'bar' asserting 'foo' && 'bar' asserting 'foo' again 22:17 >

    Whether the optimisation also works as desired, I haven’t tested, but this should at least give you a basis for further experimentation.

    Hope it helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: assertions ? how do they work..
by tobyink (Canon) on Nov 24, 2013 at 14:18 UTC

    Personally I'd avoid assertions - it's got kind of a weird interface.

    Devel::Assert or Carp::Assert will probably bring you more joy.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Devel::Assert seem nice... Carp::Assert requires "if DEBUG"

        Carp::Assert has a hell of a lot less magic on the inside though.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1064108]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-29 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found