trapping file i/o error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • toylet

    trapping file i/o error


    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(f name,"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
  • Charles LaCour

    #2
    Re: trapping file i/o error

    toylet wrote:[color=blue]
    >
    > 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(f name,"r")
    > if (fhandle > 0) {
    > process_file();
    > fclose(fhandle)
    > }
    > else
    > show_error()
    > other_codes();
    >[/color]
    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

    Comment

    • toylet

      #3
      Re: trapping file i/o error

      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > 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).[/color]

      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

      Comment

      • Joe Smith

        #4
        Re: trapping file i/o error

        toylet wrote:
        [color=blue]
        > 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?[/color]

        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(IN FILE);
        close(INFILE);
        } else {
        warn "Unable to read $fname: $!\n";
        }
        other_codes();

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

        Comment

        • toylet

          #5
          Re: trapping file i/o error

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

          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

          Comment

          • toylet

            #6
            Re: trapping file i/o error

            > 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

            Comment

            • Joe Smith

              #7
              Re: trapping file i/o error

              toylet wrote:
              [color=blue][color=green]
              >> Be sure to include $! in the error message; it has strerror(errno) .[/color]
              >
              >
              > "$!" is a text message. can I get the errorno?
              > is it "$?" as in bash?[/color]

              $! = 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

              Comment

              • toylet

                #8
                Re: trapping file i/o error

                Thank you. Seems that perl requires the programms the know about context.
                [color=blue]
                > print "As a string, the last error was '$!'\n";
                > print "As a number, errno was ", $!+0, "\n";[/color]

                --
                .~. 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

                Comment

                • bob

                  #9
                  Re: trapping file i/o error

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

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

                  Comment

                  • toylet

                    #10
                    Re: trapping file i/o error

                    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
                    [color=blue]
                    > Yes. but once you *do* know about it, it can be very convenient.[/color]

                    --
                    .~. 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

                    Comment

                    • Ben Morrow

                      #11
                      Re: trapping file i/o error


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

                      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 ().
                      [color=blue]
                      > for integer, $i+0 or (int)$i.[/color]

                      yes no, though int($i) will give you int rather than
                      float
                      [color=blue]
                      > for string, $i+""? or is it (string)$i?[/color]

                      $i.'' or "$i" no
                      [color=blue]
                      > for array
                      > for hash[/color]

                      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.me.u k *

                      Comment

                      • toylet

                        #12
                        Re: trapping file i/o error

                        >> for array[color=blue][color=green]
                        >> for hash[/color]
                        >
                        > eh what? Please explain what you expect, e.g., (hash)$i to achieve?[/color]

                        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

                        Comment

                        • Anno Siegel

                          #13
                          Re: trapping file i/o error

                          Ben Morrow <usenet@morrow. me.uk> wrote in comp.lang.perl. misc:[color=blue]
                          >
                          > toylet <toylet_at_mail .hongkong.com> wrote:[color=green]
                          > > hmm... how do you force a variable into a certain context (could I also
                          > > call it "type casting")?[/color]
                          >
                          > 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 ().[/color]

                          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.

                          [...]
                          [color=blue]
                          > And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)[/color]

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

                          Anno

                          Comment

                          • Anno Siegel

                            #14
                            Re: trapping file i/o error

                            Ben Morrow <usenet@morrow. me.uk> wrote in comp.lang.perl. misc:[color=blue]
                            >
                            > toylet <toylet_at_mail .hongkong.com> wrote:[color=green]
                            > > hmm... how do you force a variable into a certain context (could I also
                            > > call it "type casting")?[/color]
                            >
                            > 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 ().[/color]

                            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.

                            [...]
                            [color=blue]
                            > And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)[/color]

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

                            Anno

                            Comment

                            • Joe Smith

                              #15
                              Re: trapping file i/o error

                              toylet wrote:
                              [color=blue][color=green][color=darkred]
                              >>> for array
                              >>> for hash[/color]
                              >>
                              >>
                              >> eh what? Please explain what you expect, e.g., (hash)$i to achieve?[/color]
                              >
                              > Just asking for a general method of forcing context.[/color]

                              ($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

                              Comment

                              Working...