Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: At which line number did the print statement occure?

by Sartak (Hermit)
on Oct 13, 2006 at 17:32 UTC ( [id://578182] : note . print w/replies, xml ) Need Help??


in reply to Re^4: At which line number did the print statement occure?
in thread At which line number did the print statement occure?

The distinction between $0 and __FILE__ is handy. You can use it to implement unit tests for classes. I do this a lot in Ruby. For example, in classFoo.pm:

#/usr/bin/perl package Foo; sub new { # ... } # other stuff if (__FILE__ eq $0) { my $foo = new Foo; # test foo's methods, etc } 1;

Now you'll only get the unit tests if you run classFoo.pm directly, because otherwise, __FILE__ ne $0.

Of course, I must recommend using Test::More and friends instead. Just showing a possible application of the subtle difference between __FILE__ and $0.

Replies are listed 'Best First'.
Re^6: At which line number did the print statement occure?
by ikegami (Patriarch) on Oct 13, 2006 at 17:54 UTC
    caller is more reliable for this.
    #!/usr/bin/perl package Foo; sub new { # ... } # other stuff if (!caller()) { my $foo = new Foo; # test foo's methods, etc } 1;