(Lists and Output Formats) RE: Should I use $ and $# ?
by mwp (Hermit) on Aug 11, 2000 at 00:58 UTC
|
Hi Guildenstern,
Everyone has pretty much covered the basic info regarding
$[ and $#, but I wanted to point out that $# is depreciated
with regards to the output format of numbers, and NOT for
lists. $#LIST still returns the index of the last element of a
list, and is not depreciated at all! $#LIST is the equivalent
of scalar(@LIST)-1 and somewhat faster, I believe.
With regards to Perl 6, expect many features and functions
to be changed or completely removed. I have been following
the RFCs and the language mailing list, and I believe that
a pretty hefty Perl 5 to Perl 6 translator is planned. :-)
So you should really only be worried about compatibility
with future version of Perl 5, and I daresay that Perl 5
isn't going to change much after 5.6.
Best of luck,
Alakaboo
| [reply] |
Re: Should I use $ and $# ?
by tilly (Archbishop) on Aug 11, 2000 at 00:25 UTC
|
Deprecated features are headed for removal. Don't expect
them to survive into Perl 6. Also don't expect bugs that
come up for them to be treated as a priority.
I remember hearing of at least one bug with $[ where the
response was, "Oh well, people shouldn't be doing that any
more anyways." I would have to search to verify that
though.
YMMV, but I certainly believe that "deprecated" means
"dangerous - avoid". | [reply] |
|
|
See
This node about the 'our' keyword for a discussion
about the word "deprecated". Turns out that it really
means "reserved" as in, it was used once, or may be
used in the future.
At any rate, $# is fairly widely used, and $[
is not widely used, but I'd hardly call it one of the
more obscure special variables. It's one that you should
almost never change but it is perfectly
alright to use. Not that I ever do,
but:
for $x ($] .. $#myarray)
## is actually technically better than
for $x (0..$#myarray)
But still, the occasions to change it outside of an
obfuscation are few and far between.
| [reply] [d/l] |
|
|
Use of reserved word """"%s"""" is deprecated
(D) The indicated bareword is a reserved word. Future
versions of perl may use it as a keyword, so you're
better off either explicitly quoting the word in a
manner appropriate for its context of use, or using a
different name altogether. The warning can be
suppressed for subroutine names by either adding a &
prefix, or using a package qualifier, e.g. &our(), or
Foo::our().
In general if you get any message from Perl that
you do not understand, you should try "perldoc perldiag"
and search for that message.
Compare with the related message:
Use of %s is deprecated
(D) The construct indicated is no longer recommended
for use, generally because there's a better way to do
it, and also because the old way has bad side effects.
Which is, of course, what you get when the keyword is deprecated.
So your believing that our was deprecated because
someone got that message simply means that you (and they)
did not understand the message, and didn't know how to
RTFM. Now you do. :-) | [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
That's exactly how I've been using it, and it works much cleaner than other solutions I've tried.
The new edition of the Camel book says for each, "Deprecated, do not use in anything new." It sounds more to me like calling the use of 'our' deprecated was a mistake. It should have been given a better message about being reserved for future use. Deprecated has always meant to me that something had been superceded by something better (safer, faster, etc) so you should transition before the feature is removed. Just my .02
| [reply] |
|
|
| [reply] |
|
|
And this is directly from perlvar:
$[
...snip...
As of Perl 5, assignment to "$[" is treated as a
compiler directive, and cannot influence the
behavior of any other file. Its use is
discouraged.
Cheers,
KM | [reply] |
Re: Should I use $ and $# ?
by chromatic (Archbishop) on Aug 11, 2000 at 00:26 UTC
|
I don't use $# much myself, as evaluating the array in scalar context gets the length just fine, and negative values are allowed as subscripts. (I don't believe I've ever used [$[].)I suppose if you really needed to write a C-style loop and index into the array, it would come in handy, but I've not seen the need in a long time.
Generally, operators such as shift, pop, push and unshift do the job for me, while foreach (@array) takes care of the rest. | [reply] |
RE: Should I use $ and $# ?
by Adam (Vicar) on Aug 11, 2000 at 00:35 UTC
|
I'm kind of curious as to when a Perl Monk suggested the use of these two variables. Short of an obfuscated post, there is no reason to use them. | [reply] |
|
|
I didn't have the foresight to note who it was, or even what the topic was, but I do remember the drift of the topic. Somebody had asked a question that dealt with iterating over an array. One of the first posts suggested using $# as the upper limit of the for. A later post suggested using $[ as the lower limit, since there was no guarantee that the array began at index 0. I'll keep searching. It would be good for me to find again, since the details of that topic may be completely different than what I'm concerned about.
| [reply] |
|
|
Ok, I see where you are going with this. I used to use $[ like that too, but it's both hard to read and unnecessary unless you alter it somewhere else. So if you don't touch $[ anywhere, there is no reason not to use 0. Besides, its somewhat deprecated.As for $#, that has to do with formatting print statements... not array boundaries, unless of course, you meant $#arrayName which is the index of the last element of the specified array and certainly not deprecated. In fact, not only does it give you the last index, but you can use it as an lvalue to change the size of an array. (And yes, the use of $# by itself to alter the formatting of print statements is highly deprecated and should not be used.)
| [reply] |
|
|