Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: What if Perl had an OO standard library?

by haj (Priest)
on Aug 25, 2022 at 12:29 UTC ( #11146406=note: print w/replies, xml ) Need Help??


in reply to Re^2: What if Perl had an OO standard library?
in thread What if Perl had an OO standard library?

the lack of a native type system means that your classes and methods can’t be certain of the type of data they’re being passed, which forces you into a position of defensive programming

Well, in my view Perl does never force me into any position, but, depending on the task, defensive programming is recommended.

Using objects as a surrogate for a type system is just a thin layer. For safety, this only helps if the classes do the appropriate validations on object creation. For example, Venus::Number accepts any value, so my classes and methods still can't be certain of the type of data they're being passed:

use 5.028; use warnings; use Venus::Number; use File::Temp; my $n; # These two come with warnings say Venus::Number->new($n); # '' say Venus::Number->new($n,$n); # 0 $n = 1; say Venus::Number->new($n); # 1 $n = "abc"; say Venus::Number->new($n); # abc $n = [3,2,1]; say Venus::Number->new($n); # ARRAY(0x...) say Venus::Number->new($n)->abs; # 94653281131368 $n = File::Temp->new; say Venus::Number->new($n); # /tmp/ER2Mi14BOz

Replies are listed 'Best First'.
Re^4: What if Perl had an OO standard library?
by awncorp (Acolyte) on Aug 31, 2022 at 23:35 UTC

    Thanks for the feedback. I don't mean to be argumentative but if you write a Perl subroutine expecting a certain type of data (say a hashref), how do you handle the case where the routine is called with something else (say an integer).

    As far as I can tell you only have two options: i.e. yolo! and let it crash, or try to do some simple sanity checking. The simple sanity checking is defensive programming (and you're forced to do it (or at the very least, coerced)).

    The most recent release of Venus, v1.30, ships with a simple type assertion framework that all standard library classes utilize via the "make" method. The new method still allows value classes to accept bad values, intentionally. The idea is that "make" is useful in situations where you're unsure about the data being submitted. Obvisouly "make" (e.g. new w/type checking) is not as performant as "new".

    Venus::Number->make; # Exception! Venus::Number->make(0,0); # bless(..., "Venus::Number") Venus::Number->make(1); # bless(..., "Venus::Number") Venus::Number->make('abc'); # Exception! Venus::Number->new([3,2,1]); # Exception! Venus::Number->new([3,2,1])->abs; # Exception!
    "I am inevitable." - Thanos

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2023-03-26 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (63 votes). Check out past polls.

    Notices?