http://www.perlmonks.org?node_id=998402


in reply to Re: Error "Can't use an undefined value as a HASH reference" while working with generated objects
in thread Error "Can't use an undefined value as a HASH reference" while working with generated objects

Hi McA,

Waow, thanks it worked perfectly, but could you just please explain it to me ^^" I'm unused to your use of : ? %{$values[0][0]} : ();

  • Comment on Re^2: Error "Can't use an undefined value as a HASH reference" while working with generated objects
  • Download Code

Replies are listed 'Best First'.
Re^3: Error "Can't use an undefined value as a HASH reference" while working with generated objects
by Anonymous Monk on Oct 11, 2012 at 10:04 UTC

    my %valeur; if( @values and defined $values[0] and 'ARRAY' eq ref $values[0] and defined $values[0][0] and 'HASH' eq ref $values[0][0] ) { %valeur = %{ $values[0][0] }; }

    my %valeur; if ( my $want = Dive( \@values, qw/ 0 0 / ){ %valeur = %$want; }

      The Data::Diver example will die if $values[0][0] is defined and true but not a hashref - e.g. if it's an arrayref.

      use Data::Diver 'Dive'; use Scalar::Does 'does'; if (my $want = Dive( \@values, qw/ 0 0 / )) { %valeur = %$want if does($want, 'HASH'); }
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Waow thanks, I really didn't push the defined so far ...

      I just tested it (the first method, I'm having some trouble with your second, some undeclared varibles...), It's working as well, thanks

        the first method, I'm having some trouble with your second, some undeclared varibles...

        Its a missing ")"

        one pair for if() and another for Dive()

Re^3: Error "Can't use an undefined value as a HASH reference" while working with generated objects
by McA (Priest) on Oct 12, 2012 at 09:07 UTC

    Hi!

    Sorry for the late reply: Please have a look at http://perldoc.perl.org/perlop.html#Conditional-Operator . This operator is also known as triadic operator. It's a kind of if-then-else-operator.

    Hope it helps

    Best regards
    McA

      Hi,

      Not a problem, I just though you didn't see my reply ^^" Well thanks, it's a little easier to understand now with some documentation.

      Thanks again for your answers

      Regards, HJO