FAQ
Just a quick question on the need of "my" http://perldoc.perl.org/functions/my.html

Was using an example from the beginning perl text in the example he
gave he wasn't using strict but i have it on to all scripts by
default.

For this example. the first two assignments work okay, but then at $c
if I do not declare my local then I get an error. $d also requires
that it be assigned using my, though it is directly the same as $b, my
assumption here is that $d required explicit assignment as it used the
explicitly assigned variable $c (could be wrong).

Why from $c was "my" required?
NB I am using 5.14.1

#!/usr/bin/perl -w
# vars3.pl --- Assignment of vars using strict
# Author: sayth <sayth@sayth>
# Created: 05 Sep 2011
# Version: 0.01

use warnings;
use strict;

$a = 6*9;
print "six times 9 is ", $a, "\n";
$b = $a + 3;
print "plus three is ", $b,"\n";
my $c = $b/3;
print "Divided by 3 is ", $c, "\n";
my $d = $c + 1;
print "plus one is ", $d, "\n";

Search Discussions

  • Brandon McCaig at Sep 5, 2011 at 11:50 am

    On Sun, Sep 4, 2011 at 11:53 PM, flebber wrote:
    For this example. the first two assignments work okay, but then at $c
    if I do not declare my local then I get an error. $d also requires
    that it be assigned using my, though it is directly the same as $b, my
    assumption here is that $d required explicit assignment as it used the
    explicitly assigned variable $c (could be wrong).

    Why from $c was "my" required? *snip*
    $a = 6*9;
    print "six times 9 is ", $a, "\n";
    $b = $a + 3;
    print "plus three is ", $b,"\n";
    my $c = $b/3;
    print "Divided by 3 is ", $c, "\n";
    my $d = $c + 1;
    print "plus one is ", $d, "\n";
    $a and $b are special global variables used by sort. :) See perldoc
    perlvar and/or perldoc -f sort. :) So, in short, $a and $b already
    existed, so there was no problem using them without declaring them
    first. You should ALWAYS declare your variables. :) You either use
    'my' for a lexical scope or 'our' for module scope. :) There is also
    'local', but I'm not going to attempt to explain how that works so
    early in the morning. You should be able to learn about each using
    perldoc -f though. To be safe and clear you shouldn't use the names $a
    and $b unless you are actually using them within a sort.


    --
    Brandon McCaig <http://www.bamccaig.com/> <bamccaig@gmail.com>
    V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
    Castopulence Software <http://www.castopulence.org/> <bamccaig@castopulence.org>
  • Uri Guttman at Sep 5, 2011 at 3:51 pm
    "BM" == Brandon McCaig writes:
    BM> On Sun, Sep 4, 2011 at 11:53 PM, flebber wrote:
    For this example. the first two assignments work okay, but then at $c
    if I do not declare my local then I get an error. $d also requires
    that it be assigned using my, though it is directly the same as $b, my
    assumption here is that $d required explicit assignment as it used the
    explicitly assigned variable $c (could be wrong).
    >>
    Why from $c was "my" required?
    BM> *snip*
    $a = 6*9;
    print "six times 9 is ", $a, "\n";
    $b = $a + 3;
    print "plus three is ", $b,"\n";
    my $c = $b/3;
    print "Divided by 3 is ", $c, "\n";
    my $d = $c + 1;
    print "plus one is ", $d, "\n";
    BM> $a and $b are special global variables used by sort. :) See perldoc
    BM> perlvar and/or perldoc -f sort. :) So, in short, $a and $b already
    BM> existed, so there was no problem using them without declaring them
    BM> first. You should ALWAYS declare your variables. :) You either use
    BM> 'my' for a lexical scope or 'our' for module scope. :) There is also

    there is no such thing as module scope. our declares package globals and
    give them a short name in a lexical scope.

    BM> 'local', but I'm not going to attempt to explain how that works so
    BM> early in the morning. You should be able to learn about each using
    BM> perldoc -f though. To be safe and clear you shouldn't use the names $a
    BM> and $b unless you are actually using them within a sort.

    single letter variable names are bad in general. they tell you nothing
    about the use and content of the variable. about the only exception are
    $i and $j for array/matrix indexing and the aforementioned $a and $b for
    scoping.

    uri

    --
    Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
    ------------ Perl Developer Recruiting and Placement Services -------------
    ----- Perl Code Review, Architecture, Development, Training, Support -------
  • Brandon McCaig at Sep 5, 2011 at 6:19 pm

    On Mon, Sep 5, 2011 at 11:51 AM, Uri Guttman wrote:
    there is no such thing as module scope. our declares package globals and
    give them a short name in a lexical scope.
    Ah, yes, thank you for correcting me. :) The line between "module" and
    "package" had blurred on me. Google cleared that up.
    single letter variable names are bad in general. they tell you nothing
    about the use and content of the variable. about the only exception are
    $i and $j for array/matrix indexing and the aforementioned $a and $b for
    scoping.
    I'm used to writing C# and in my experience descriptive variable names
    become both tiresome and difficult to read. Instead, I usually try to
    keep functions/subroutines short and give variable names a mnemonic
    meaning. If it is not obvious what the variable's purpose is then I'll
    leave a comment next to its declaration (often in C# the type alone
    describes the variable sufficiently though). I do use descriptive
    names when the code requires it, but I don't make it a rule to always
    use them. I find that code is often more difficult to read when the
    names exceed a few characters. It's important to see what a variable
    represents, but it's also important to see how that variable interacts
    with the code around it.

    This habit is somewhat holstered due to Perl's use of global variables
    with short names, like $a and $b. I don't remember if there are any
    other /\$[A-za-z]/ variables, but to be safe I generally avoid using
    single character names in Perl (well, OK, also to avoid getting "told"
    on this list when I post my code). I guess variable name length is
    less of an issue in Perl though because the language is so much more
    terse all by itself :P


    --
    Brandon McCaig <http://www.bamccaig.com/> <bamccaig@gmail.com>
    V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
    Castopulence Software <http://www.castopulence.org/> <bamccaig@castopulence.org>
  • Uri Guttman at Sep 5, 2011 at 6:55 pm
    "BM" == Brandon McCaig writes:
    BM> On Mon, Sep 5, 2011 at 11:51 AM, Uri Guttman wrote:
    single letter variable names are bad in general. they tell you nothing
    about the use and content of the variable. about the only exception are
    $i and $j for array/matrix indexing and the aforementioned $a and $b for
    scoping.
    BM> I'm used to writing C# and in my experience descriptive variable names
    BM> become both tiresome and difficult to read. Instead, I usually try to
    BM> keep functions/subroutines short and give variable names a mnemonic
    BM> meaning. If it is not obvious what the variable's purpose is then I'll
    BM> leave a comment next to its declaration (often in C# the type alone
    BM> describes the variable sufficiently though). I do use descriptive
    BM> names when the code requires it, but I don't make it a rule to always
    BM> use them. I find that code is often more difficult to read when the
    BM> names exceed a few characters. It's important to see what a variable
    BM> represents, but it's also important to see how that variable interacts
    BM> with the code around it.

    it doesn't matter the language or the comments. single letter var names
    are just bad coding. names are a communication to the reader of the
    code, not a placeholder or whatever to the coder. much more work needs
    to be put into choosing good names than most coders realize. it is one
    of the major characteristics i look for when i review code (and i review
    a ton of code for my business of place perl hackers).

    BM> This habit is somewhat holstered due to Perl's use of global variables
    BM> with short names, like $a and $b. I don't remember if there are any
    BM> other /\$[A-za-z]/ variables, but to be safe I generally avoid using
    BM> single character names in Perl (well, OK, also to avoid getting "told"
    BM> on this list when I post my code). I guess variable name length is
    BM> less of an issue in Perl though because the language is so much more
    BM> terse all by itself :P

    as i said, name quality is independent of language choice. $a and $b are
    special cases only for sort and nowhere else are any special named
    variables are found in perl (other than the punctuation vars like $!).

    uri

    --
    Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
    ------------ Perl Developer Recruiting and Placement Services -------------
    ----- Perl Code Review, Architecture, Development, Training, Support -------
  • Brandon McCaig at Sep 5, 2011 at 7:15 pm

    On Mon, Sep 5, 2011 at 2:54 PM, Uri Guttman wrote:
    it doesn't matter the language or the comments. single letter var names
    are just bad coding. names are a communication to the reader of the
    code, not a placeholder or whatever to the coder. much more work needs
    to be put into choosing good names than most coders realize. it is one
    of the major characteristics i look for when i review code (and i review
    a ton of code for my business of place perl hackers).
    You're welcome to your opinion.


    --
    Brandon McCaig <http://www.bamccaig.com/> <bamccaig@gmail.com>
    V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
    Castopulence Software <http://www.castopulence.org/> <bamccaig@castopulence.org>
  • Uri Guttman at Sep 5, 2011 at 7:38 pm
    "BM" == Brandon McCaig writes:
    BM> On Mon, Sep 5, 2011 at 2:54 PM, Uri Guttman wrote:
    it doesn't matter the language or the comments. single letter var names
    are just bad coding. names are a communication to the reader of the
    code, not a placeholder or whatever to the coder. much more work needs
    to be put into choosing good names than most coders realize. it is one
    of the major characteristics i look for when i review code (and i review
    a ton of code for my business of place perl hackers).
    BM> You're welcome to your opinion.

    it isn't just mine. and since i have been coding for 37 years, done
    training, review code for placements and more, my opinion carries a
    little weight with those who know me. you may not agree but that puts
    you on the side of not appreciating what code quality means. i can see i
    won't be helping you with any perl jobs in the future as that is an
    attitude i can't support.

    just to clarify, here are the rules simple enough for anyone to
    understand.

    code is for people, not computers.

    code is for other people, not yourself.

    think about those for a bit or a few years maybe. they will eventually
    ring true to you.

    uri

    --
    Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
    ------------ Perl Developer Recruiting and Placement Services -------------
    ----- Perl Code Review, Architecture, Development, Training, Support -------
  • Rob Dixon at Sep 5, 2011 at 8:13 pm

    On 05/09/2011 19:54, Uri Guttman wrote:
    it doesn't matter the language or the comments. single letter var names
    are just bad coding. names are a communication to the reader of the
    code, not a placeholder or whatever to the coder. much more work needs
    to be put into choosing good names than most coders realize. it is one
    of the major characteristics i look for when i review code (and i review
    a ton of code for my business of place perl hackers).
    I would counter with the thought that lack of capitalisation is just
    poor-quality and bad-mannered writing. Capitals provide a focus for the
    reader to understand the meaning of what has been written without having
    to examine every word, just as the serifs on a properly-chosen body text
    font lead the reader to the next symbol.

    Much more work should be put into your literary style than you realise,
    as your documentation and comments will be your sole means of
    communication to all who have site of your code while you are not there
    to explain it.

    I am certain that no one who is unable to format and punctuate their own
    written language properly can possibly imagine how to write software
    that is both functional and comprehensible.

    Rob
  • Rob Dixon at Sep 7, 2011 at 5:49 pm

    On 05/09/2011 21:26, Uri Guttman wrote:
    off list

    rob,
    No. This order of nastiness cannot be done in private. Your words are
    crude and brutal, and backed by no one here.

    ON list.
    what is with your attitude recently? you had that blowup a few months
    ago. are you doing that again? i really think you should take a breather
    here and get your head clear. i am just concerned about your tone here
    right now. i haven't been nasty or anything in my recent posts but you
    are getting personal for no apparent reason. cool it off please.
    If you look back at the archives, I explained 'that blowup' and
    apologised for it. Things are different now, and despite our situation
    we are doing well and improving. Your references to my daughter don't
    help what I am trying to do, and to me they seem rude and inconsiderate.

    I wonder why you have started belittling the questions here instead of
    offering a useful answer. I believe it is for you as an intelligent Perl
    programmer to reply constructively to questions posed here. All we have
    seen rudeness that will discourage any questiond at all.

    Please go think again Uri.

    Rob
  • Uri Guttman at Sep 7, 2011 at 6:56 pm
    "RD" == Rob Dixon writes:
    RD> On 05/09/2011 21:26, Uri Guttman wrote:
    >>
    off list
    >>
    rob,
    RD> No. This order of nastiness cannot be done in private. Your words are
    RD> crude and brutal, and backed by no one here.

    RD> ON list.
    what is with your attitude recently? you had that blowup a few months
    ago. are you doing that again? i really think you should take a breather
    here and get your head clear. i am just concerned about your tone here
    right now. i haven't been nasty or anything in my recent posts but you
    are getting personal for no apparent reason. cool it off please.
    RD> If you look back at the archives, I explained 'that blowup' and
    RD> apologised for it. Things are different now, and despite our situation
    RD> we are doing well and improving. Your references to my daughter don't
    RD> help what I am trying to do, and to me they seem rude and inconsiderate.

    what reference to what daughter? i didn't know about or care about your
    family.

    RD> I wonder why you have started belittling the questions here instead of
    RD> offering a useful answer. I believe it is for you as an intelligent Perl
    RD> programmer to reply constructively to questions posed here. All we have
    RD> seen rudeness that will discourage any questiond at all.

    again, what are you talking about? i made some feedback on some post as
    usual. the OP didn't object nor did anyone else, just you. the last time
    i did something (i upper cased NEVER) it caused a silly firestorm and
    nothing like that happened this time. you just jumped on my comments and
    no one else did.

    uri

    --
    Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
    ------------ Perl Developer Recruiting and Placement Services -------------
    ----- Perl Code Review, Architecture, Development, Training, Support -------
  • Randal L. Schwartz at Sep 16, 2011 at 10:43 pm
    "Rob" == Rob Dixon writes:
    Rob> No. This order of nastiness cannot be done in private. Your words are crude
    Rob> and brutal, and backed by no one here.

    Rob> ON list.

    [presumably private message made public deleted]

    *NOW* you've violated a very fundamental rule of mailing list etiquette.

    You should never ever bring someone's *private* email into a *public*
    list without their permission, no matter how hurt you feel.

    Whatever support you might have gotten from me regarding the issue
    is now *gone*. Is that clear?

    Methinks you should *now* do what I *must* do from time to time here...

    ... and take a break. please.

    go away for a bit. chill out.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
  • Rob Dixon at Sep 17, 2011 at 11:39 pm

    On 16/09/2011 23:43, Randal L. Schwartz wrote:
    "Rob" == Rob Dixon<rob.dixon@gmx.com> writes:
    Rob> No. This order of nastiness cannot be done in private. Your words are crude
    Rob> and brutal, and backed by no one here.

    Rob> ON list.

    [presumably private message made public deleted]

    *NOW* you've violated a very fundamental rule of mailing list etiquette.

    You should never ever bring someone's *private* email into a *public*
    list without their permission, no matter how hurt you feel.

    Whatever support you might have gotten from me regarding the issue
    is now *gone*. Is that clear?

    Methinks you should *now* do what I *must* do from time to time here...

    ... and take a break. please.

    go away for a bit. chill out.
    I hoped to hear about this from the less elite on the group.

    Uri has said a very similar thing about me on list, and there was plenty
    of opportunity there for you to express 'whatever support' you wanted.

    My contravention of your 'fundamental rules' shouldn't stop you
    expressing your thoughts, whether or not they support what you see as my
    position.

    Scattering asterisks and upper case about your post seems rather silly
    to me. Certainly, "Is that clear?" is rhetorical and very rude, and it
    all suggests that you should be taking your own advice here.

    You and Uri are amongst those who see themselves as the elite of the
    Perl community. All that either of you have shown me is that you are
    rather fond of yourselves and like to see your opinions as lore.

    I am no japh - that has become a label for anti-snobbery - I am a Perl
    programmer who likes to understand his craft, perform it well, and pass
    on what he knows to others. I am also grateful that I am able to teach
    well, and can pass on my ideas together with the notion that there are
    always alternative solutions that I have not come across.

    Randal, from your posts here and your public history I worry that you
    lack humility and require that your assertions be accepted as truth. Uri
    seems to behave similarly, and both of you hide behind the defence (that
    I believe you invented) that you attack not the person but their behaviour.

    This being a First-World, Western, Capitalist list, I invite both you
    (Randal) and Uri to exemplify the difference between a person and his
    behaviour.

    In the mean time, to Uri, I am sure the majority of us here would rather
    you stopped flexing your experience muscles at us and started to help us
    become the Perl programmers that we could be. It is embarrassing that
    you promote yourself and your CPAN modules here above everything else
    available, and if there is one regular poster to this list who is
    insecure and identifies themselves with their achievements then it is you.

    A final thought to Randal. I think you are right that I shouldn't have
    publicized a private mail from Uri. In my defence, this is a list where
    rules are different: we try incessantly to get people who post here to
    edit the post they are replying to and add their response afterwards;
    the majority will be used to doing the opposite in casual or business
    mail. We are also often replied to personally - either by mistake or
    because of ignorance - and force that reply back to the relevant thread
    on this list.

    That said, I am aware that some of my posts may have been out of order a
    while ago, and I explained and apologised at the time. I hoped that
    would be the end of it until Uri saw it as a hit on his unblemished record.
    [My opinion] isn't just mine. and since i have been coding for 37
    years, done training, review code for placements and more, my
    opinion carries a little weight with those who know me. you may not
    agree but that puts you on the side of not appreciating what code
    quality means. i can see i won't be helping you with any perl jobs in
    the future as that is an attitude i can't support.
    In my experience, bigness in business is more likely to encourage egoes
    than good programming.

    Rob
  • Rob Dixon at Sep 5, 2011 at 8:03 pm

    On 05/09/2011 16:51, Uri Guttman wrote:
    there is no such thing as module scope. our declares package globals and
    give them a short name in a lexical scope.
    All software comcepts exist only in someone's imagination. Brandon and I
    can imagine what 'module scope' means and I am surprised that you
    cannot. In the end there are only global declarations and scoped
    declarations, and I believe you are wrong to pick holes in people's
    terminology when it is quite clear what they mean, and that meaning is
    correct.
    BM> 'local', but I'm not going to attempt to explain how that works so
    BM> early in the morning. You should be able to learn about each using
    BM> perldoc -f though. To be safe and clear you shouldn't use the names $a
    BM> and $b unless you are actually using them within a sort.

    single letter variable names are bad in general. they tell you nothing
    about the use and content of the variable. about the only exception are
    $i and $j for array/matrix indexing and the aforementioned $a and $b for
    scoping.
    Fortran tradition has us use 'i' .. 'n' for integer values - often
    counters - and everything else for floats, of which 'x' .. 'z' are
    familiar in mathematical and graphical programming. Outside that, qw/Y M
    D h m s/ are universal, $c is an undimensioned constant, $e is exp(1),
    $f is a file or a format, $g is the gravitational constant, $h would be
    height, hours, Henries, and so on.

    I know I have said something very similar before, but those who disagree
    with me are forgetting Perl's legacy. It is a language that is intended
    to cope with ambiguity by context, and can tell the difference between
    the equivalents of there, their and they're; and even its, it's and it's.

    So yes, we are free to use single-character variable names if that use
    is unambiguous in context. Unfortunately most people have a history of
    writing software that looks like bricklaying: every stroke and placement
    has its own independent purpose. So we get C programmers, Fortran
    programmers, and possibly COBOL programmers, each writing Perl code
    where every single line will stand unambiguously on its own. That is not
    what Perl is for, and it goes a long way to explaining the controversy
    that it generates.

    The reason $a and $b are broken is because of a fix on top of a nice
    idea. There should be nothing wrong with names like that but, because no
    one thought of it sooner, it can be a better rule to prohibit
    single-character variable names altogether.

    My rule would be to write what you mean, all of the time. If for some
    defect in the language that doesn't work, then understand why and write
    what you mean in a different way. People will love you for it.

    Rob
  • Randal L. Schwartz at Sep 16, 2011 at 10:39 pm
    "Rob" == Rob Dixon writes:
    Rob> All software comcepts exist only in someone's imagination. Brandon and I
    Rob> can imagine what 'module scope' means and I am surprised that you
    Rob> cannot. In the end there are only global declarations and scoped
    Rob> declarations, and I believe you are wrong to pick holes in people's
    Rob> terminology when it is quite clear what they mean, and that meaning is
    Rob> correct.

    I can imagine what a module scope might be, and what "our" does isn't
    it. So there's the problem. We imagine different things for that word,
    and then we have no authority to define it.

    That's *why* you should use the *proper* words. No communication
    without that.

    For the record, "my" and "our" have *precisely* the same scoping
    rules. The variable name they introduce has different bindings, however.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 5, '11 at 3:55a
activeSep 17, '11 at 11:39p
posts14
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase