matrixx515 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,
Attempting to retrieve the fcntl flags for a newly created output file handle using the code below.
The call to fcntl returns: 'Inappropriate ioctl for device'

use strict; use warnings; use Fcntl; my $file_name='/home/dir/temp/test_me.txt'; open my $fh,'>',$file_name || die "can't open $file_name for writing: +$!\n"; my $flags=fcntl $fh,F_GETFL,0 || die "fcntl call failed: $!\n"; print "Flags: $flags\n"; close $fh || die "can't close write handle to $file_name: $!\n";

Thoughts on what I'm missing here? Any, and all ideas are appreciated.
This is my first posted question so please advise (and go easy on me :) ) if there are formatting errors.
Thank you!

Replies are listed 'Best First'.
Re: Using fcntl function for the first time
by dave_the_m (Monsignor) on Mar 11, 2015 at 22:24 UTC
    Precedence issues. Try replacing those '||'s with 'or's. For example, this
    my $flags=fcntl $fh,F_GETFL,0 || die "fcntl call failed: $!\n";
    is being seen by the compiler as:
    my $flags=fcntl $fh,F_GETFL, (0||die "fcntl call failed: $!\n");

    Dave.

Re: Using fcntl function for the first time
by Anonymous Monk on Mar 11, 2015 at 22:30 UTC
    Either use:
    use strict; use warnings; use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); my $file_name='/tmp/test_me.txt'; open my $fh,'>',$file_name || die "can't open $file_name for writing: +$!\n"; my $flags=fcntl $fh,F_GETFL,O_NONBLOCK || die "fcntl call failed: $!\n +"; print "Flags: $flags\n"; close $fh || die "can't close write handle to $file_name: $!\n";
    or capture your packed data into a variable:
    my $flags=fcntl(FH,F_GETFL,$_) || die "fcntl call failed: $!\n";
      open my $fh,'>',$file_name || die "can't open $file_name for writing: +$!\n";
      No..either:
      open my $fh,'>',$file_name or die "can't open $file_name for writing: +$!\n";
      Or:
      open(my $fh,'>',$file_name) || die "can't open $file_name for writing: + $!\n";
      And similar for your other uses of the "||" operator.
Re: Using fcntl function for the first time
by matrixx515 (Acolyte) on Mar 11, 2015 at 23:18 UTC

    Thanks for all the replies. Replaced all "||"'s with "or"'s, and am getting expected return from fcntl call.

    Will also brush up on precedence rules.