ghenry has asked for the wisdom of the Perl Monks concerning the following question:
Update: That's deprecated, not depreciated ;-)
Dear Monks,
Just a quickie, is $# still used, as I saw it in the first section of Higher Order Perl, in the Honoi example, but I haven't used it before (which shows my skill level). So I checked it out in perlvar, but it says it is depreciated?
But if it's in HOP, I take it its not.
Thanks.
Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!
Re: Is $# still used?
by Zaxo (Archbishop) on Jun 11, 2005 at 10:26 UTC
|
It's documented as deprecated, so it probably is. I've never used it, but a few trials turn up some fishy results:
$ perl -e'$_=1.657329486;$#="%8d";print,print $/'
-330231720
$ perl -e'$_=1.657329486;$#="%05f";print,print $/'
1.657329
$ perl -e'$_=1.657329486;$#="%06f";print,print $/'
1.657329
$ perl -e'$_=1.657329486;$#="%08f";print,print $/'
1.657329
$ perl -e'$_=1.657329486;$#="%s";print,print $/'
Segmentation fault (core dumped)
$
Yet another solution to the age-old question of how to make perl segfault.
I haven't seen HOP yet. Is MJD talking about the $#foo construct for the last index of @foo, instead?
| [reply] [d/l] |
|
$# has been deprecated at least since the
second-edition Camel, and I guess probably since
the early 5.0 days, which is virtually forever.
However, it is still occasionally used and/or abused,
especially in obfuscated code. It does not exist
in Perl6, so its usage will die out with Perl5 usage,
which, judging by the claims you occasionally
hear that Perl4 is still in use some places, could
be some time after Duke Nukem Forever is released.
"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."
— Pratico & Van Pelt, BBHG, p68
| [reply] |
|
| [reply] |
Re: Is $# still used?
by gaal (Parson) on Jun 11, 2005 at 09:15 UTC
|
I must have missed the deprecation announcement — you've quite surprised me.
$# is useful when you need an explicit index, eg., 0 .. $#arr . Sure, it's more perlish to avoid the index and iterate over the memebers directly, but sometimes you need this, for example when accessing two related arrays. (Yes, I know. If they're related then they may have been associated together in a list of hashes. Sometimes that's not appropriate though.)
In Perl 6, the .kv method operates on arrays too, yielding numeric indexes and values:
say "what is your favorite color?";
my @favorite_colors = ("blue", "red", "uhh, yellow!");
for @favorite_colors.kv -> $id, $color {
say "$id. $color";
}
| [reply] [d/l] [select] |
|
| [reply] |
|
Ah! I'm much less surprised now. In Perl, "there's something more" happens more often than "there's something less". :-)
| [reply] |
Re: Is $# still used?
by dug (Chaplain) on Jun 11, 2005 at 13:00 UTC
|
I couldn't find the variable $# in the hanoi example in chapter one of HOP. Are you referring to the check-move routine that uses the "last-index" notation $#position?
-- Douglas Hunter | [reply] [d/l] [select] |
|
| [reply] |
Re: Is $# still used?
by bart (Canon) on Jun 11, 2005 at 10:33 UTC
|
I seem to remember an RFC in the early days of Perl6, for recycling the rarely used special variable $# for more useful purposes, in this case, the current loop index/item inside a foreach loop. That must have been close to 5 years ago.
So yes, it was rarely used and deprecated back then, I guess it's even ore so now.
| [reply] |
|
|