5

I need to pass back a enum value in perl, how can I do this?

pulling from this thread: Does Perl have an enumeration type?

use strict;

use constant {
    HOME   => 'home',
    WORK   => 'work',
    MOBILE => 'mobile',
};

my $phone_number->{type} = HOME;
print "Enum: ".$phone_number->{type}."\n";

but shouldn't this return index 0? or am I understanding this wrong?

EDIT:

So would something like this be more expectable for a enum type?

use strict;

use constant {
    HOME   => 0,
    WORK   => 1,
    MOBILE => 2,
};

my $phone_number->{type} = HOME;
print "Enum: ".$phone_number->{type}."\n";

EDIT #2

Also I would like to validate on the option selected but pass back the Word rather then the Value. How can I have the best of both examples?

@VALUES = (undef, "home", "work", "mobile");

sub setValue {

if (@_ == 1) {
   # we're being set
   my $var = shift;
   # validate the argument
   my $success = _validate_constant($var, \@VALUES);

   if ($success == 1) {
       print "Yeah\n";
   } else {
       die "You must set a value to one of the following: " . join(", ", @VALUES) . "\n";
   }
}
}

sub _validate_constant {
# first argument is constant
my $var = shift();
# second argument is reference to array
my @opts = @{ shift() };

my $success = 0;
foreach my $opt (@opts) {
    # return true
    return 1 if (defined($var) && defined($opt) && $var eq $opt);
}

# return false
return 0;
}
Community
  • 1
  • 1
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • What do you mean by *index 0*? – Sinan Ünür Jan 25 '10 at 19:34
  • 3
    Did you read the accepted answer to the question you linked? Especially the part the said that Perl does *not* have an enum type? If so, where are you expecting "index 0" to come from? – friedo Jan 25 '10 at 19:35
  • @friedo I agree with you, I just want to make sure I'm understanding this right. I need to have the option to set HOME but return 0. Would just changing the value to 0 work? See Edit – Phill Pafford Jan 25 '10 at 19:44
  • 1
    Yes, that works, in fact, I submitted a patch for clarity to POE to use that form instead of prototyped subs. – Evan Carroll Jan 25 '10 at 19:46
  • one last question, in the example after MOBILE => 2, there is a comma. is this needed? – Phill Pafford Jan 25 '10 at 19:49
  • 1
    no, but it doesn't hurt, it is just a hash. – Evan Carroll Jan 25 '10 at 19:50
  • 1
    This is beginning to smell like homework to me. – Sinan Ünür Jan 25 '10 at 20:05
  • nope, not homework. REAL WORK – Phill Pafford Jan 25 '10 at 20:08
  • Well for your morals I am a PHP developer that has been assigned a perl project. With limit time and knowledge of perl I have come to StackOverflow for some help from fellow developers. As you can see by my question I indeed do not understand ENUM types in perl and have asked for your help. I understand the concept but the implementation is aloof to me. If this is not the forum where developers can come and ask questions without being a GURU in the subject, where else would you suggest? – Phill Pafford Jan 25 '10 at 20:19
  • if I need to ask two questions for the same problem, would that work for you. Just trying to understand how I can implement both versions and get this into one acceptable solution. Thanks for your help and answer you have posted below. Cheers – Phill Pafford Jan 25 '10 at 20:21
  • 1
    @Phill Pafford: It would help if you explained what *Also I would like to validate on the option selected but pass back the Word rather then the Value. How can I have the best of both examples?* means. What are `set_value` and `_validate_constant`? Oh, and by the way, just a reminder: You have already selected an answer as correct to a question that keeps morphing. You need to be able to explain your question and understand it enough to know when the problem has been solved. – Sinan Ünür Jan 25 '10 at 20:28
  • `my $phone_number->{type}` is wrong. – Anonymous Jan 26 '10 at 13:26
  • @sinan learning during work is prime to any programming job lasting more than a week. I often take a task that requires massive retooling and retraining because of the demands of my position at work (see any of my UniVerse questions). You don't know he claimed knowledge over this specific piece of perl minutia, or even any level of perl at all. You can't possibly parley that off to a personal attack and come off as sane. – Evan Carroll Jan 26 '10 at 18:54
  • Perl is just a language. The actual knowledge and skill of programming is not the language, nor is the ability to organize a proper question. I think Phil has a bit of the XY problem (http://www.perlmonks.org/index.pl?node_id=542341), but then, we all do sometimes. – brian d foy Jan 26 '10 at 19:03
  • @phill: I'm confused by your edit2, this is really getting carried away for the question. I think you should open another SO question (don't be afraid of having too many). If you can, clarify what you're trying to do. – Evan Carroll Jan 26 '10 at 19:04
  • @Phil: although you can ask questions here, it's really your responsibility to research and develop your question on your own to solve your own problem. You don't have to be a Perl guru, but you should understand what you are trying to do and be able to explain it clearly. You linked to and used source code from an answer that clearly said that Perl does not have an enum type, but here you are asking for an enum type despite the fact that people already answered the question and that you've read the answers. – brian d foy Jan 26 '10 at 19:10
  • 2
    @Evan Carroll I deleted my comment: True, I do not know if the OP pretended to know Perl before taking on this Perl job. On the other hand, editing the question in substantial ways even after he has accepted an answer indicates to me that the OP has not yet understood the question he wants to ask. – Sinan Ünür Jan 26 '10 at 19:29

