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

I'm trying to port some scripts from an old server to a new one and it appears that the functionality of the CGI::script_name() function has changed when running it from the command line.

When running it from a browser session, it works correctly, but I have a number of scripts that run from cron as command-line programs that breaks because CGI::script_name() no longer returns a from when run from the command line.

Is this intentional or a bug, and is there a way to restore the old functionality?

Running the below script from the command line:
#!/usr/bin/perl use CGI; print $CGI::VERSION; print "\n"; print CGI::script_name(); print "\n";
on the old working server I get:
3.05 /var/www/vhosts/domainname.com/cgi-bin/testcgi.pl
but on the new version I get:
3.59 [blank line]
This is breaking bunches of scripts. Is there a way to enable the old functionality? Thanks!

Replies are listed 'Best First'.
Re: CGI::script_name() changed or bug?
by GrandFather (Saint) on Apr 29, 2012 at 04:49 UTC

    CGI::script_name expects the CGI environment variable SCRIPT_NAME to contain the name of the notional script being executed. Running in the context of a web server such as Apache the variable is set by the calling code. However if you are running the script in the context of a cron job then very likely SCRIPT_NAME is not being set. You could fix this by having the cron job run a bash script that sets SCRIPT_NAME before calling your Perl script.

    True laziness is hard work
Re: CGI::script_name() changed or bug?
by NetWallah (Canon) on Apr 29, 2012 at 15:38 UTC
    To complete the story on GrandFather's explanation, the older 3.05 version of CGI.pm provided the script name this way:
    sub script_name { return $ENV{'SCRIPT_NAME'} if defined($ENV{'SCRIPT_NAME'}); # These are for debugging return "/$0" unless $0=~/^\//; return $0; }
    The 'debugging' code is not present in the 3.59 version - it relies exclusively on the environment variable.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL