Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

does a subroutine exist? defined vs glob

by bulk88 (Priest)
on Jul 22, 2015 at 23:57 UTC ( [id://1135926]=perlquestion: print w/replies, xml ) Need Help??

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

use Config; use Devel::Peek 'Dump'; use Data::Dumper; print Dumper(!!defined &Config::non_bincompat_options); print Dumper(!!*Config::non_bincompat_options{CODE}); print Dumper(Config::non_bincompat_options());
$VAR1 = ''; $VAR1 = 1; $VAR1 = 'PERL_COPY_ON_WRITE'; $VAR2 = 'PERL_DISABLE_PMC'; $VAR3 = 'PERL_DONT_CREATE_GVSV'; $VAR4 = 'PERL_HASH_FUNC_ONE_AT_A_TIME_HARD'; $VAR5 = 'PERL_PRESERVE_IVUV'; $VAR6 = 'USE_LOCALE'; $VAR7 = 'USE_LOCALE_CTYPE'; $VAR8 = 'USE_PERL_ATOF';
The sub clearly exists in a social sense. Why is the sub undefined, yet is a subroutine, which is runnable? Are all subs undefined by their very nature since they aren't scalars? What is going on in the code sample?

Replies are listed 'Best First'.
Re: does a subroutine exist? defined vs glob
by BrowserUk (Patriarch) on Jul 23, 2015 at 00:13 UTC

    Running it under 5.10 where the subroutine doesn't exist I think suggests an explaination:

    C:\test>1135926.pl $VAR1 = ''; $VAR1 = ''; Goto undefined subroutine &Config::non_bincompat_options at C:/perl64/ +lib/Config_heavy.pl line 1290.

    Ie. It is autoloaded.

    Which is confirmed by running it with 5.18 and -d:Trace I get:

    >> [0] 1135926.pl : 7: pp( Config::non_bincompat +_options() ); >> [0] C:/perl5.18/perl/lib/Config.pm: 81: require 'Config_heavy +.pl'; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 5: use strict; ... >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 6: use warnings; ... >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 7: use vars '%Config'; ... >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 75: our $summary = <<'! +END!'; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 108: my $summary_expande +d; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 129: local *_ = \my $a; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 130: $_ = <<'!END!'; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1231: my $i = ord(8); ... >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1233: our $byteorder = jo +in('', unpack('aaaaaaaa', pack('Q', $i))); >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1234: s/(byteorder=)(['"] +).*?\2/$1$2$Config::byteorder$2/m; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1236: my $config_sh_len = + length $_; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1238: our $Config_SH_expa +nded = "\n$_" . << 'EOVIRTUAL'; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1244: eval { >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1246: require 'Confi +g_git.pl'; >> [0] C:/perl5.18/perl/lib/Config_git.pl: 5: $Config::Git_Data=<<' +ENDOFGIT'; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1247: $Config_SH_exp +anded .= $Config::Git_Data; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1248: 1; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1275: my $prevpos = 0; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1301: *DELETE = *CLEAR = +\*STORE; # Typeglob aliasing uses less space >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1342: 1; >> [0] C:/perl5.18/perl/lib/Config.pm: 82: goto \&launcher unles +s $Config::AUTOLOAD =~ /launcher$/; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1338: undef &AUTOLOAD +; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 1339: goto \&$Config: +:AUTOLOAD; >> [0] C:/perl5.18/perl/lib/Config_heavy.pl: 14: return split ' +', (Internals::V())[1];

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
      Yes. To amplify the point about autoloading, try this:
      use Config; print "defined before\n" if defined &Config::non_bincompat_options; Config::non_bincompat_options(); print "defined after\n" if defined &Config::non_bincompat_options;
      Calling the sub causes it to become defined through autoloading.
        ... and there's a bit of magic that I hadn't realized before, because you can do the same thing with a coderef.
        use Config; my $sub = *Config::non_bincompat_options{CODE}; print "defined before\n" if defined &$sub; $sub->(); print "defined after\n" if defined &$sub;
        Same result: not defined before, but defined after.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found