Professional Web Applications Themes

Testing whether a subroutine exists (symbolic ref) - PERL Miscellaneous

Arvin Portlock wrote: > I'm having a look at some old code which uses symbolic subroutine > references (I know, A Very Bad Thing). I simply want to test whether > the named subroutine actually exists in the code. This simple pro- > gram: > > my $sub_name = "mySub"; > my $sub_ref = sub { &{"$sub_name"} }; Here you create an anonymous subroutine and assign it (a reference to it) to $sub_ref. The subroutine itself calls, via symref, a subroutine, but that does not have any influence on what $sub_ref contains. > if ($sub_ref) { > print "It exists\n"; ...

  1. #1

    Default Re: Testing whether a subroutine exists (symbolic ref)

    Arvin Portlock wrote:
    > I'm having a look at some old code which uses symbolic subroutine
    > references (I know, A Very Bad Thing). I simply want to test whether
    > the named subroutine actually exists in the code. This simple pro-
    > gram:
    >
    > my $sub_name = "mySub";
    > my $sub_ref = sub { &{"$sub_name"} };
    Here you create an anonymous subroutine and assign it (a reference to it) to
    $sub_ref. The subroutine itself calls, via symref, a subroutine, but that does
    not have any influence on what $sub_ref contains.
    > if ($sub_ref) {
    > print "It exists\n";
    > } else {
    > print "It doesn't exist\n";
    > }
    >
    > always prints "It exists!" even though there's no subroutine
    > called mySub anywhere in the program.
    of course. $sub_ref contains a subref and is absolutely unaware what code it
    points to.

    try

    if( defined &{ $sub_name } )

    that's ok with strict refs, too.

    --
    sub{use strict;local$=sub{select($,,$,,$,,pop)};unshift_ ,(45)x 24,split q=8==>
    55.52.56.49.49.55.56.49.49.53;do{print map(chr,_[0..(_/2-1)]),"\r";$->(1/6)=>
    push_=>shift}for_,++$|}->(map{$_+=$_%2?-1:1}map ord,split//,'u!`onuids!Qdsm!'.
    'i`bjds') #my email-address is reversed! <http://fruiture.de>

    Richard Voss Guest

  2. #2

    Default Re: Testing whether a subroutine exists (symbolic ref)

    >>>>> "AP" == Arvin Portlock <apollock11hotmail.com> writes:

    AP> I'm having a look at some old code which uses symbolic subroutine
    AP> references (I know, A Very Bad Thing). I simply want to test whether
    AP> the named subroutine actually exists in the code. This simple pro-
    AP> gram:

    AP> my $sub_name = "mySub";
    AP> my $sub_ref = sub { &{"$sub_name"} };

    you just created a new anon sub.

    AP> if ($sub_ref) {
    AP> print "It exists\n";
    AP> } else {
    AP> print "It doesn't exist\n";
    AP> }

    since $sub_ref always has a fresh code ref in it, it is true.

    AP> always prints "It exists!" even though there's no subroutine
    AP> called mySub anywhere in the program. Do I have to mess around
    AP> with Scary Things, like the symbol table?

    you can use the {CODE} thingy to get the ref from the typeglob. that
    will tell you if the sub exists or not.

    perl -e 'sub r{1} ; $f = *{'r'}{CODE} ; print "$f\n"'
    CODE(0x10442c)

    perl -e ' $f = *{'r'}{CODE} ; print "$f\n"'

    prints nothing

    but as you know symrefs are evil, just use a dispatch table. you can
    predefine the supported subs with code refs and not worry about odd
    things like {CODE}.

    uri

    --
    Uri Guttman ------ [email]uristemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
    Uri Guttman Guest

  3. #3

    Default Re: Testing whether a subroutine exists (symbolic ref)

    Richard Voss wrote:
    > Arvin Portlock wrote:
    >
    > > I'm having a look at some old code which uses symbolic subroutine
    > > references (I know, A Very Bad Thing). I simply want to test whether
    > > the named subroutine actually exists in the code. This simple pro-
    > > gram:
    > >
    > > my $sub_name = "mySub";
    > > my $sub_ref = sub { &{"$sub_name"} };
    >
    >
    > Here you create an anonymous subroutine and assign it (a reference to
    > it) to $sub_ref. The subroutine itself calls, via symref, a subroutine,
    > but that does not have any influence on what $sub_ref contains.
    >
    > > if ($sub_ref) {
    > > print "It exists\n";
    > > } else {
    > > print "It doesn't exist\n";
    > > }
    > >
    > > always prints "It exists!" even though there's no subroutine
    > > called mySub anywhere in the program.
    >
    >
    > of course. $sub_ref contains a subref and is absolutely unaware what
    > code it points to.
    >
    > try
    >
    > if( defined &{ $sub_name } )
    >
    > that's ok with strict refs, too.
    That works! Thank you. In fact it's a FAQ. I kept trying
    if (defined (\&$sub_name), don't know why I wanted to put
    the backsplash in there. Thanks for taking the time to set
    me straight.


    Arvin Portlock Guest

  4. #4

    Default Re: Testing whether a subroutine exists (symbolic ref)

    Arvin Portlock wrote:
    > I simply want to test whether
    > the named subroutine actually exists in the code.
    print "$sub exists" if main->can( $sub );

    hth, tina
    --
    [url]http://www.tinita.de/[/url] \ enter__| |__the___ _ _ ___
    [url]http://Movies.tinita.de/[/url] \ / _` / _ \/ _ \ '_(_-< of
    [url]http://www.perlquotes.de/[/url] \ \ _,_\ __/\ __/_| /__/ perception
    - my mail address expires end of august 2003 -
    Tina Mueller Guest

  5. #5

    Default Re: Testing whether a subroutine exists (symbolic ref)

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Tina Mueller <usenetexpires082003.tinita.de> wrote in news:bhqlv6$25j9g$1
    ID-24002.news.uni-berlin.de:
    > Arvin Portlock wrote:
    >> I simply want to test whether
    >> the named subroutine actually exists in the code.
    >
    > print "$sub exists" if main->can( $sub );
    Oh, that is *bizarre*!

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP0GC6WPeouIeTNHoEQJCPgCgrJbbIFKToClS/yGoAQ+DMHCPJo4AoIp3
    8s1glI1o9qE71UL+pkNPoOUN
    =mn1c
    -----END PGP SIGNATURE-----
    Eric J. Roode Guest

  6. #6

    Default Re: Testing whether a subroutine exists (symbolic ref)

    Tina Mueller <usenetexpires082003.tinita.de> wrote in comp.lang.perl.misc:
    > Eric J. Roode <REMOVEsdnCAPScomcast.net> wrote:
    > > Tina Mueller <usenetexpires082003.tinita.de> wrote in news:bhqlv6$25j9g$1
    > > ID-24002.news.uni-berlin.de:
    > > > print "$sub exists" if main->can( $sub );
    >
    > > Oh, that is *bizarre*!
    >
    > additionally, it should be main::->can( $sub ).
    > it generates a warning otherwise, i wonder why
    > i didn't see this. oh, wait, 5.6.1 generates a
    > warning, 5.8.0 does not. oh well =)
    Also, it answers a slightly different question. If inheritance comes into
    play, can() may return true even if main::sub isn't defined.

    Anno
    Anno Siegel Guest

Similar Threads

  1. Testing if a value exists.
    By Andy Levy in forum PHP Development
    Replies: 3
    Last Post: October 30th, 11:03 AM
  2. method name exists, property value exists, calling method fails
    By Phil Powell in forum PHP Development
    Replies: 0
    Last Post: October 27th, 07:34 PM
  3. Testing if a mysql table exists
    By sam in forum PHP Development
    Replies: 1
    Last Post: October 16th, 11:10 PM
  4. How to rm a symbolic link?
    By Bing-Feng Wang in forum Sun Solaris
    Replies: 4
    Last Post: July 30th, 02:48 PM
  5. too many symbolic links
    By Enrique Arredondo in forum SCO
    Replies: 0
    Last Post: July 3rd, 10:07 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