Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: chr() function

by dsheroh (Prior)
on Nov 14, 2018 at 09:41 UTC ( #1225775=note: print w/replies, xml ) Need Help??


in reply to Re: chr() function
in thread chr() function

Please don't tell people to use $a just so they can avoid having to properly declare a real variable name. $a is a piss-poor variable name in general, completely antithetical to writing clear code, and the only reason that works is because of magic which you didn't bother to explain - magic which would one day bite the new guy in the ass if he took your ill-conceived advice and made a habit of using undeclared $as and $bs on a regular basis.

If you're really fixated on not having to properly declare your variables, it's much saner to turn strict off. And "it's much saner to turn strict off" is not something I say lightly, it's a reflection of just how astonishingly bad the advice "change your variable name to $a so you don't have to declare it" is.

Replies are listed 'Best First'.
Re^3: chr() function
by harangzsolt33 (Friar) on Nov 14, 2018 at 18:14 UTC

    The reason why I told him to use variable $a is because his entire program is only 4 lines long. Anybody looking at it can figure out what it is doing and why. And I think, it is logical, since he wanted to store the value of ASCII character "A" in a variable named $alpha. You know, you might as well store it in $a. This was by no means a suggestion that it is a good practice or that we should all do this.

    I also have MS-DOS installed on my computer with Perl 5.004. So, I also test my programs on that earlier perl to see what works and what doesn't just for fun. And if all I have to do is change some minor things in my code, then I rather do that to make sure my code is backward compatible. I like when a program runs on as many computers as possible and on as many versions of perl as possible.

      I disagree. Suggesting someone to use $a (or $b) special auto-declared (package global) variables for the purpose of avoiding the need of variable declaration with my is really poor practice.

      The:

      my $alpha = chr(65);
      solution suggested earlier by LanX is far better.

      There are a few cases where $a and $b package global variables are really useful (especially for a sort subroutine, but not only that, some other functional programming techniques can also make good use of them), please don't use them when simply declaring the variable can make what you want.

      The reason why I told him to use variable $a is because his entire program is only 4 lines long.
      And tiny programs like that, or shell one-liners, are among the very few cases where not using strict is a sane course of action.
      You know, you might as well store it in $a. This was by no means a suggestion that it is a good practice or that we should all do this.
      Then why didn't you give any warnings or other indications (aside from an emoticon, which is hardly a specific message) to indicate to the OP, who clearly appears to be relatively new to Perl (since they're not familiar with strict or the requirements it places on your code), that it's not a good practice or that they shouldn't do it all the time? Not even a simple "you shouldn't generally do this, but, in this case, because it's so simple, you can get away with it". Just "use $a instead of $alpha" with no warnings at all that it might be inappropriate or dangerous in other contexts.
      The reason why I told him to use variable $a is because his entire program is only 4 lines long

      In your original post, you didn't tell him to use variable $a ... you merely stated a fact.
      And you provided a nice demo of that fact !!

      (At least, that's the line of defence that I would have taken ;-)

      Cheers,
      Rob

      It is like it is: $a and $b have a special meaning since the Stone Age. See sort and perlvar for details. Accept this and stop bothering the fellow monks with this crap. Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1225775]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-05-30 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (171 votes). Check out past polls.

    Notices?