[tpm] Stupid question time!

Shaun Fryer sfryer at sourcery.ca
Fri Jan 29 18:12:26 PST 2010

On Fri, Jan 29, 2010 at 8:38 PM, Fulko Hew <fulko.hew at gmail.com> wrote:

> I would have thought
> $bar = $foo unless $bar;     ...might be more intuitive.?
> ... in retrospect. I guess it depends if yout think the glass is half full,
> or half empty?

The most common example would be creating a hash which I'm populating with
values, and don't want a given key to exist() if the right hand side is

my %foo;
$foo{bar} = $baz if $baz;

In this case...

$foo{bar} = $baz // undef;

...while interesting, isn't appropriate.

Another (related) example might be a dual use setter/getter, for the moment
disregarding the ": lvalue" attribute.

sub foo {
    my ($self, $value) = @_;
    $self->{foo} = $value if $value;
  return $self->{foo};

Now you could this...

$obj->foo('bar'); # set foo
$bar = $obj->foo; # get foo

Anyway, these are just simple examples off the top of my head. It can easily
be more complex. I've found myself thinking this periodically, so I figured
worth asking. Maybe someone has a good reason why it's a dumb idea to worry
about in the first place?

> but I don't think you want to put the my in front because ISTR that
> the variable would only poof into existence if the condition evaluated
> true.
> so it would still be longer.

You're absolute right about that. Noticed it right after I clicked send, but
didn't want to create more noise by correcting myself.

> I don't think the 'if right-handed truthiness' operator exists
> .. in a short hand form worth using :-|

I wasn't sure, but then that only means it's time to avail myself of those
more in the know. ;) Would be a cool/useful feature though, in my opinion.

Shaun Fryer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20100129/a92ab215/attachment.html>

More information about the toronto-pm mailing list