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

I am learning Modern Perl. The perl file chapter5_2.pl have code :
#!/bin/perl use v5.14.2; use strict; use Carp; package main; main(); sub main { show_call_information(); } sub show_call_information { my ($package, $file, $line,$func) = caller(0); say "Called $func from $package in $file:$line"; } say "---------use Carp------------------"; say add_numbers(1,2,3); sub add_numbers { croak 'Expected two numbers, received:' . @_ unless @_ == 2; my ($one, $two) = @_; return $one + $two; }
I run as
perl chapter5_2.pl
get the following output:
Called main::show_call_information from main in chapter5_2.pl:13 ---------use Carp------------------ Expected two numbers, received:3 at chapter5_2.pl line 28. main::add_numbers(1, 2, 3) called at chapter5_2.pl line 24

I can't know why the main() function is called automatically? What's the mechanism?

How can I get result from formal document such as perldoc?

Thanks!

Replies are listed 'Best First'.
Re: why main() call automatically?
by kcott (Archbishop) on Jan 17, 2014 at 01:58 UTC

    G'day Thai Heng,

    I suspect you may be confusing the main in main::show_call_information (which is a package and documented in package and perlmod: Packages) with main in main() and sub main {...} (which is a subroutine and documented in perlintro: Basic syntax overview and perlintro: Writing subroutines). [Note: many of the pages I've linked to contain links to further, relevant information.]

    main::show_call_information indicates the show_call_information subroutine in the main package; it is not providing information that show_call_information() was called from main(). If that still seems confusing, try changing the name of the main subroutine to something unambiguous: the new output should probably prove enlightening.

    You may also find these fairly recent threads to be of interest: "creating a perl entry-point" and "Main routines, unit tests, and sugar".

    -- Ken

Re: why main() call automatically?
by Anonymous Monk on Jan 16, 2014 at 23:56 UTC

    I can't know why the main() function is called automatically? What's the mechanism?

    It isn't called automatically. The line main(); calls the main function

    How can I get result from formal document such as perldoc?

    What do you mean by that, what result?

    ?? my $std_output = qx{perldoc perldoc };

Re: why main() call automatically?
by bulk88 (Priest) on Jan 17, 2014 at 05:13 UTC
    First learn Perl, then learn Modern Perl. Modern Perl is a coding policy, not a language.

      How is that helpful?

      bulk88 says: First learn Perl, then learn Modern Perl. Modern Perl is a coding policy, not a language.

      Modern Perl is the book the code sample originally came from

Re: why main() call automatically?
by Thai Heng (Beadle) on Jan 21, 2014 at 00:34 UTC
    Now I get it.  main::add_numbers(1, 2, 3) called at chapter5_2.pl line 24 This is not show_call_information() function output, So there is no automatically called.