Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://578182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-16 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found