in reply to referances of hashes, and subroutines

how can i make this more like pop() or shift() where you dont send the reference to the subroutine, it just changes it?
  • Comment on Re: referances of hashes, and subroutines

Replies are listed 'Best First'.
Re^2: referances of hashes, and subroutines
by GrandFather (Saint) on Apr 22, 2006 at 20:09 UTC

    shift and pop are part of the language and are more like operators than subroutines. They can therefore do magic that is not available to mere mortals.

    Others will very likely show ways of doing what you would like using deep Perl magic. But in most cases doing such stuff is likely to cause grief in the long run as your eye becomes accoustomed to Perl and gets confused by special magic.

    The simple rule is that Perl passes by value. If you want a reference to something then you have to do that explicitely. Confounding that rule (by invoking special magic) will, in the long run, cause trouble because the reader of the code must check every subroutine to see if it might alter parameters in unexpected ways.


    DWIM is Perl's answer to Gödel
      it isnt as much magical as it is prototyping.
      use strict; use warnings; my %list=("bob" =>123, "tom" => "CAT"); sub change(\%){ my $temp = shift; foreach (keys %$temp){ print $_. " = $$temp{$_}\n"; $$temp{$_} .= "hello"; } } change (%list); foreach (keys %list){ print "$_ = $list{$_}\n"; }
      declaring the subroutine as sub change(\%){ will send the values as a reference

        It's magical to the extent that prototypeing is generally frowned on so is one of the lesser known corners of Perl.

        It is also magical in that the local context doesn't provide enough information to understand that there may be side effects when calling the sub. That is dangerous magic. Passing a reference to a sub explicitely is a strong hint that the referent will be altered.


        DWIM is Perl's answer to Gödel
        thanks! This works.
Re^2: referances of hashes, and subroutines
by sh1tn (Priest) on Apr 22, 2006 at 20:34 UTC
    When you want sm.th. like pop() and shift() you use pop() and shift() without reinventing the wheel.


      ya im not reinventing the weel. As you can see in the example. Im just trying to learn the ins and outs of the language