in reply to Variable scoping and globs and reference passing to subroutines!!!

As was pointed out, your confusion was because assigning a hashref to *foo assigns the hash to %foo.

But the reason that I am responding is to head off a bad habit. Lose the prototypes. They don't do what you think and you almost definitely don't want what they actually do. Prototypes in Perl do argument coercion. They don't prevent any potential mistakes or warn you of anything wrong. They just provide the possibility of minor convenience at the risk of causing serious confusion and introducing unexpected bugs.

  • Comment on Re: Variable scoping and globs and reference passing to subroutines!!!

Replies are listed 'Best First'.
Re^2: Variable scoping and globs and reference passing to subroutines!!!
by MarkovChain (Sexton) on Dec 19, 2008 at 14:37 UTC
    Awesome. Thank you Sir / Madame for all your help and time.
    Ran it thru my debugger and it looks such.

    Lessons learnt:
      > Will use Data::Dumper for debugging from now on.
      > As to not using subroutine declarations, should I then disable the  use strict 'subs';? I understand its an error to use subroutine_name when use strict 'subs' is in effect and the subroutine is not pre-declared. (You could use subroutine_name() though.) I just checked up Perl::Tidy and you are indeed correct that no subroutine declarations are done - subroutines are directly defined. Also in another of the perl scripts used by perl-support, the author ends up defining all the subroutines prior to using them. Would you encourage such authoring to be best-practice?
      You misunderstand. I was advising you to not use the prototype. That is you should write:
      sub foo { # do something }
      rather than
      sub foo ($) { # do something }
      But since you have raised the issue of predeclaration, the usual practice is to pre-declare nothing and to call them with parens. For instance that is what Damian Conway recommends in Perl Best Practices. Incidentally it isn't just strict that doesn't like the bare form if no declaration has happened. If foo hasn't been declared then foo "bar"; is a syntax error. So no matter what you do, there is no reason to avoid strict.