Professional Web Applications Themes

FQN from references - PERL Beginners

Hi All, How do i know the full name of a variable from its reference??. Say i have a routine defined like this: package Bar; sub foo {}; And then i say: my $x = \&foo; Now from this coderef ($x) i want to know its fully qualified name which would be somthing like Bar::Foo. Is there a way to do that.??. Thanx, -Sharad...

  1. #1

    Default FQN from references


    Hi All,

    How do i know the full name of a variable from its reference??.

    Say i have a routine defined like this:

    package Bar;
    sub foo {};

    And then i say:
    my $x = \&foo;

    Now from this coderef ($x) i want to know its fully qualified name which would be somthing like Bar::Foo.
    Is there a way to do that.??.

    Thanx,
    -Sharad





    Sharad Gupta Guest

  2. #2

    Default Re: FQN from references

    Gupta, Sharad wrote:
    > Hi All,
    >
    > How do i know the full name of a variable from its reference??.
    >
    > Say i have a routine defined like this:
    >
    > package Bar;
    > sub foo {};
    >
    > And then i say:
    > my $x = \&foo;
    >
    > Now from this coderef ($x) i want to know its fully qualified name which would be somthing like Bar::Foo.
    > Is there a way to do that.??.
    >
    Where do you want to know it? Presumably if you have set it then you
    should know it already, if it is set dynamically you still have to have
    the name somewhere that you can access, then it is just a matter of
    storing it smartly, aka rather than using

    my $x = \&foo;

    Why not:

    my %handler;
    $handler{'Bar::Foo'} = \&foo;

    Now you can access the reference and have its name at the same time. The
    problem comes in with inheritance because maybe $y wants to call $x
    (foo) and thinks it is in Bar but it really is in Baz, does this mean it
    shouldn't be called, or just that it was inherited and it is ok to call?

    Within foo, there is __PACKAGE__ which will give you Bar...

    You may also want to have a look at Symbol Tables in perldoc perlmod....

    Gurus, am I missing out on something?? ;-)

    [url]http://danconia.org[/url]

    Wiggins D'Anconia Guest

  3. #3

    Default Re: FQN from references

    On Wed, Oct 22, 2003 at 08:22:47PM -0500, Wiggins d'Anconia wrote:
    > Gupta, Sharad wrote:
    >> Now from this coderef ($x) i want to know its fully qualified name
    >> which would be somthing like Bar::Foo. Is there a way to do that.??.
    >
    > Where do you want to know it? Presumably if you have set it then you
    > should know it already [ snip ]
    >
    > Within foo, there is __PACKAGE__ which will give you Bar...
    Given only the coderef, you can use something like this

    #!/usr/bin/perl -l

    sub fq_name {
    require B;
    my $ref = shift or return;
    my $glob = B::svref_2object($ref)->GV;
    join "::", $glob->STASH->NAME, $glob->SAFENAME;

    }

    print fq_name($_) for \&foo, sub {};

    But normally you don't need to know the name of a code reference in
    order to use it. (And anonymous coderefs don't really have a name.)

    --
    Steve
    Steve Grazzini Guest

  4. #4

    Default RE: FQN from references

    I can't store it somewhere. I have a routine to which a user can pass a coderef.
    And in that routine i want to know the full name of that coderef.

    -Sharad
    > -----Original Message-----
    > From: Wiggins d'Anconia [mailto:wigginsdanconia.org]
    > Sent: Wednesday, October 22, 2003 6:23 PM
    > To: Gupta, Sharad
    > Cc: [email]beginnersperl.org[/email]
    > Subject: Re: FQN from references
    >
    >
    > Gupta, Sharad wrote:
    > > Hi All,
    > >
    > > How do i know the full name of a variable from its reference??.
    > >
    > > Say i have a routine defined like this:
    > >
    > > package Bar;
    > > sub foo {};
    > >
    > > And then i say:
    > > my $x = \&foo;
    > >
    > > Now from this coderef ($x) i want to know its fully
    > qualified name which would be somthing like Bar::Foo.
    > > Is there a way to do that.??.
    > >
    >
    > Where do you want to know it? Presumably if you have set it then you
    > should know it already, if it is set dynamically you still
    > have to have
    > the name somewhere that you can access, then it is just a matter of
    > storing it smartly, aka rather than using
    >
    > my $x = \&foo;
    >
    > Why not:
    >
    > my %handler;
    > $handler{'Bar::Foo'} = \&foo;
    >
    > Now you can access the reference and have its name at the
    > same time. The
    > problem comes in with inheritance because maybe $y wants to call $x
    > (foo) and thinks it is in Bar but it really is in Baz, does
    > this mean it
    > shouldn't be called, or just that it was inherited and it is
    > ok to call?
    >
    > Within foo, there is __PACKAGE__ which will give you Bar...
    >
    > You may also want to have a look at Symbol Tables in perldoc
    > perlmod....
    >
    > Gurus, am I missing out on something?? ;-)
    >
    > [url]http://danconia.org[/url]
    >
    >
    Sharad Gupta Guest

  5. #5

    Default RE: FQN from references

    Hoooo!!. I am spinning ;)
    I'll look at B.

    The problem was i wanted to know the name of the coderef to generate an error message.

    Thanx,
    -Sharad
    > -----Original Message-----
    > From: Steve Grazzini [mailto:grazzpobox.com]
    > Sent: Wednesday, October 22, 2003 6:58 PM
    > To: Wiggins d'Anconia
    > Cc: Gupta, Sharad; [email]beginnersperl.org[/email]
    > Subject: Re: FQN from references
    >
    >
    > On Wed, Oct 22, 2003 at 08:22:47PM -0500, Wiggins d'Anconia wrote:
    > > Gupta, Sharad wrote:
    > >> Now from this coderef ($x) i want to know its fully qualified name
    > >> which would be somthing like Bar::Foo. Is there a way to
    > do that.??.
    > >
    > > Where do you want to know it? Presumably if you have set it
    > then you
    > > should know it already [ snip ]
    > >
    > > Within foo, there is __PACKAGE__ which will give you Bar...
    >
    > Given only the coderef, you can use something like this
    >
    > #!/usr/bin/perl -l
    >
    > sub fq_name {
    > require B;
    > my $ref = shift or return;
    > my $glob = B::svref_2object($ref)->GV;
    > join "::", $glob->STASH->NAME, $glob->SAFENAME;
    >
    > }
    >
    > print fq_name($_) for \&foo, sub {};
    >
    > But normally you don't need to know the name of a code reference in
    > order to use it. (And anonymous coderefs don't really have a name.)
    >
    > --
    > Steve
    >
    Sharad Gupta Guest

  6. #6

    Default Re: FQN from references

    Gupta, Sharad wrote:
    > I can't store it somewhere. I have a routine to which a user can pass a coderef.
    > And in that routine i want to know the full name of that coderef.
    >
    > -Sharad
    But in that case isn't it just a design issue, to me it would make more
    sense to pass the name of the routine and call it directly from the
    symbol table rather than passing the code ref, or pass both the ref and
    its' name, since they are really independent pieces of information you
    don't *need* one to use the other, it would limit the ability of the sub
    if you are passing a code ref, and it is expecting a code ref & and sub
    name in the same argument, since presumably if you want the code to call
    it as a subroutine an anonymous code ref should work in the same manner
    as any other code ref, but it won't have a name, so you are really
    wanting "a code ref with a name" as your argument, but at that point it
    makes more sense to break that into "a code ref, and a name"....

    But then, obviously I haven't seen the rest of the code......

    [url]http://danconia.org[/url]
    >
    >
    >>-----Original Message-----
    >>From: Wiggins d'Anconia [mailto:wigginsdanconia.org]
    >>Sent: Wednesday, October 22, 2003 6:23 PM
    >>To: Gupta, Sharad
    >>Cc: [email]beginnersperl.org[/email]
    >>Subject: Re: FQN from references
    >>
    >>
    >>Gupta, Sharad wrote:
    >>
    >>>Hi All,
    >>>
    >>>How do i know the full name of a variable from its reference??.
    >>>
    >>>Say i have a routine defined like this:
    >>>
    >>>package Bar;
    >>>sub foo {};
    >>>
    >>>And then i say:
    >>>my $x = \&foo;
    >>>
    >>>Now from this coderef ($x) i want to know its fully
    >>
    >>qualified name which would be somthing like Bar::Foo.
    >>
    >>>Is there a way to do that.??.
    >>>
    >>
    >>Where do you want to know it? Presumably if you have set it then you
    >>should know it already, if it is set dynamically you still
    >>have to have
    >>the name somewhere that you can access, then it is just a matter of
    >>storing it smartly, aka rather than using
    >>
    >>my $x = \&foo;
    >>
    >>Why not:
    >>
    >>my %handler;
    >>$handler{'Bar::Foo'} = \&foo;
    >>
    >>Now you can access the reference and have its name at the
    >>same time. The
    >>problem comes in with inheritance because maybe $y wants to call $x
    >>(foo) and thinks it is in Bar but it really is in Baz, does
    >>this mean it
    >>shouldn't be called, or just that it was inherited and it is
    >>ok to call?
    >>
    >>Within foo, there is __PACKAGE__ which will give you Bar...
    >>
    >>You may also want to have a look at Symbol Tables in perldoc
    >>perlmod....
    >>
    >>Gurus, am I missing out on something?? ;-)
    >>
    >>[url]http://danconia.org[/url]
    >>
    >>
    >
    >
    >
    Wiggins D'Anconia Guest

  7. #7

    Default Re: FQN from references

    Gupta, Sharad wrote:
    > Hoooo!!. I am spinning ;)
    > I'll look at B.
    >
    > The problem was i wanted to know the name of the coderef to generate an error message.
    >
    Ahh... again depending on your use you may want to have a look at:

    perldoc -f caller

    If you haven't, it is very affective for creating traces....

    [url]http://danconia.org[/url]

    Wiggins D'Anconia Guest

  8. #8

    Default RE: FQN from references

    Hi D',

    I agree whatever you are saying.
    But i need to support somebody's code without annoying users by breaking their old code which depends on this interface.

    Yes, i can expand the scope of the routine now and let users pass in the additional information.
    But again if i can guess it somehow, why to make it mandatory in the interface.

    -Sharad
    > -----Original Message-----
    > From: Wiggins d'Anconia [mailto:wigginsdanconia.org]
    > Sent: Wednesday, October 22, 2003 8:53 PM
    > To: Gupta, Sharad
    > Cc: [email]beginnersperl.org[/email]
    > Subject: Re: FQN from references
    >
    >
    > Gupta, Sharad wrote:
    > > I can't store it somewhere. I have a routine to which a
    > user can pass a coderef.
    > > And in that routine i want to know the full name of that coderef.
    > >
    > > -Sharad
    >
    > But in that case isn't it just a design issue, to me it would
    > make more
    > sense to pass the name of the routine and call it directly from the
    > symbol table rather than passing the code ref, or pass both
    > the ref and
    > its' name, since they are really independent pieces of
    > information you
    > don't *need* one to use the other, it would limit the ability
    > of the sub
    > if you are passing a code ref, and it is expecting a code ref
    > & and sub
    > name in the same argument, since presumably if you want the
    > code to call
    > it as a subroutine an anonymous code ref should work in the
    > same manner
    > as any other code ref, but it won't have a name, so you are really
    > wanting "a code ref with a name" as your argument, but at
    > that point it
    > makes more sense to break that into "a code ref, and a name"....
    >
    > But then, obviously I haven't seen the rest of the code......
    >
    > [url]http://danconia.org[/url]
    >
    > >
    > >
    > >>-----Original Message-----
    > >>From: Wiggins d'Anconia [mailto:wigginsdanconia.org]
    > >>Sent: Wednesday, October 22, 2003 6:23 PM
    > >>To: Gupta, Sharad
    > >>Cc: [email]beginnersperl.org[/email]
    > >>Subject: Re: FQN from references
    > >>
    > >>
    > >>Gupta, Sharad wrote:
    > >>
    > >>>Hi All,
    > >>>
    > >>>How do i know the full name of a variable from its reference??.
    > >>>
    > >>>Say i have a routine defined like this:
    > >>>
    > >>>package Bar;
    > >>>sub foo {};
    > >>>
    > >>>And then i say:
    > >>>my $x = \&foo;
    > >>>
    > >>>Now from this coderef ($x) i want to know its fully
    > >>
    > >>qualified name which would be somthing like Bar::Foo.
    > >>
    > >>>Is there a way to do that.??.
    > >>>
    > >>
    > >>Where do you want to know it? Presumably if you have set it
    > then you
    > >>should know it already, if it is set dynamically you still
    > >>have to have
    > >>the name somewhere that you can access, then it is just a matter of
    > >>storing it smartly, aka rather than using
    > >>
    > >>my $x = \&foo;
    > >>
    > >>Why not:
    > >>
    > >>my %handler;
    > >>$handler{'Bar::Foo'} = \&foo;
    > >>
    > >>Now you can access the reference and have its name at the
    > >>same time. The
    > >>problem comes in with inheritance because maybe $y wants to call $x
    > >>(foo) and thinks it is in Bar but it really is in Baz, does
    > >>this mean it
    > >>shouldn't be called, or just that it was inherited and it is
    > >>ok to call?
    > >>
    > >>Within foo, there is __PACKAGE__ which will give you Bar...
    > >>
    > >>You may also want to have a look at Symbol Tables in perldoc
    > >>perlmod....
    > >>
    > >>Gurus, am I missing out on something?? ;-)
    > >>
    > >>[url]http://danconia.org[/url]
    > >>
    > >>
    > >
    > >
    > >
    >
    >
    Sharad Gupta Guest

Similar Threads

  1. Web References
    By sendhil in forum ASP.NET Web Services
    Replies: 5
    Last Post: December 11th, 08:34 PM
  2. Getting references
    By John in forum Ruby
    Replies: 2
    Last Post: November 3rd, 07:35 AM
  3. ASP.net using UNC references and IIS 5.0
    By Erik Lindeblom in forum ASP.NET General
    Replies: 1
    Last Post: August 6th, 09:32 AM
  4. references help
    By slash in forum PERL Miscellaneous
    Replies: 6
    Last Post: July 24th, 11:33 PM
  5. references
    By Carl Furst in forum PHP Development
    Replies: 2
    Last Post: July 22nd, 04:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139