Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Secret Perl Operators: the boolean list squash operator, x!!

by rinceWind (Monsignor)
on Jul 31, 2006 at 17:07 UTC ( #564806=note: print w/ replies, xml ) Need Help??


in reply to Secret Perl Operators: the boolean list squash operator, x!!

This is cool!

Sadly, in most cases where I am doing conditionals, I am using optional hash keys with exists. Unfortunately, these would autovivify under x!!, which is an undesirable side effect. Ifs or ternaries work, but I can't see a way of stopping the autovivification with the "boolean list squash" operator. Can anybody suggest how to solve my variant?

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)


Comment on Re: Secret Perl Operators: the boolean list squash operator, x!!
Select or Download Code
Re^2: Secret Perl Operators: the boolean list squash operator, x!!
by Jenda (Abbot) on Jul 31, 2006 at 17:20 UTC

      He probably means something like this:

      exists $hash{ foo } && exists $hash{ foo }{ bar } ? $hash{ foo }{ bar +} : ()

      In this case, switching to x!! will cause $hash{ foo }{ bar } to always be evaluated, which will cause $hash{ foo } to be autovivified even if the condition is false.

      Makeshifts last the longest.

        I see. Actually to prevent confusion ... even this would autovivify:

        @a = ( $hash{foo}{bar} x!! 0);
        I guess we'd need to "fix" the x operator to not evaluate the lefthand operator if the righthand one is <=0.

        Update: Never mind, i see it now ;P)

        Why would that be trouble?

        use strict; use warnings; use Data::Dumper; my $hash = { test => 1}; print Dumper($hash); print "Hello\n" x!! exists($hash->{test}); print Dumper($hash); print "One More\n" x!! exists($hash->{test2}) && exists($hash->{test2} +->{Dude}); print Dumper($hash);

        That works as expected and doesn't autovivify anything...did I miss something?


        ___________
        Eric Hodges
Re^2: Secret Perl Operators: the boolean list squash operator, x!!
by Aristotle (Chancellor) on Jul 31, 2006 at 17:40 UTC

    Yeah, multi-level data structures and autovivification mean headaches. Check out Data::Diver, it will make your life much easier.

    Makeshifts last the longest.

      Wow! That's a module I wasn't aware of, by our own tye, too!

      Also spot on for understanding and explaining my autovivi problem. Aristotle++

      --

      Oh Lord, won’t you burn me a Knoppix CD ?
      My friends all rate Windows, I must disagree.
      Your powers of persuasion will set them all free,
      So oh Lord, won’t you burn me a Knoppix CD ?
      (Missquoting Janis Joplin)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2015-07-03 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls