Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: BWTDI: Better Way To Do It?

by Kanji (Parson)
on Jan 14, 2001 at 22:12 UTC ( [id://51780]=note: print w/replies, xml ) Need Help??


in reply to BWTDI: Better Way To Do It?

I don't see the need for declaring your variables on one line and assigning to them on the next, or the if/else as you could do ...

my( $qty, $type, $face, @dice ) = ( $1, $2, $3 ? 1 : undef ); push @dice, Die->get( $type, $face ) while $qty--;

... to reduce 9 lines of code to just 2.

Granted, that destroys the value of $qty in the process, but it's easy to preserve by substituting for 1 .. $qty (as Mirod suggests) for the while $qty--.

You may also want to tighten up your regular expression (the .*-thing has been beaten to death), especially if the only optional argument following the second number is an 's'.

As an alternative to the use of a regexp to pass arguments though, I'd personally be more inclined to pass them seperately rather (ie, grab( -dice => 3, -sides => 6, -face => 0 ); or even grab( qw( 3 d 6 s ) );), possibly coupling them with defaults if they aren't set.

    --k.


Replies are listed 'Best First'.
Re: Re: BWTDI: Better Way To Do It?
by coreolyn (Parson) on Jan 14, 2001 at 22:45 UTC

    ++! I'm definately going to play with all your suggestions, with the exception of the passing of the arguments. In this case the only purpose the class serves is to simplify coding with dice As int(rand($num)) is so much more CPU efficient. In otherwords the main audience for the class is a casual Perl user that wants to whip out a quick application that requires manipulating many sets of Dice. The 3d6 method of labeling Dice sets is the most common nomenclature, so I'm trying to make the object as 'natural' to use as possible.

    This brings about a deeper question where should efficiency end and ease of use begin? Ahh so much to ponder :)

    Thanks again, I should have Dice::Dice updated later.

    coreolyn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-19 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found