Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: perl not going to error path

by stevieb (Canon)
on Jan 23, 2016 at 17:47 UTC ( #1153473=note: print w/replies, xml ) Need Help??


in reply to perl not going to error path

Did you try the answer you got over at StackOverflow

When cross-posting, please say so in your post, and link to it (on both sites). This prevents people from duplicating work.

To answer your question, seek requires a file handle as its first argument. Note how the seek call has $fh (a file handle) as its first argument, and not the file name (copy/pasted from SO, comments mine):

use strict; use warnings; my $offset = 1; # create a file handle from the file name open my $fh, '<', 'DCMFReport.txt' or die $!; # call seek, with the new file handle we just created seek $fh, $offset, 0 or die "Report seek error, offset:$offset - $!";

Also, please put your code into <code></code> tags. As you can see in your post, it's not very readable as is. Review Markup in the Monastery.

Replies are listed 'Best First'.
Re^2: perl not going to error path
by gupr1980 (Acolyte) on Jan 23, 2016 at 17:58 UTC

    Thanks. Will make sure I provide a link in future. As for trying the script, i did. in the open statement, i pass the file name as a variable

    use strict; use warnings; my $offset = 1; # create a file handle from the file name open my $fh, '<', $filename or die $!; # call seek, with the new file handle we just created seek $fh, $offset, 0 or die "Report seek error, offset:$offset - $!";

    Now when i pass a non numeric offset, i get an error that says argument non numeric but i dont get the die error message and the exit value is 0. If i pass a numeric offset things work ok. If i put the file handle inside quotes like the below

    seek "$fh", $offset, 0 or die "Report seek error, offset:$offset - $!";

    now when i pass a non numeric offset, the script dies and throws the die error message and the exit value is non zero. this is what i want but i don't understand why i need the quotes.

      "Now when i pass a non numeric offset, i get an error that says argument non numeric but i dont get the die error message"

      That's because seek is effectively calling die() before you get a chance to. It's saying "pass in an integer or I'll proceed no further. This is an error, and I'm killing all processes".

      It's hard to get seek to fail when your argument is an integer, because (at least on Unix), it allows you to seek past the end of the file.

      This sounds like an XY Problem. Why don't you tell us what it is you're trying to do? seek() as you've noticed, handles its own errors.

      UPDATE:

      "now when i pass a non numeric offset, the script dies and throws the die error message and the exit value is non zero. this is what i want but i don't understand why i need the quotes."

      Well, you didn't include the error message from $!, but I'll bet it's something along these lines: "Bad file descriptor...". You've hit your die, but it's because quoted "$fh" is no longer a valid file handle anymore. It's pretty much saying that it can't open the file.

      Again, tell us what you're really trying to do in the bigger picture.

      When cross-posting, please say so in your post, and link to it (on both sites).
      Thanks. Will make sure I provide a link in future.

      Good news! Because you posted your OP as a registered user, you can go back and add the SO link right now! Please see How do I change/delete my post? for etiquette and protocol regarding this. You can also add code tags at the same time — a double win!.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-05-18 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?