in reply to Re^3: Evil Interview Questions
in thread Evil Interview Questions

Can you see the problem?
Well, some... 1 not so, and 5 serious problems:
package Foo::Bar; # (1) strict, warnings? sub new { my %args = shift; # (2) first argument is class name. # (3) shift? really? even if @_, then # odd number of elements in hash? my $self; # (4) uninitialized scalar... bless $self; # ... can't be blessed (not a reference) # (5) not using 2-arg-bless? inheritance? if ($args{name}) { $self->{_name} } # (6) $self->{_name} /what/ ? return $self; } sub name { my $self = shift; return $self->{_name}; }

Granted, your object implementation is just a quickly-whipped-up example. Don't let the heat of the discussion lend you to write bad code!

But anyways -

Technically they are both correct, but one of these basically creates havoc that cannot be overcome by intimate knowledge of the language.

Oh, it can be overcome - if you use that knowledge to implement the object in a good way in the first place:

package Foo::Bar; use Alter ego => {}; use strict; use warnings; sub new { my $class = shift; @_ % 2 and die "odd number of elements in \@_, aborted"; my %args = @_; my $self = \do { my $obj }; bless $self, $class; ego($self)->{_name} = $args{name} if $args{name}; return $self; } sub name { my $self = shift; defined( ego $self) or die "not a valid object"; return ego($self)->{_name} } package main; my $bar = Foo::Bar->new( name => 'Bob Lambert' ); print "Name of my object " . $bar->name . "\n"; # method call print "Name from my hash " . $bar->{_name} . "\n"; # hash key __END__ Name of my object Bob Lambert Not a HASH reference at - line 23.

No room for mistakes - the $bar object is just an empty scalar reference. It is better to not create the pits than avoiding the pitfalls. Or as a friend of mine uses to say

mejor la seguridad que la policia.

better is security than the police

;-)

update:

The validity of hash key lookup for object attributes depends entirely on the purpose and declared interface of the object - its contract. Hash lookups are much faster than method calls, and there might be a speed concern.

But back to the discussion - these sort of interview questions make sense, to test the depth of detail knowledge. If they are the only ones, the answers will reveal only a small part of the person interviewed. It certainly is short-sighted to only rely upon, or over-estimate, the technical quiz.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^5: Evil Interview Questions
by Herkum (Parson) on Feb 11, 2008 at 00:42 UTC

    BUSTED! You are right, I did not really go do good work for the object code (the problem of when you get into a hurry).

    However, I will stick to my guns with, design/process > technical knowledge.

      Before I post code here, I run it - that's what we preach to newcomers on a daily basis... ;-)

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}