in reply to Problem with function

You only think that both versions of your code use the same variable, but the second version does not. What happens in fact is, that your sub print_summary uses the global variable $type ($main::type, to be verbose), while you later on declare a lexical variable $type. The strict pragma tells you about that, which is why you should use it:

use strict; sub print_summary { if ($type =~ /w/) { print "Hello\n"; }; }; my $type = "w"; print_summary;

Here, Perl raises an error that you're using an undeclared variable $type in print_summary, which tells you that declaring the variable too late prevents Perl from seeing it.