Shawn Corey wrote:
> John W. Krahn wrote:
>> You can't because perl implements constants using subroutines and
>> subroutines
>> can only return a list.
>
> Perl subroutines return only lists but it converts them to hashes
> automatically:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use Data::Dumper;
>
> sub list_to_hash {
> return qw( a 1 b 2 c 3 );
> }
>
> my %hash = list_to_hash();
Any list can be assigned to a hash just as a hash can be assigned to a list.
> print Dumper( \%hash );
>
> __END__
>
>
> The thing about constant is that does not create a true constant; it
> creates a reference to an anonymous subroutine.
If it were an anonymous code reference then it would have to be stored in a
scalar and have a '$' sigil in front of it. Unless it was stored in a type
glob and then "strict 'subs'" would not allow it to be used without
parentheses or the '&' sigil. It has to be a subroutine in the current
package for strict not to complain. See the "Constant Functions" section in
perlsub.
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use constant STOPWORDS => map { $_, 1 } qw(a about above across adj after);
>
> for my $key ( sort keys %{ { STOPWORDS } } ){
> my $value = ${ { STOPWORDS } }{$key};
In both lines you are copying the entire list to an anonymous hash. If you
want efficient code (and less punctuation) you should just use a hash.
> print "$key => $value\n";
> }
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>