in reply to Get My Own Name

Ignore the people who come with $0. If people can have renamed the program, they could have given it an additional link as well, and no $0 is going to protect you against that. Besides, what if people rename the program after it was started?

Don't rely on the file, use the inode number instead. To get the inode number of your program, add __DATA__ to the end of it, and use:

my $inode_number = (stat DATA) [1]; # And not fileno!

Abigail

Replies are listed 'Best First'.
Re: Re: Get My Own Name
by blue_cowdawg (Monsignor) on Feb 04, 2004 at 16:50 UTC

    To expand on what Abigail-II points out here, once you have the inode number of your script, you can then test the inode numbers in a similar manner for each file you are operating on and test that they are not equal.

    Just thought I'd clarify that point...

    I like that solution!


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
Re: Re: Get My Own Name
by Fletch (Bishop) on Feb 04, 2004 at 17:56 UTC

    Of course then too you could be very unlucky and be asked to process a symlink to a file on a different filesystem which just happens to have the same inode number . . . :)

    $ ls -i ./foo /tmp/foo 1808983 ./foo@ 228688 /tmp/foo $ perl -le 'print( (stat "./foo")[1] )' 228688

    So the truly paranoid might use join("|",(stat DATA)[0,1]).

Re: Re: Get My Own Name
by NetWallah (Canon) on Feb 04, 2004 at 18:21 UTC
    The original query did not specify what OS they were on.

    inode is meaningless on win32, so this suggestion is not portable.

    Sorry - I do not have a better original suggestion - I would settle for parsing $0 on win32.

    "When you are faced with a dilemma, might as well make dilemmanade. "