2 Answers2

2

A constant is not an enum (in perl, or any language I know of)

No, because here what you're doing is inserting in the symbol table a link between the key HOME and the literal Home, this is also called a bareword in perl parlance. The symbol table is implemented with a hash, and there is no number equivalence of its keys and the order they were added.

In your example what you're doing is setting $perl_number->{type} = 'Home', and then printing out $phone_number->{type}.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • Invoking the symbol table doesn't really matter. It's much less confusing to people to say that you create a subroutine that takes no arguments and returns a constant value. Leave the innards out of it. – brian d foy Jan 26 '10 at 19:12
  • @brian This is my answer, you're free to leave your own using the "Add another answer" option on the bottom of your screen. – Evan Carroll Jan 26 '10 at 19:28
  • 1
    OF course, I did add my answer. However, I also help people refine and correct their own answers. Symbol tables are an unnecessary confusion here, and you appear a bit confused about what is actually going on. You can provide better answers if you brought more focus to them by leaving out unnecessary details. – brian d foy Jan 26 '10 at 19:54
  • I want to explain why I feel it is appropriate, I think in order to understand why there isn't an `enum type`, you have to understand what a *constant* really *is*. I think I did a decent job at explaining it. `BEGIN {*{FOO} = sub {5}}; print FOO` is your constant. I didn't have to introduce other concepts like you want too either, like prototyping. Now he understands that all he is doing is **putting a key/value pair in a special hash**. My response to you, brian, who **never likes *my* answer** will always be to go get your own. – Evan Carroll Jan 26 '10 at 19:55
  • There's no need for a typeglob there. That's my point. I don't think you understand that a constant is just a value that doesn't change. You don't even need to use a subroutine for it (i.e. the Readonly module). I think you're conflating two different ideas in your answer, leaving people more confused than when they started. As noted, I already have my own answer. If you don't like discussion or think that a technical discussion is personally offensive, Stackoverflow is not the place for you. – brian d foy Jan 26 '10 at 20:43
  • I understand this, in fact, I explicitly said it **"The point is, there is no enum types they're implemented with symbol table hacks, the typical hack is just key->value, he is just implementing it as a sub on the namespace, rather than a bareword (sub prototyped to take no arguments). – Evan Carroll yesterday"** I don't think the discussion is offensive, I think *you* are out to make a statement rather than help anyone. – Evan Carroll Jan 26 '10 at 21:17
  • oh, if that sounds like a personal attack, it is. You've responded to probably 10 of my answers now with nothing, while your answers consist of one line that you know the questioner has already read, with no explanation or reasoning whatsoever. Good you have 28.7k exp; if you're here for the game: you win. I'm here to help. – Evan Carroll Jan 26 '10 at 21:19
2

If you want enums, use the enum module.

brian d foy
  • 129,424
  • 31
  • 207
  • 592