FAQ
I want to be able to return a true or false value from a function in a
module and populate the $! variable with the specific errors. Is this
possible? Is there documentation on how to do this? I can find docs on how
to use $! but not how to set it. Thanks for any help
-Ken


----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.

Search Discussions

  • Jenda Krynicky at Dec 16, 2003 at 11:14 pm
    From: Ken Lehman <ken_lehman@summithq.com>
    I want to be able to return a true or false value from a function in a
    module and populate the $! variable with the specific errors. Is this
    possible? Is there documentation on how to do this? I can find docs on
    how to use $! but not how to set it. Thanks for any help -Ken
    You can set $! ... but not to a specific error.

    Try
    foreach (0..30) {
    $! = $_;
    print "$!\n";
    }
    and
    $! = "Hello world";
    print "$!\n";

    As you can see you can assign the error numbers, but not the
    messages, those are fixed.

    You need to use another variable to hold the error message.

    You may also want to die("with the message") and wrap the code in an
    eval{} block.

    See
    perldoc -f eval

    Jenda
    ===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery
  • David at Dec 16, 2003 at 11:56 pm

    Ken Lehman wrote:

    I want to be able to return a true or false value from a function in a
    module and populate the $! variable with the specific errors. Is this
    possible? Is there documentation on how to do this? I can find docs on how
    to use $! but not how to set it. Thanks for any help
    $! is tied to errno which is basically (usually) an interger set by the
    system calls in case of an error. it's defined by the ISO C standard. you
    can't set errno to an arbitrary number or string and expect the it to work.
    check 'man errno' for a full discussion. you can check your system's errno.h
    to see values are defined for errno. for example, in my linux box:

    [panda]# more /usr/include/asm/errno.h
    ...
    #define ENOTBLK 15 /* Block device required */
    #define EBUSY 16 /* Device or resource busy */
    #define EEXIST 17 /* File exists */
    ...

    you can then assign $! like:

    [panda]# perl -MErrno=:POSIX -le '$!=ENOTDIR; print $!+0,": ",$!'
    20: Not a directory

    mess with errno only when you really need to!

    david
    --
    sub'_{print"@_ ";* \ = * __ ,\ & \}
    sub'__{print"@_ ";* \ = * ___ ,\ & \}
    sub'___{print"@_ ";* \ = * ____ ,\ & \}
    sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
  • John W. Krahn at Dec 17, 2003 at 12:30 am

    Ken Lehman wrote:

    I want to be able to return a true or false value from a function in a
    module and populate the $! variable with the specific errors. Is this
    possible? Is there documentation on how to do this? I can find docs on how
    to use $! but not how to set it. Thanks for any help
    $! is a special variable that is a number in numeric context and a
    string in string context. You can assign a number to $! and it will
    display the corresponding system error message but you cannot assign a
    string to it. $! is set by perl whenever a system call sets errno. $!
    is described in the perlvar.pod document.

    perldoc perlvar

    So, you CAN set it if you want to return one of the pre-defined error
    messages but it can also be changed by perl on any system call so it
    might not retain the value that you set.


    John
    --
    use Perl;
    program
    fulfillment
  • Dan Muey at Dec 29, 2003 at 6:56 pm

    I want to be able to return a true or false value from a
    function in a module and populate the $! variable with the
    specific errors. Is this possible? Is there documentation on
    how to do this? I can find docs on how to use $! but not how
    to set it. Thanks for any help -Ken
    Did anyone ever reply to this post?
    I couldn't find it in the archives and I thinnk it's a pretty good question.

    For instance would it be 'safe' and 'proper', and 'ok', etc... to do somethign like:

    sub whatever {
    my $foo = shift;
    undef $!;
    if($foo eq 'bar') { return 1; }
    else { $! = "Foo must equal bar";return 0; }
    }

    TIA

    Dan
  • Randal L. Schwartz at Dec 29, 2003 at 7:03 pm

    "Dan" == Dan Muey writes:
    I want to be able to return a true or false value from a
    function in a module and populate the $! variable with the
    specific errors. Is this possible? Is there documentation on
    how to do this? I can find docs on how to use $! but not how to
    set it. Thanks for any help -Ken
    Dan> Did anyone ever reply to this post? I couldn't find it in
    Dan> the archives and I thinnk it's a pretty good question.

    Dan> For instance would it be 'safe' and 'proper', and 'ok',
    Dan> etc... to do somethign like:

    Dan> sub whatever { my $foo = shift; undef $!; if($foo eq 'bar') {
    Dan> return 1; } else { $! = "Foo must equal bar";return 0; } }

    $! is for system-call-related errors that can be mapped to errno
    return values. I don't see "Foo must equal bar" fits that definition.
    Perhaps you want to look at the "Exception" module.

    Having said that, you can set $! to a numeric value, and it'll return
    that number in a numeric context, and the appropriate error text in a
    string context. Rememeber that $! is just a thin wrapper over errno
    though, so any system-call-related activity will change it.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
  • Daniel Staal at Dec 29, 2003 at 7:18 pm
    --As off Monday, December 29, 2003 12:54 PM -0600, Dan Muey is
    alleged to have said:
    I want to be able to return a true or false value from a
    function in a module and populate the $! variable with the
    specific errors. Is this possible? Is there documentation on
    how to do this? I can find docs on how to use $! but not how
    to set it. Thanks for any help -Ken
    Did anyone ever reply to this post?
    I couldn't find it in the archives and I thinnk it's a pretty good
    question.
    There were several responses, I can forward them to you if you really
    want them. Basically it boiled down to: "Read 'perldoc perlvar' for
    how and possible values.", and "Are you sure you want to do that?".
    For instance would it be 'safe' and 'proper', and 'ok', etc... to
    do somethign like:

    sub whatever {
    my $foo = shift;
    undef $!;
    if($foo eq 'bar') { return 1; }
    else { $! = "Foo must equal bar";return 0; }
    }
    "Foo must equal bar" is not a valid value for $!, most likely. :-)
    For what you are doing here I would really prefer you either 'die' or
    'warn', depending on what severity you think the problem is. (You
    could of course 'croak' or 'carp' instead, as applicable.)

    Daniel T. Staal

    ---------------------------------------------------------------
    This email copyright the author. Unless otherwise noted, you
    are expressly allowed to retransmit, quote, or otherwise use
    the contents for non-commercial purposes. This copyright will
    expire 5 years after the author's death, or in 30 years,
    whichever is longer, unless such a period is in excess of
    local copyright law.
    ---------------------------------------------------------------
  • R. Joseph Newton at Dec 31, 2003 at 5:58 am

    Dan Muey wrote:

    I want to be able to return a true or false value from a
    function in a module and populate the $! variable with the
    specific errors. Is this possible? Is there documentation on
    how to do this? I can find docs on how to use $! but not how
    to set it. Thanks for any help -Ken
    Did anyone ever reply to this post?
    Yes. There was actually an extensive thread.
    I couldn't find it in the archives and I thinnk it's a pretty good question.

    For instance would it be 'safe' and 'proper', and 'ok', etc... to do somethign like:

    sub whatever {
    my $foo = shift;
    undef $!;
    if($foo eq 'bar') { return 1; }
    else { $! = "Foo must equal bar";return 0; }
    }

    TIA

    Dan
    I agree that a close localization can be relatively harmless, maybe even useful. Still
    I would not recommend getting into the habit, since it could mask much more valuable
    information from the interpreter, and since there are tons of other ways to communicate
    custom errors.

    Joseph
  • Dan Muey at Dec 29, 2003 at 7:30 pm

    --As off Monday, December 29, 2003 12:54 PM -0600, Dan Muey is
    alleged to have said:
    I want to be able to return a true or false value from a
    function in
    a module and populate the $! variable with the specific errors. Is
    this possible? Is there documentation on how to do this? I
    can find
    docs on how to use $! but not how to set it. Thanks for
    any help -Ken
    Did anyone ever reply to this post?
    I couldn't find it in the archives and I thinnk it's a pretty good
    question.
    There were several responses, I can forward them to you if you really
    want them. Basically it boiled down to: "Read 'perldoc perlvar' for
    how and possible values.", and "Are you sure you want to do that?".
    For instance would it be 'safe' and 'proper', and 'ok',
    etc... to do
    somethign like:

    sub whatever {
    my $foo = shift;
    undef $!;
    if($foo eq 'bar') { return 1; }
    else { $! = "Foo must equal bar";return 0; }
    }
    "Foo must equal bar" is not a valid value for $!, most likely. :-)
    For what you are doing here I would really prefer you either 'die' or
    'warn', depending on what severity you think the problem is. (You
    could of course 'croak' or 'carp' instead, as applicable.)

    Daniel T. Staal
    I'll probably just do something else besides setting it. If I
    must have a variable then I can just have my own little $error
    variable to use fo rthat purpose. Thanks for the offer to forward them,
    I'll just look at the perldoc and go from there.

    Thanks

    Dan
  • Dan Muey at Dec 29, 2003 at 9:02 pm

    "Dan" == Dan Muey writes:
    I want to be able to return a true or false value from a
    function in a module and populate the $! variable with the
    specific errors. Is this possible? Is there documentation on
    how to do this? I can find docs on how to use $! but not how to
    set it. Thanks for any help -Ken
    Dan> Did anyone ever reply to this post? I couldn't find it in
    Dan> the archives and I thinnk it's a pretty good question.

    Dan> For instance would it be 'safe' and 'proper', and 'ok',
    Dan> etc... to do somethign like:

    Dan> sub whatever { my $foo = shift; undef $!; if($foo eq 'bar') {
    Dan> return 1; } else { $! = "Foo must equal bar";return 0; } }

    $! is for system-call-related errors that can be mapped to
    errno return values. I don't see "Foo must equal bar" fits
    that definition. Perhaps you want to look at the "Exception" module.

    Having said that, you can set $! to a numeric value, and
    it'll return that number in a numeric context, and the
    appropriate error text in a string context. Rememeber that
    $! is just a thin wrapper over errno though, so any
    system-call-related activity will change it.

    --
    Thanks for the info, very helpful!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedDec 16, '03 at 10:39p
activeDec 31, '03 at 5:58a
posts10
users8
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase