Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Why did @$ variable bite me in the ass?

by dmitri (Priest)
on Feb 26, 2015 at 19:12 UTC ( [id://1117980]=perlquestion: print w/replies, xml ) Need Help??

dmitri has asked for the wisdom of the Perl Monks concerning the following question:

So there I was, happily rolling along, writing my class, and using an array ref as my underlying data structure. I then wrote another method:
sub another_method { my ($self, $magic_val) = @_; $$[MAGIC_VALS]{$magic_val} = 1; }
and ran it. There were no compile-time or run-time errors, but the logic did not work. Can you spot the problem? That's right: it should be $$self[MAGIC_VALS]{$magic_val} instead! What is this magical @$? Perl does not complain about it and will happily use this array:
use strict; use warnings; $$[0] = 1; $$[1] = 'dude!'; print "@$", "\n"; # Prints "1 dude!"
It's not in perlvar... What's up with this variable?

Replies are listed 'Best First'.
Re: Why did @$ variable bite me in the ass?
by LanX (Saint) on Feb 26, 2015 at 19:17 UTC
    IIRC do global symbols (aka type globs) of special vars in perlvar reserve all types.

    And $$ exists.

    Same with @a or %_ and so on ...

    We had this discussion already many times, try super search for more details. :)

    HTH!

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

    PS:

    > Can you spot the problem?

    yes I spotted it right away! :)

    updates
    Various additions for clarification
      Thank you for the speedy reply! I am reading Chapter 10 of Programming Perl, 3rd Edition, looking for the global symbol bit of wisdom you refer to.

      I did use the Super Search before posting, but my query for "@$" did not produce anything related to my issue. What search terms did you use?

        > What search terms did you use?

        I queried old biological device, glued to my neck and fueled with with sandwiches and coffee, but unfortunately not linkable. ;)

        I'll try finding it (slow with mobile) and will update/msg you. :)

        update

        Sorry, can't find it, but I'm sure we had this discussion... but people always complain about various vars, not only $$.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        PS: Je suis Charlie!

      Perhaps someone will make a list of all of the variable names that don't trigger strict messages?

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        > make a list of all of the variable names that don't trigger strict messages?

        Isn't it pretty obvious?

        Parse perlvar or English and all missing sigils per listed symbol don't bother strict.

        Eg symbol _ => %_ is ignored, cause $_,@_ exist.

        edit

        And to be sure try string eval with strict and catch (the missing) error.

        update

        Hmm parsing %main:: might do already ... Can't check while mobile. (update: Nope! :)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        PS: Je suis Charlie!

Re: Why did @$ variable bite me in the ass?
by Anonymous Monk on Feb 26, 2015 at 19:33 UTC
    That's right: it should be $$self[MAGIC_VALS]{$magic_val} instead!
    I'd say it should've been $self->[MAGIC_VALS]{$magic_val}... But yeah, as LanX said, @$ is part of the $ typeglob. @$ and its friend %$ are not 'magic', but they are a part of a larger structure, one element of which ($$) is 'magic', and so the entire glob is special-cased to supress strict... something like that.
Re: Why did @$ variable bite me in the ass? (perlcritic)
by Anonymous Monk on Feb 27, 2015 at 03:38 UTC
    because you didn't ask perlcritic for help
    Magic punctuation variable $$ used at line 3, column 5. See page 79 o +f PBP. (Severity: 2) Magic variable "$$" should be assigned as "local" at line 3, column 32 +. See pages 81,82 of PBP. (Severity: 4)
      tieing unused slots of global symbols to classes which croak at access should also do.

      I can understand why these names are reserved, but not why there is no warning implemented.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1117980]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-13 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found