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

Re^2: Ulimit being reached

by viffer (Beadle)
on Jan 14, 2015 at 03:30 UTC ( #1113174=note: print w/replies, xml ) Need Help??


in reply to Re: Ulimit being reached
in thread Ulimit being reached

The ulimit is set at system level - and has since been increased. I wanted to trap an error when perl tried to write to a file and exceeded the ulimit. The closing file handle worked ok.

Since then we have come up with the following to trap the error - although it has thrown up a strange error of not liking the "|| die" appearing on a separate line.

#!/usr/local/bin/perl use strict; use warnings FATAL => 'all'; open(OUT, '>', "splat") or die "Opening (write) 'splat' $!"; while (1) { my $line = time().; # THIS WORKS print (OUT $line) || die "writing 'splat' $!"; # But putting || die on another line does not. i.e: # print (OUT $line) # || die "writing 'splat' $!"; } close(OUT) or die "Closing 'splat' $!";
The error message produced is:

writing 'splat'

A file cannot be larger than the value set by ulimit. at u3.pl line 12.

Replies are listed 'Best First'.
Re^3: Ulimit being reached
by eyepopslikeamosquito (Bishop) on Jan 14, 2015 at 03:38 UTC

    You should always use low precedence or (in preference to ||) with the "or die" idiom. Also, you should normally avoid parens when calling print, so this is the recommended way to write it:

    print OUT $line or die "writing 'splat' $!";
    For a working example, see my reply below.

    Update: further notice that lexical file handles (e.g. $fh in my reply below) are preferred to the old global file handles (e.g. OUT in your example code).

Re^3: Ulimit being reached
by eyepopslikeamosquito (Bishop) on Jan 14, 2015 at 04:07 UTC

    # THIS WORKS print (OUT $line) || die "writing 'splat' $!"; # But putting || die on another line does not. i.e: # print (OUT $line) # || die "writing 'splat' $!";
    That's strange. They both work for me.

    Moreover, I see no reason why they would be different, in fact Deparse tells me they produce identical code:

    % cat id1.pl print (OUT $line) || die "writing 'splat' $!"; % perl -MO=Deparse id1.pl die "writing 'splat' $!" unless print OUT $line; id1.pl syntax OK % cat id2.pl print (OUT $line) || die "writing 'splat' $!"; % perl -MO=Deparse id2.pl die "writing 'splat' $!" unless print OUT $line; id2.pl syntax OK
    Though you could dig further into the root cause of the difference you experienced, I suggest you just stick to the low precedence or and avoid parens with print as described earlier.

      ...and if you need or want parens after print (not in the case you put the filehandle too, but in the case your list to be printed starts with something that need parens) to avoid the error print (...) interpreted as function at .. use the following trick:
      print +(43-1);
      HtH
      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2022-05-21 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?