Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Difference between $0 and __FILE__

by solegaonkar (Beadle)
on Sep 26, 2012 at 04:05 UTC ( [id://995692]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, Often wondered what is the difference between the two special values $0 and __FILE__ in a Perl script. But always brushed it off saying "Perl has many ways to do anything". But, today, I saw some code containing:
unless ($0 eq __FILE__) .....
That sets me thinking, what exactly is the difference between the two. Can they be different? When?

Replies are listed 'Best First'.
Re: Difference between $0 and __FILE__
by BrowserUk (Patriarch) on Sep 26, 2012 at 04:28 UTC

    Within a module (say a.pm) used from a program (say b.pl), __FILE__ will be a.pm but $0 will be b.pl.

    The test you posted checks whether the file that contains it is being run as a 'main' program, or is being used or required, or done or eval'd.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: Difference between $0 and __FILE__
by pobocks (Chaplain) on Sep 26, 2012 at 04:45 UTC

    __FILE__ is the file name that the __FILE__ token is in, and $0 is a variable containing the name of the program, as given to the shell.

    They can most certainly be different - one case that comes to mind is a __FILE__ token in a library called by your program, but there are many others, particularly since you can assign to $0 (with varying effects on different OS), but you can't meaningfully assign to __FILE__.

    Example code:

    File 1 - firstfile

    #!/usr/bin/perl -w use strict; print $0 . "\n"; print __FILE__ . "\n"; do 'filetwo';

    File 2 - filetwo

    #!/usr/bin/perl -w use strict; print $0 . "\n"; print __FILE__ . "\n";

    It's simplistic, but should show the basic concept.

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
      That explains it! Thank you!!
Re: Difference between $0 and __FILE__
by BitDreamer (Sexton) on Jul 20, 2017 at 19:04 UTC
    This is an old question, but there's more of an answer. If a module is found in the current directory by matching '.' in @INC, then the value of __FILE__ is just the filename of the module with no path information. This is important if something does a chdir, then the module is no longer in "the current directory".

      Good point; part of why I eschew chdir style code. A little more on your call out–

      package AB; # File: /Users/moo/AB.pm use strict; use Path::Tiny; sub ohai { __FILE__ } sub ohai_der { path(__FILE__)->absolute } 1;
      moo@cow[54]~>perl -MAB -le 'print AB->ohai; print AB->ohai_der' AB.pm /Users/moo/AB.pm
      moo@cow[55]~>perl -MAB -le 'chdir "/tmp"; print AB->ohai; print AB->oh +ai_der;' AB.pm /private/tmp/AB.pm

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://995692]
Approved by lidden
Front-paged by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found