Debugging message routine with line directives

This example Perl script shows how to print a message with a line directive in Perl using caller.

use warnings;
use strict;

sub debugmsg
    my ($msg) = @_;
    my (undef, $file, $line) = caller (0);
    printf ("%s:%d: ", $file, $line);
    if ($msg) {
        print $msg;
    print "\n";

sub othersub
    debugmsg ("Here I am.");

debugmsg ("Nothing special to say");
othersub ();
debugmsg ();


The output of this program looks like this: Nothing special to say Here I am. 

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer