http://qs321.pair.com?node_id=151418


in reply to Problem Declaring Variable

Try:
&print_report($yesterday); #And in print_report: sub print_report { my $yesterday=shift foreach (sort keys %$today) { print LOG "$_\n" unless exists $yesterday->{$_}; } # etc. etc. etc.


GreetZ!,

print "profeth still\n" if /bird|devil/;

Replies are listed 'Best First'.
Re(2): Problem Declaring Variable
by dmmiller2k (Chaplain) on Mar 13, 2002 at 17:21 UTC

    Exactly! ++ChOas.

    Rather than effectively creating a global variable by moving the declaration of my $yesterday out of the if ($script_mode eq 'normal') { statement in the main code (as has been suggested in other comments), it is much better to pass it as a parameter to the print_report subroutine.

    In fact, I'd even take it one step further by also passing $today to all three subroutines that use it (read_benchmark, write_benchmark AND print_report), thus decoupling those subroutines from this specific application and making them candidates for a Perl module (e.g., if the scope of the program were to grow, as occasionally happens, requiring the writing of other scripts which would otherwise have to use cut'n'pasted copies of the same routines)./P>

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime