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

Hello, I'm relatively new to perl but not programming, and I'm learning this wonderful language now :) but I'm trying to do something, and I'm a little stuck. I have the O'Reilly's stuff but maybe I'm just not searching it right.

Anyways, here's the question.

In C/C++ there's the ARGC and ARGV variables, which I understand that @ARGV is like ARGV in C. However, ARGV[0] is the first argument given in Perl, whereas it's the executable name in C. Is there a way to get the name of the perl script I'm running, so I can generate some sort of message instructing the user on usage? For example...
#!/usr/local/bin/perl use strict; if (@ARGV != 2) { print "Usage: (program name) arg1 arg2\n"; exit; }
I tried ARGV[0] by instinct from C and, of course, got the first argument I called the function with - blew my mind till I did for (@ARGV) {print "$_\n}; and saw that it didn't have the script name.

Any help? Thanks.

Brian, relatively new perlmonk

Replies are listed 'Best First'.
(tye)Re: Anything comparable to argv0 from C in Perl?
by tye (Sage) on Apr 11, 2001 at 00:06 UTC

    Type the command perldoc perltrap and read. There is a section for people moving from C to Perl.

            - tye (but my friends call me "Tye")
Re: Anything comparable to argv0 from C in Perl?
by japhy (Canon) on Apr 11, 2001 at 00:01 UTC
Re: Anything comparable to argv0 from C in Perl?
by satchboost (Scribe) on Apr 11, 2001 at 00:00 UTC
    Try $0. Have you read the Camel book? I'd reccommend that, as well as Advanced Programming in Perl. Both are available from O'Reilly.
Re: Anything comparable to argv0 from C in Perl?
by jink (Scribe) on Apr 11, 2001 at 22:10 UTC
    I usually use this to get only the last part of your script name:
    #!/usr/bin/perl -w use strict; my @a=split(/\//,$0); my $myname=pop @a; if ($#ARGV != -1) { die "Usage: $myname parameters\n"; }
      Here's a one-liner to extract the base name from the full path without a temporary array:
      my ($basename) = $0 =~ m/.*\/(.*)/;
      You can also use split() like this:
      my $basename = (split /\//, $0)[-1];

      -Matt