Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Good practice: Variable declaration

by Basilides (Friar)
on Jun 27, 2002 at 16:31 UTC ( [id://177754]=perlquestion: print w/replies, xml ) Need Help??

Basilides has asked for the wisdom of the Perl Monks concerning the following question:

Further to emilford's node today about tidy code , and my own query the other day about OO inheritance, here's a new question:

Say I've got a parent class, Word, with subclasses, Noun, Verb & Adjective, and I want to create a reference to one of the subclasses, whose type depends on a file input. To comply with use strict on lexical scope, I reckon I need to start off with a variable declaration, which *doesn't* include an assignment: my $word;. You see this in other languages but I've never seen it in Perl, hence I'm raising it now:

my $word; #this is it--are you supposed to do this? if ($part eq 'n') { $word = new Noun(@blah); } elsif ($part eq 'v') { $word = new Verb(@blah); } else { $word = new Adjective(@blah); } print $word->get_english(); #etc etc
If this isn't the done thing, please could you tell me how you *should* do it.

Replies are listed 'Best First'.
Re: Good practice: Variable declaration
by japhy (Canon) on Jun 27, 2002 at 16:43 UTC
    That's fine. You could be spiffy and write it like:
    my %class = qw( n Noun v Verb ); my $word = ($class{$part} || "Adjective")->new(...);
    if you feel like being spiffy.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      That's fine. You could be spiffy and write it like:
      my %class = qw( n Noun v Verb );
      my $word = ($class{$part} || "Adjective")->new(...);

      Spiffier.

      my $word = ({ qw/n Noun v Verb/ }->{$part} || 'Adjective')->new(...);

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        I changed my code from that because that doesn't lend itself to repetition. Easier to change one data structure than many lines of code. Although I un-abstracted from what I had originally:
        my %class = qw( n Noun v Verb ); my $default = 'Adjective'; ...

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Good practice: Variable declaration
by mfriedman (Monk) on Jun 27, 2002 at 16:40 UTC
    Yes, that's exactly how you're supposed to do it. :)

    If you want to be really anal, you could always write

    my $word = undef;

    Which accomplishes the same thing.

Re: Deciding a class at runtime.
by frankus (Priest) on Jun 27, 2002 at 16:40 UTC
    This seems correct. Perhaps better to use:
    $word = Verb->new(@blah);
    But I guess that's a matter of preference unless you're doing multi-inheritence.

    --

    Brother Frankus.

    ¤

Re: Good practice: Variable declaration
by Aristotle (Chancellor) on Jun 27, 2002 at 17:19 UTC
    To comply with use strict on lexical scope, I reckon I need to start off with a variable declaration, which *doesn't* include an assignment: my $word;
    No, you only need the my in there. Whether you assign something or not doesn't matter any. May I suggest though, that in your case it would be more readable to do it this way:
    my $word = $part eq 'n' ? new Noun(@blah) : $part eq 'v' ? new Verb(@blah) : new Adjective(@blah); print $word->get_english();
    Update: DOH! Ack. I don't know where I left my brain while I was posting the initial version of this. japhy++

    Makeshifts last the longest.

      Ouch. Remove those extra $word ='s. Not only are they assigning to a package-scoped $word, they're screwing up your ?: code. And you have a ":" where you want a "?", beneath the first "?".
      japhy% perl -MO=Deparse,-p my $word = $part eq 'n' ? $word = new Noun(@blah) : $part eq 'v' ? $word = new Verb(@blah) : $word = new Adjective(@blah);
      Check that out and you'll see that $word ends up getting assigned as an Adjective object no matter what, because of the precedence of ?: compared with that of =.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://177754]
Approved by Rex(Wrecks)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found