Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Converting to boolean

by davido (Cardinal)
on Jun 17, 2004 at 04:32 UTC ( [id://367531]=note: print w/replies, xml ) Need Help??


in reply to Converting to boolean

How about a tied scalar operating as a boolean?

package Tie::MyBoolean; sub TIESCALAR { my($class, $value ) = @_; my $boolval = ( $value ) ? 1 : 0; return bless \$boolval, $class; } sub STORE { my $self = shift; ${$self} = ( shift ) ? 1 : 0; return ${$self}; } sub FETCH { my $self = shift; return ${$self}; } 1; package main; use strict; use warnings; tie my $bool, 'Tie::MyBoolean'; $bool = 100; print "$bool\n"; $bool = 0; print "$bool\n"; $bool = ! $bool; print "$bool\n";

This has the characteristic of working a lot like $| (the autoflush special variable), which can only take on 0 or 1 as a value.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-18 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found