Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

cgi code line message

by Murcia (Monk)
on Jun 06, 2005 at 07:22 UTC ( [id://463845]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Confreres,
In my cgi I have some break conditions printing a possible error in functions, e.g

open(FILE, "$file") || &error_message("file can not be opened")
. If an errors occurs, I want to know in which line of my code this happens, but to not know how to pass the line number to the error_message subroutine!?

Thanks in advance
Guido

20050606 Janitored by Corion: Removed PRE tag, put HTML formatting instead

Replies are listed 'Best First'.
Re: cgi code line message
by Zaxo (Archbishop) on Jun 06, 2005 at 07:29 UTC

    If you call warn or die without an explicit newline at the end of the message, you'll get file and line number in the text to STDERR.

    If you want your own error message function, you can pass it __FILE__ and __LINE__ so that its message gets to refer to the caller's location. Another way to do that is with the caller builtin.

    After Compline,
    Zaxo

Re: cgi code line message
by davido (Cardinal) on Jun 06, 2005 at 07:28 UTC

    Here is a solution that involves a minimal edit to your existing code:

    open( FILE, "$file" ) || error_message( "File can not be opened: Line __LINE__.\n" );

    But when debugging CGI scripts, CGI::Carp can be much more useful, and would enable you to use die while dumping fatal error messages to the browser.

    Update:
    Dumb mistake here: __LINE__ doesn't interpolate (nor should it). See my followup: Re^3: cgi code line message. Apologies!


    Dave

      sorry; but I tried your code and it did not work ... error_message( "File can not be opened: Line ". __LINE__. "\n" ); to be correct ... thanks a lot!

        My dumb mistake. Forgot that __LINE__ doesn't (and shouldn't) interpolate. Here ya go...

        error_message( "File cannot be opened: Line " . __LINE__ . "\n" );

        Looks like you already figured it out though. Hope you find it helpful!


        Dave

Re: cgi code line message
by brian_d_foy (Abbot) on Jun 06, 2005 at 08:41 UTC

    In the error_message() routine, use the caller() function to figure out who called you from where. See the perlfunc entry for examples.

    --
    brian d foy <brian@stonehenge.com>
Re: cgi code line message
by rupesh (Hermit) on Jun 06, 2005 at 10:35 UTC
    Just an exerpt from of $! perldoc perlvar.

    $! If used numerically, yields the current value of the C "errno"
    variable, or in other words, if a system or library call fails,
    it sets this variable. This means that the value of $! is
    meaningful only *immediately* after a failure:
    if (open(FH, $filename)) { # Here $! is meaningless. ... } else { # ONLY here is $! meaningful. ... # Already here $! might be meaningless. } # Since here we might have either success or failure, # here $! is meaningless.
    In the above *meaningless* stands for anything: zero, non-zero,
    "undef". A successful system or library call does not set the
    variable to zero.
    If used an a string, yields the corresponding system error
    string. You can assign a number to $! to set *errno* if, for
    instance, you want "$!" to return the string for error *n*, or
    you want to set the exit value for the die() operator.
    (Mnemonic: What just went bang?)


    Cheers,
    Rupesh.
Re: cgi code line message
by jpeg (Chaplain) on Jun 06, 2005 at 07:57 UTC
    Besides __LINE__, there's the special variable $.
    from perldoc perlvar:

    $. Current line number for the last filehandle accessed.

    Each filehandle in Perl counts the number of lines that have been read from it. (Depending on the value of $/, Perl's idea of what constitutes a line may not match yours.) </quote>

    Update: nevermind me, I'm an idiot. $. won't help in this case.
    /me wanders off...

    --
    jpg

Log In?
Username:
Password:

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

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

    No recent polls found