develooper Front page | perl.perl5.porters | Postings from November 2010

concerns regarding push $scalar

Thread Next
From:
Tom Christiansen
Date:
November 17, 2010 07:37
Subject:
concerns regarding push $scalar
Message ID:
29094.1290008205@chthon
The compiler caught a bug that it will/would miss under the change to
push/pop/etc that tolerates a scalar argument.  At one point in the code
below, I used push $flags when I needed push @flags, and the compiler got
me right away.

The following program is admittedly in rather bad form because of its reuse
of variable names under different sigils, but I still was glad that it
caught the problem.  It won't catch it any longer, will it though?  Yeah,
yeah: use of $flag, $flags, @flags, %flags is just terrible, but I did it
to prove a certain point.

--tom

    #!/usr/bin/env perl
    #
    # demo showing open flags

    use 5.10.0;
    use strict;
    use autodie;
    use warnings qw< FATAL all >;

    use Fcntl;

    my (%flags, @fh);
    my $DEVICE  = "/dev/null";
    my @F_MODES = map { $_ => "+$_" } qw[ < > >> ];
    my @O_MODES = map { $_ | O_WRONLY } 
	    O_SYNC                          ,
		     O_NONBLOCK             ,
	    O_SYNC              | O_APPEND  ,
		     O_NONBLOCK | O_APPEND  ,
	    O_SYNC | O_NONBLOCK | O_APPEND  ,
	;

       open($fh[++$#fh], $_, $DEVICE) for @F_MODES;
    sysopen($fh[++$#fh], $DEVICE, $_) for @O_MODES;

    eval { $flags{$_} = main->$_ } for grep /^O_/, keys %::; 

    for my $fh (@fh) { 
	printf("fileno %2d: " => fileno($fh));
	my ($flags => @flags) = 0+fcntl($fh, F_GETFL, my $junk);
	while (my($_, $flag) = each %flags) {
	    next if $flag == O_ACCMODE;
	    push @flags => /O_(.*)/ if $flags & $flag;
	} 
	push @flags => "RDONLY" unless $flags & O_ACCMODE;
	printf("%s\n",  join(", " => map{lc}@flags));
    }

    close $_ for reverse STDOUT => @fh;


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About