in reply to Re: How to Remove Commas ?
in thread How to Remove Commas ?

flexvault,

Thanks for taking the time to read my post and script. The comment is a quip for the professor, who made an off-hand comment during the lecture about declaring variables.

Which is the better way of declaring variables ... that depends on what the compiler (or professor, or employer) wants! I do feel more at ease declaring my variables beforehand, though. legacy feelings of inadequacy left over from previous programming languages ... :)

Thanks ... Allen.

Replies are listed 'Best First'.
Re^3: How to Remove Commas ?
by ww (Archbishop) on Mar 27, 2012 at 14:11 UTC
    Were I your professor, I'd mark that quip down, since it's attached to a method of declaring variables that's legal, but falls far short of 'best practice.'

    Perl is not one of your "previous programming languages" and your response to your own rhetorical question about "the better way of declaring variables" isn't quite 'on-target' here. Perl (well, thru 5.10, IIRC) will accept the way you've done it, without even suggesting that there's a better way... which involves understanding scope... and the best practice of limiting a variable's scope as narrowly as possible.

Re^3: How to Remove Commas ?
by tobyink (Canon) on Mar 27, 2012 at 16:10 UTC

    Best practice for declaring variables varies from programming language to programming language. But the declaration you used is very far from best practice in Perl.

    When declaring a variable you in Perl, it is best practice to declare whether it's a lexical (my) or package (our) variable. e.g.:

    my $counter = 0;

    It's also best practice to declare the variable in the tightest possible scope. For example, if a variable is used inside a loop, and needs reinitialising each time round the loop, then declare it inside the loop, not before the loop.

    Find the bug here:

    use 5.010; use strict; my $gender = 'unknown'; while (<DATA>) { chomp; given ($_) { when ("Alice") { $gender = 'female' } when ("Annie") { $gender = 'female' } when ("Andy") { $gender = 'male' } when ("Arnold") { $gender = 'male' } } say "$_ is $gender."; } __DATA__ Alice Andy Arnold Jennifer Annie Henry

    Output is:

    Alice is female.
    Andy is male.
    Arnold is male.
    Jennifer is male.
    Annie is female.
    Henry is female.
    

    Why is Jennifer male? Why is Henry female? It's because $gender is declared outside the loop, so is allowed to stay alive between loop iterations. Merely moving the one line where it's declared solves our subtle bug:

    use 5.010; use strict; while (<DATA>) { chomp; my $gender = 'unknown'; given ($_) { when ("Alice") { $gender = 'female' } when ("Annie") { $gender = 'female' } when ("Andy") { $gender = 'male' } when ("Arnold") { $gender = 'male' } } say "$_ is $gender."; } __DATA__ Alice Andy Arnold Jennifer Annie Henry
    Alice is female.
    Andy is male.
    Arnold is male.
    Jennifer is unknown.
    Annie is female.
    Henry is unknown.
    

    Perl variables have some pretty cool features, but they can trip you up. The precaution of adding use strict near the top of each script is generally a wise one. This one line tells Perl to force you to declare all your variables. This doesn't stop you shooting yourself in the foot, but it makes it difficult to shoot yourself in the foot accidentally. (You're still able to shoot yourself in the foot if you put in a bit of effort.)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: How to Remove Commas ?
by flexvault (Monsignor) on Mar 27, 2012 at 13:58 UTC

    allendevans,

    Glad it was from the professor!

    Have a good one!

    "Well done is better than well said." - Benjamin Franklin