Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Perl is not a “strongly typed” language.

True for some definitions of “strongly typed“ (see Strong_typing), but in this context I think it’s clearer if we describe Perl as dynamically typed, in contrast to languages like C which are statically typed. With static typing, a variable’s type is known at compile time; with dynamic typing, a (scalar) variable’s data type is determined at runtime, and may change as the script executes — $foo may hold a string, then an integer, then a reference, ...

You cannot now say my $foo : integer;

Well, not in core Perl, no. But if you have objects, you’ll probably be using an object system (and if not, please see the picture on Your Mother’s home page!), and in an object system like Moose you can certainly specify that $foo is an Int. Here is one way to approach the OP’s task using Moose:

#! perl use strict; use warnings; use Data::Dump; package Square { use Moose; use namespace::autoclean; has queen_is_present => ( is => 'rw', isa => 'Bool', default => 0, ); has is_threatened => ( is => 'rw', isa => 'Int', default => 0, ); no Moose; __PACKAGE__->meta->make_immutable; } use enum qw( :Rank_=0 r1 r2 r3 r4 r5 r6 r7 r8 :File_=0 a b c d e f g h ); my @board; for my $rank (Rank_r1 .. Rank_r8) { $board[$rank][$_] = Square->new() for File_a .. File_h; } $board[1][2]->is_threatened(7); # OK: 7 is an +'Int' dd @board; $board[3][4]->is_threatened('foo'); # Runtime error: foo is not an +'Int'

But this is still dynamic typing: if a type mismatch occurs, it is a runtime error which results.

Hope that helps,

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


In reply to Re^2: Creating a unique variable type by Athanasius
in thread Creating a unique variable type by tfredett

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2014-12-18 01:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (41 votes), past polls