develooper Front page | perl.perl6.language | Postings from April 2002

Subroutine variables are like underwear

Thread Next
From:
Miko O'Sullivan
Date:
April 12, 2002 19:25
Subject:
Subroutine variables are like underwear
Message ID:
001201c1e292$0608ff00$010a0a0a@mosullivan
SUMMARY

A way to declare public names for params irrelevant to the internal variable
names:

  sub load_data (-filename $filename_tainted ; 'version' 'ver'
$version_input / /= 1) {...}


DETAILS

Subroutine variables are like underwear: you don't generally go showing them
to everybody.  So, when I noticed how named params seem to be designed for
Perl 6, I felt, well, bashful about it.  Consider this sub declaration:

   sub load_data ($filename ; $version / /= 1) {...}

You could call this sub like this:

   my %data = load_data(filename=>'weblog', version=>1);

Now, suppose I decide I don't like the variable name "$filename".  I might
want to change it to "$filename_tainted".  I wouldn't want to have to change
every call to that subroutine, especially if I've put the module on CPAN.
Yes, I could do this:

   sub load_data ($filename ; $version / /= 1) {
       my $filename_tainted = $filename;
    undef $filename;
   }

.... but gosh that's a lot of work just to maintain flexibility in a few
variable names.  Even worse, sometimes I can't remember if it's $filename or
$file_name or $filepath or $file_path or... you get the idea.  So, suppose
there were a way to declare that any of one or more named params "counts" as
the name for the param.  There are several ways to do this, here's my idea:
each named param can be preceded by one or more names that begin with - or
are quoted.  Bare strings without - or quotes aren't allowed because those
would be data types.  Here's an example:

  sub load_data (-filename $filename_tainted ; 'version' 'ver'
$version_input / /= 1) {...}

Setting names wouldn't be required.  If they aren't set, then Perl uses the
name of the variable. One question would be if the variable name is still
used as the name for a param.  My inclination is no: declaring them means
you want the declared name, not the private name.  If you want all the
names, you declare them all.

-Miko


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About