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

Following the tutorial at BUILDING AN APPLICATION FROM EXISTING APPERL everything works until this step:
Now, you may still wish for your application to be run, even if the executable is renamed. Add default_script to your config to set a fallback script:
  "default_script" : "/zip/bin/hello"

Where exactly does that line belong? Adding it to apperl-project.json causes the error "Failed to find APPERL_DEFAULT_SCRIPT" when doing "apperlm build" again:
{ "apperl_configs" : { "my_nobuild_config" : { "desc" : "description of this config", "base" : "nobuild-v0.1.0", "dest" : "perl.com", "zip_extra_files" : { "bin" : ["src/hello"] }, "default_script" : "/zip/bin/hello" } }, "defaultconfig" : "my_nobuild_config" }
I've tried adding it to the other two "logical" places in apperl-project.json and also in .apperl/o/user-project.json, without getting any errors, but it still does not work. By "work" I expect that after adding "default_script" and running "apperlm build" again that running the newly built ./perl.com would run that script, without having to type "./perl.com /zip/bin/hello" and that it will work if even the perl.com is renamed.

Replies are listed 'Best First'.
Re: APPerl tutorial problem: default_script
by Corion (Patriarch) on Feb 22, 2023 at 06:42 UTC

    Grepping for default_script points to line 1177 in Perl::Dist::APPerl, and that code suggests to me that the place of default_script you've chosen is right.

    I also interpret the documentation in the way that you say - the perl.com program should invoke /zip/bin/hello in case no program is given on the command line.

    Have you tried debugging it by giving it a non-existing script name as default_script? Or by adding debug messages to lib/Perl/Dist/APPerl.pm?

        Have you tried debugging it by giving it a non-existing script name as default_script?
      Thanks for the suggestion. It dies with the same error if the file referenced by default_script exists or not!
      Failed to find APPERL_DEFAULT_SCRIPT, is this an old APPerl binary? 
      at /path/to/Perl/Dist/APPerl.pm line 1185.
      
      There is an another possible issue (warning) in the build process after adding this line to apperl-project.json per the previous step in the tutorial:
      "zip_extra_files" : { "bin" : ["src/hello"] }
      
      These are the new output lines shown during the build process:
      cd /path/to/apperl/hello/.apperl/o/my_nobuild_config/tmp/zip
      zip -r /path/to/apperl/hello/.apperl/o/my_nobuild_config/tmp/perl.com lib bin
      	zip warning: name not matched: lib
      updating: bin/ (stored 0%)
        adding: bin/hello (deflated 1%)
      
      However running "/perl.com /zip/bin/hello" after the build does work as advertised. I'll try hacking at APPerl.pm next but it's about 1500+ lines of dense code...
Re: APPerl tutorial problem: default_script
by Anonymous Monk on Feb 24, 2023 at 17:19 UTC
    OP here, I'm guessing the documentation is more advanced than the code and none of the "other ways of invoking embedded scripts" have actually been implemented yet (or bugs exist).

    Commenting line 1185 in APPerl.pm lets the build happen but does not enable default_script:

    $sentinelpos != -1 or die "Failed to find APPERL_DEFAULT_SCRIPT, is th +is an old APPerl binary?";
    From the docs:

      APPERL_SCRIPTNAME - When the environment variable APPERL_SCRIPTNAME is set, APPerl attempts to load the basename of APPERL_SCRIPTNAME without file extension from /zip/bin or opens the perl interpreter like normal if it is perl.

    export APPERL_SCRIPTNAME=hello
    echo $APPERL_SCRIPTNAME
    hello
    
    DOES NOT WORK!

    From the docs:

      APPERL_DEFAULT_SCRIPT - If argv[0] doesn't yield a valid target either, if the APPERL_DEFAULT_SCRIPT field inside of the binary is set, APPerl will attempt to load that. This way is meant for APPerl application authors to protect against accidental rename messing up argv[0] script execution. "BUILDING AN APPLICATION FROM EXISTING APPERL" shows how to set it with "default_script", but you could also set/change it, by searching for APPERL_DEFAULT_SCRIPT in a hex editor and modifying it.

    APPERL_DEFAULT_SCRIPT is not found in the binary when using a hex editor!

    perl.com --assimilate does not help

    Looking at perl.com with a hex editor does reveal a change when setting default_script in apperl-project.json.

    Start of perl.com without default_script (minus weird chars):

    MZqFpD='
    ø@JT' <<'@'
    
    Start of perl.com with default_script:
    MZqFpD='
    ø/zip/bin/helloJT' <<'@'