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

God bless you, oh Monks!

I am meditating on the following not so complex only at first glance piece of code:

(caller)[1] eq $0

Is this always true within single perl script file?

It is very interesting to see the implementation in Perl source code of the following things:

1) internal function caller()

2) variable $0

So can you please give a guidance where to look in Perl source for them?

Replies are listed 'Best First'.
Re: Where is implementation of $0 in Perl source code
by ikegami (Patriarch) on May 27, 2015 at 21:03 UTC

    The implementation of caller is found here.

    The code called after you store a value in $0 is found here.

    The code called before you fetch the value of $0 is a no-op.

Re: Where is implementation of $0 in Perl source code
by dave_the_m (Monsignor) on May 27, 2015 at 18:21 UTC
    Consider the following:
    $ cat /tmp/Foo.pm sub f { print $0, ",", (caller)[1], "\n"; } f(); 1; $ perl -I/tmp -MFoo -e1 -e,/tmp/Foo.pm $
    Reading from the $0 variable is implemented in the "case '0'" branch of the switch within the function Perl_magic_get() in mg.c.

    Caller is implemented by the pp_caller() function in pp_ctl.c

    Dave.

      Update: I just noticed that you specified within a single file. Ok:
      $ perl -le'$0="Foo"; sub f { print "$0,", (caller)[1] } f()' Foo,-e

      Dave.