Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Indirect variable name

by FreakyGreenLeaky (Sexton)
on Nov 20, 2008 at 16:29 UTC ( [id://724917]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Indirect variable name
in thread Indirect variable name

Using symbolic references for plain variables works as expected. However, I'm stuck on how to reference 'flag' below.

Origional code:
package Util::Stuff::aaa1; sub flag { 10 } #### use Util::Stuff; print Util::Stuff::aaa1->flag; # yields 10 ... our $v = 'Util::Stuff::aaa1'; print $$v->flag; # yields undef error ... our $v = 'Util::Stuff::aaa1->flag'; print "$$v\n"; # yields nothing

If it wasn't for the need to find a clean way to glue two separate projects together, I wouldn't bother with this. However, it's an interesting issue and I'm keen to learn something different.

Thanks!
Henry

Replies are listed 'Best First'.
Re^5: Indirect variable name
by JadeNB (Chaplain) on Nov 20, 2008 at 17:08 UTC
    print Util::Stuff::aaa1->flag; # yields 10 ... our $v = 'Util::Stuff::aaa1'; print $$v->flag; # yields undef error ... our $v = 'Util::Stuff::aaa1->flag'; print "$$v\n"; # yields nothing
    The last 2 aren't working because, in the second case, you are trying to call the flag method on the variable named Util::Stuff::aaa1; and, in the third case, you are referring to the variable named Util::Stuff::aaa1->flag (no method call is occurring). What you want to do is to call the flag method on the class Util::Stuff::aaa1 itself—no symbolic references are required.
    our $v = 'Util::Stuff::aaa1'; print $v->flag;
      In other words $$v->flag is not being dereferenced and the method called, as I incorrectly assumed.

      Thanks for all the help.
        I'm confused ... do you really want to do
        print Util::Stuff::aaa1->flag();
        or rather
        print Util::Stuff::aaa1::flag();
        in other words, are you importing moduls or classes ? I understood simple moduls...

        Does flag() need to know, in which package he was called?

        Cheers LanX

        UPDATE: you may want to have a look into perlboot if you don't know the difference... perl isn't a language one can master with try and error hacking ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found