[Melbourne-pm] $0 behaviour

Mathew Robertson mathew.blair.robertson at gmail.com
Thu Jul 19 20:17:49 PDT 2012


Thanks for the info Ben -> I browsed the Catalyst source via the cpan.org,
but couldn't find any calls to $0... (I dont really want to install every
Catalyst module.... just to grep the source...).

I know some other software that uses it, eg: MySQLRelicationClient.pl


So just as a follow up to this thread, here is the code that I execute (via
loading the "Lib.pm" module) for every script that I write:

package Lib;
....
use Cwd qw( cwd abs_path chdir );
use File::Basename;
use File::Spec;
our $BASENAME;
our $DIRNAME;
our $BASEPATH;
BEGIN {
  $BASENAME = basename($0);
  $BASEPATH = abs_path(dirname($0)) || "";
  $DIRNAME = $0 =~ /^\// ? dirname($0) : $ENV{PWD}."/".$0;
  $DIRNAME =~ s/\/?\w+\/\.\.\//\//g;
}
use lib $BASEPATH;
$0 = "$BASEPATH/$BASENAME".(@ARGV == 0 ? "" : " ".join(" ", at ARGV));
use Sys::Prctl;
Sys::Prctl::prctl_name($BASENAME);
...
1;


Ignoring the globals, this sets $0 to something useful for
top/ps/pidof/killall.

regards,
Mathew


On 18 July 2012 01:26, Ben Hare <benhare at gmail.com> wrote:

> Hey Mat and yes I'm still Perling..! :)
>
> I like what Catalyst is doing regarding this. Maybe have a look at
> that project if you haven't already.
>
> NOTE: Below examples all relate to FastCGI implementations of Catalyst.
>
> * Older versions made the script name show in top but not in ps ( ps
> would just show 'perl', top would show 'script.cgi' ).
> * The latest version does the reverse and will just show 'perl' in top
> but in ps:
>
> 'perl-fcgi-pm [Xtapes]'
>
> the [<app_name>] indicating each app's name as defined in Catalyst config.
>
> I like this approach ( as long as the processes are running as
> different users, you know what it is in top anyway ).
>
> Some more ideas ( It's 00:55 and I'm too lazy to look at *how* they're
> doing it sorry - exercise for reader :) ).
>
> Ben.
>
>
> On 16 July 2012 14:50, Mathew Robertson
> <mathew.blair.robertson at gmail.com> wrote:
> > Hi list,
> >
> > I having been having some quirky behaviour when assigning $0...
> > For me this turned out to be as a result from this:
> >
> > $0 = abs_path(dirname($0))."/".basename($0);
> >
> > ie: make the process name (as shown in top/ps, be the name of the script
> -
> > instead of the perl binary.
> > Which worked well when running "pidof whatever.pl", "ps auxww | grep
> > whatever.pl", but not so much for "killall whatever.pl".
> >
> > Some Googling has turned up the problem/cause:
> >
> http://blogs.perl.org/users/aevar_arnfjor_bjarmason/2010/03/what-happens-when-you-assign-to-0.html
> >
> > perl version is: v5.14.2
> >
> >
> > So I run the example shown -> which shows that the patch is applied.
>  Upon
> > further investigation, I think the implementation could be refined, ie:
> in
> > my code above, only the first 16 characters of string are chomped, thus
> > leaving the program title with part of the the basepath of the script.
> In my
> > case I worked around it using Sys::Prtcl->prctl_name($BASENAME),
> >
> >
> > The suggested change to this enhancement would be, if $0 points at a
> script,
> > then change $0 assignment so that Perl grok's the rightmost part of the
> > path. ie: leave $0 as-is, but modify this patch as so.
> >
> > Thoughts?
> > Mathew
> >
> > _______________________________________________
> > Melbourne-pm mailing list
> > Melbourne-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/melbourne-pm
>
>
>
> --
> Ben Hare
> Professional Web Development Services
> ABN: 22-48-55-71-887
> Phone: +61-415-607-197
> Web: http://www.benhare.com
> Email: ben at benhare.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20120720/39fab8d3/attachment.html>


More information about the Melbourne-pm mailing list