in reply to Passing a list to an Object

I know danger has already answered your question, but I thought I'd show another possibly clearer way to accomplish your aim, using a slightly different style.
sub do_that { my $self = shift; my %args = ref($_[0]) ? %{$_[0]} : @_; $args{chew} ||= $self->{food} || 'meat'; return "I like $args{chew)!\n"; }

Replies are listed 'Best First'.
Re: Re: Passing a list to an Object
by lzcd (Pilgrim) on Jan 13, 2001 at 13:59 UTC
    Thank you for your second opinion. :)

    I seem to remember that hash arrays aren't known for thier default sorting abilities (eg. they may not keep their original insertion order.).
    Doesn't a shift run the risk of killing an unintended list item?
    (...or am I just getting lists, hashes and small terriers named 'Spanky' all turned around at this wrong hour of the day?)
      When you say
      MyClass->doThat(chew=>'bananas')
      The arguments don't get passed as a hash. '=>' doesn't automatically transform things into hashes, but instead is the equivalent of a comma, with the side effect of ensuring that the word before it is interpreted as a string. So it's the equivalent of saying:
      MyClass->doThat('chew', 'bananas')
      Or even:
      MyClass::doThat('MyClass', 'chew', 'bananas')
      So it's a list, not a hash, and you don't need to worry about order getting scrambled. The list doesn't turn into a hash until you do something like:
      sub doThat { my $type = shift; my %input = @_; }
      Then, you've copied the list into an associative array. Until then, plain-vanilla list... Sorry to flog a dead horse, but I saw a misconception waiting to happen...

      stephen

      No, as danger says above, if a funtion/method is called as Class->method(); or $instance->method();, the first argument will always be the part before the class name or instance variable. He didn't mention the indirect syntax ie method Class ();, which acts the same way as the direct syntax, but is less clear except for $foo = new Class;.