ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl6-language
perl6-language
Customizable default hash and array values.
by Michael G Schwern other posts by this author
Sep 25 2001 2:06AM messages near this date
BabyPerl 0.01 | Re: Customizable default hash and array values.
How many times have you written this (well, since this is Perl 6
grammar, you've probably *never* written this, but you get the idea)

    my $foo = %hash{$key} || 'some default';

Annoying, and you've got to scatter this sort of thing all over the
code, even if just to avoid "use of uninitialized value" warnings.

Wouldn't it be nice if there was a way to change the default value of
a hash?  So instead of undef you'll get whatever default you like?

Well, if we go through with making hashes and arrays objects under the
hood (I'm assuming of classes HASH and ARRAY for the purposes of this
discussion) why not just allow:

    %hash.default('some default');

then your code simplifies to:

    my $foo = %hash{$key};

so if $key does not exist you'll get 'some default' instead of undef.

The same thing can be applied to arrays.


PS  Once again, this is Ruby thievery.

-- 

Michael G. Schwern   <schwern@[...].com>     http://www.pobox.com/~schwern/

Perl6 Quality Assurance     <perl-qa@[...].org> 	     Kwalitee Is Job One

Maybe they hooked you up with one of those ass-making magazines.
	-- brian d. foy as misheard by Michael G Schwern


Thread:
Michael G Schwern
RaFaL Pocztarski
Johan Vromans
Bart Lateur
Michael G Schwern
Bart Lateur
Michael G Schwern
Dan Sugalski
David Grove

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved