I am using a Linux system (Ubuntu 9.10).
It took me several hours of frustration to realise something about $0. This built-in variable is defined in
perlvar as "the name of the program being executed". Perlvar doesn't mention that its meaning can change, even inside the same program running under the same system, according to the way the program is run.
You have this code:
#!/usr/bin/perl -w
use UI::Dialog; # sudo apt-get install libui-dialog-perl
my $statement = "My name is $0...\n";
my $d1 = new UI::Dialog(title => 'I know my name');
$d1->msgbox(text => $statement);
print $statement;
exit;
You save it into a file named "knowthyself" in your Desktop.
You give the file executable permissions (chmod 755 knowthyname).
Then you run the program 3 times:
- The first time you open a terminal and type "perl knowthyself". You get this output: "My name is knowthyself..."
- Then you use the terminal and type "./knowthyself". You get this output: "My name is ./knowthyself..."
- Finally you go to your file manager (Nautilus) and click on the file. You are asked what to do: Do you want to run it in a terminal, do you want to show its contents, do you want to cancel the action, or do you want to just run it? Of course you choose this last action (run the program without opening a terminal). You get this output (in a window): My name is /home/user/Desktop/knowthyself...
Moral: even if you are running your program under one and the same system, even if it supports $0, you have to be very careful before relying on what $0 returns.