in reply to Determine if sub is called from control block.

With caller, you can find out about the line this sub was called. If you write a __DATA__ at the end of the script, you can use a seek(DATA, 0, 0) to jump at the beginning of the script or seek(DATA, 0, $offset) to jump to a certain line. Then just search for the surrounding { } and the logical statements.

As another solution that might also work with several files, you can find the Filename of the code which called your sub with caller, open the file and do about the same as above. The latter might be cleaner, but in the moment I can't find a better solution...

  • Comment on Re: Determine if sub is called from control block.

Replies are listed 'Best First'.
Re: Re: Determine if sub is called from control block.
by strat (Canon) on Feb 27, 2002 at 12:45 UTC
    You can do so even without using DATA, which only seems to work for a single file or the like... just use an open on the Filename caller gave you, jump into the right line caller gave you and scan around a bit :-)

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

      Interesting idea. Thanks!

      -Lee

      "To be civilized is to deny one's nature."
Re: Re: Determine if sub is called from control block.
by shotgunefx (Parson) on Feb 27, 2002 at 11:35 UTC
    Pretty Clever! Never played much with DATA. Unfortunately, it won't work as I want this as a module. I think trying to do that on the fly will cause many problems. (Evals, etc)

    I actually just finished a version using Filter::Simple (Changes the calls if the line doesn't contain a for/foreach/while that works but I'd like to avoid requiring it if I can.

    Thanks!

    -Lee

    "To be civilized is to deny one's nature."