trapping file i/o error

Discussion in 'Perl' started by toylet, Feb 19, 2004.

  1. toylet

    toylet Guest

    In most perl examples, it used this method to trap error:

    open(INFILE, $fname) or die "Unable to open $fname";
    process_file();
    close(INFILE)
    other_codes();

    Now that if I don't want to die after the open so as to run
    other_codes()? Could I test the value of file handle INFILE like what I
    did with C?

    fhandle=fopen(fname,"r")
    if (fhandle > 0) {
    process_file();
    fclose(fhandle)
    }
    else
    show_error()
    other_codes();

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 5:00pm up 2 days 18:54 load average: 1.00 1.00 1.00
    toylet, Feb 19, 2004
    #1
    1. Advertising

  2. toylet wrote:
    >
    > In most perl examples, it used this method to trap error:
    >
    > open(INFILE, $fname) or die "Unable to open $fname";
    > process_file();
    > close(INFILE)
    > other_codes();
    >
    > Now that if I don't want to die after the open so as to run
    > other_codes()? Could I test the value of file handle INFILE like what I
    > did with C?
    >
    > fhandle=fopen(fname,"r")
    > if (fhandle > 0) {
    > process_file();
    > fclose(fhandle)
    > }
    > else
    > show_error()
    > other_codes();
    >

    Try something like this:
    if ( -e $fname && -r $fname) {
    open(INFILE, $fname) or show_error();
    }
    else {
    die "Unable to open $fname";
    }

    The "-e" tests if the file exists and the "-r" tests if the file is
    readable. If you want to see if it is writable use "-w". By testing
    for the existence of the file and if you can read from or write to the
    file first you can handle those situations gracefully and not have to
    capture the error condition.

    There is nothing special about the "or die". The "or" is just a logical
    operator. Perl uses a short cut optimization of boolean statements.
    the open statement returns a 1 if it suceeds and undefined if it fails.
    So when the open statement succeeds it returns 1 and evaluating "1 or
    anything" will alwayse be true so it will not do the "anything" on the
    other hand of the open fails the returned undefined is treated as false
    so the second part of the or needs to be evaluated so it could be any
    statement or block of statement you want. Do not expect to get anything
    of use out of the fhandle.

    If you are going to continue writing scripts in perl I would suggest
    getting a good book on it. My preferences are either from O'Reilly or
    Wrox (if you can find them).

    --
    Thanks
    Charles LaCour
    Charles LaCour, Feb 20, 2004
    #2
    1. Advertising

  3. toylet

    toylet Guest


    > Try something like this:
    > if ( -e $fname && -r $fname) {
    > open(INFILE, $fname) or show_error();
    > }
    > else {
    > die "Unable to open $fname";
    > }
    > statement or block of statement you want. Do not expect to get anything
    > of use out of the fhandle.


    Too bad. I thought checking he file handle is the best appraoch. in
    fact, many languages do that, like SQLCONNECT() in Foxpro, fopen() in
    C/Clipper/Foxpro, ... It would be quite troublesome to work around that.

    > If you are going to continue writing scripts in perl I would suggest
    > getting a good book on it. My preferences are either from O'Reilly or
    > Wrox (if you can find them).


    There are many websites hosting Perl books online. I use google.com to
    find them. Thanks for the advice. What I really need is a job that
    demands the use of perl, which is rather scarse in my city. Most of them
    uses M$ tools.


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 4:08pm up 5:41 1 user 1.03 1.01
    toylet, Feb 20, 2004
    #3
  4. toylet

    Joe Smith Guest

    toylet wrote:

    > In most perl examples, it used this method to trap error:
    >
    > open(INFILE, $fname) or die "Unable to open $fname";
    > process_file();
    > close(INFILE)
    > other_codes();
    >
    > Now that if I don't want to die after the open so as to run
    > other_codes()? Could I test the value of file handle INFILE like what I
    > did with C?


    In perl, open() does not return a file handle but it does return
    a true/false value you can test.

    if (open(INFILE, $fname)) {
    process_file(INFILE);
    close(INFILE);
    } else {
    warn "Unable to read $fname: $!\n";
    }
    other_codes();

    Be sure to include $! in the error message; it has strerror(errno).
    -Joe
    Joe Smith, Feb 20, 2004
    #4
  5. toylet

    toylet Guest

    > In perl, open() does not return a file handle but it does return
    > a true/false value you can test.
    > if (open(INFILE, $fname)) {
    > Be sure to include $! in the error message; it has strerror(errno).
    > -Joe


    that's what I should be going after. thanks.

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 7:46pm up 9:19 1 user 1.00 0.94
    toylet, Feb 20, 2004
    #5
  6. toylet

    toylet Guest

    > Be sure to include $! in the error message; it has strerror(errno).

    "$!" is a text message. can I get the errorno?
    is it "$?" as in bash?

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 7:48pm up 9:21 1 user 1.00 0.94
    toylet, Feb 20, 2004
    #6
  7. toylet

    Joe Smith Guest

    toylet wrote:

    >> Be sure to include $! in the error message; it has strerror(errno).

    >
    >
    > "$!" is a text message. can I get the errorno?
    > is it "$?" as in bash?


    $! = 28; # ENOSPC = 'No space left on device'
    print "As a string, the last error was '$!'\n";
    print "As a number, errno was ", $!+0, "\n";

    That is, $! is magic. See also 'perldoc perlvar'.
    -Joe
    Joe Smith, Feb 21, 2004
    #7
  8. toylet

    toylet Guest

    Thank you. Seems that perl requires the programms the know about context.

    > print "As a string, the last error was '$!'\n";
    > print "As a number, errno was ", $!+0, "\n";


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 3:28pm up 15:48 1 user 1.02 1.00
    toylet, Feb 21, 2004
    #8
  9. toylet

    bob Guest

    toylet wrote:
    > Thank you. Seems that perl requires the programms the know about context.
    >
    >> print "As a string, the last error was '$!'\n";
    >> print "As a number, errno was ", $!+0, "\n";

    >
    >


    Yes. but once you *do* know about it, it can be very convenient.
    bob, Feb 22, 2004
    #9
  10. toylet

    toylet Guest

    hmm... how do you force a variable into a certain context (could I also
    call it "type casting")?

    for integer, $i+0 or (int)$i.
    for string, $i+""? or is it (string)$i?
    for array
    for hash

    > Yes. but once you *do* know about it, it can be very convenient.


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 1:10pm up 2:19 1 user 1.41 1.33
    toylet, Feb 22, 2004
    #10
  11. toylet

    Ben Morrow Guest

    toylet <toylet_at_mail.hongkong.com> wrote:
    > hmm... how do you force a variable into a certain context (could I also
    > call it "type casting")?


    You almost never need to. About the time string/number matters is with
    magic values like $!; other than that, scalar context can be forced with
    scalar() or unary + and list context with parentheses ().

    > for integer, $i+0 or (int)$i.


    yes no, though int($i) will give you int rather than
    float

    > for string, $i+""? or is it (string)$i?


    $i.'' or "$i" no

    > for array
    > for hash


    eh what? Please explain what you expect, e.g., (hash)$i to achieve?

    Ben

    --
    And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
    * *
    Ben Morrow, Feb 22, 2004
    #11
  12. toylet

    toylet Guest

    >> for array
    >> for hash

    >
    > eh what? Please explain what you expect, e.g., (hash)$i to achieve?


    Just asking for a general method of forcing context.

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 3:24pm up 4:33 1 user 1.79 1.45
    toylet, Feb 22, 2004
    #12
  13. toylet

    Anno Siegel Guest

    Ben Morrow <> wrote in comp.lang.perl.misc:
    >
    > toylet <toylet_at_mail.hongkong.com> wrote:
    > > hmm... how do you force a variable into a certain context (could I also
    > > call it "type casting")?

    >
    > You almost never need to. About the time string/number matters is with
    > magic values like $!; other than that, scalar context can be forced with
    > scalar() or unary + and list context with parentheses ().


    Parentheses only provide list context on the left side of an assignment
    (anywhere else?). Watch this:

    sub wanta { print wantarray ? "array\n" : "scalar\n" }

    $x = wanta;
    ( $x) = wanta;
    $x = ( wanta);
    ( $x) = ( wanta);

    The parentheses on the right side don't seem to do anything.

    I can't meaningfully say much about the difference between unary +
    and scalar(), except that there is one. Vaguely, "+" can change parsing,
    scalar() can't.

    [...]

    > And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)


    Can't say I understand that sig of your's, but I like it :)

    Anno
    Anno Siegel, Feb 22, 2004
    #13
  14. toylet

    Anno Siegel Guest

    Ben Morrow <> wrote in comp.lang.perl.misc:
    >
    > toylet <toylet_at_mail.hongkong.com> wrote:
    > > hmm... how do you force a variable into a certain context (could I also
    > > call it "type casting")?

    >
    > You almost never need to. About the time string/number matters is with
    > magic values like $!; other than that, scalar context can be forced with
    > scalar() or unary + and list context with parentheses ().


    Parentheses only provide list context on the left side of an assignment
    (anywhere else?). Watch this:

    sub wanta { print wantarray ? "array\n" : "scalar\n" }

    $x = wanta;
    ( $x) = wanta;
    $x = ( wanta);
    ( $x) = ( wanta);

    The parentheses on the right side don't seem to do anything.

    I can't meaningfully say much about the difference between unary +
    and scalar(), except that there is one. Vaguely, "+" can change parsing,
    scalar() can't.

    [...]

    > And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)


    Can't say I understand that sig of your's, but I like it :)

    Anno
    Anno Siegel, Feb 22, 2004
    #14
  15. toylet

    Joe Smith Guest

    toylet wrote:

    >>> for array
    >>> for hash

    >>
    >>
    >> eh what? Please explain what you expect, e.g., (hash)$i to achieve?

    >
    > Just asking for a general method of forcing context.


    ($a,$b) = foo(); # List context
    @array = foo(); # List context
    %hash = foo(); # List context
    foo(); # Null context
    $var = foo(); # Scalar context
    $var = +foo(); # Numeric scalar context
    $var = foo().""; # String scalar context
    if (foo()) {}; # Boolean scalar context

    -Joe
    Joe Smith, Feb 23, 2004
    #15
  16. [ F'up set to clpm;
    comp.lang.perl doesn't exist ]

    Also sprach Joe Smith:

    > toylet wrote:


    >> Just asking for a general method of forcing context.

    >
    > ($a,$b) = foo(); # List context
    > @array = foo(); # List context
    > %hash = foo(); # List context
    > foo(); # Null context
    > $var = foo(); # Scalar context
    > $var = +foo(); # Numeric scalar context


    Nope. This does not force numeric context. This does:

    $var = foo() + 0;

    Or this:

    $var = foo() * 1;

    Any numeric operator in conjunction with the neutral element with
    respect to the used operator can be used to enforce numeric context.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Feb 23, 2004
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brett Robichaud

    Exceeding File Upload max size - trapping error

    Brett Robichaud, Apr 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,878
    bruce barker
    Apr 7, 2004
  2. Craig Wilson

    Trapping the 404 (resource not found) error

    Craig Wilson, Oct 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    469
    =?Utf-8?B?TGVvbiBXZWxpY2tp?=
    Oct 8, 2004
  3. VB Programmer

    Best standard for error trapping?

    VB Programmer, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    378
    VB Programmer
    Oct 25, 2004
  4. =?Utf-8?B?TWlrZQ==?=

    error trapping

    =?Utf-8?B?TWlrZQ==?=, Dec 14, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    589
    =?Utf-8?B?RGVhc3Vu?=
    Dec 14, 2004
  5. =?Utf-8?B?UGF1bA==?=

    Error Trapping and Error Line Number

    =?Utf-8?B?UGF1bA==?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    470
    Hans Kesting
    Oct 27, 2005
Loading...

Share This Page