in reply to Please help me print this hash.

These are two of the most important things you can do (besides reading up on hashes and references, etc.) to fix your script:

When I ran your script with use warnings;:

Reference found where even-sized list expected at ./junk.pl line 6. Use of uninitialized value $amount in addition (+) at ./junk.pl line 2 +7. Use of uninitialized value $total_admin_expenses in addition (+) at ./ +junk.pl line 27. Use of uninitialized value $_[0] in reverse at ./junk.pl line 35.

Replies are listed 'Best First'.
Is use strict always appropriate?
by Je55eah (Novice) on Jun 19, 2012 at 03:16 UTC

    It appears to me that the best solution is to declare the variables with our. Is there any way to do this without writing our for each variable or without listing all of the variable names within an our declaration and again when I assign values to them?

    Since I am only writing a report with Perl, I don't have a lot of stuff competing for the namespace. I would like to keep the readability high and the amount of redundancy low so that the code is easy to change and easy to read. If I package this into an object with getters and setter later then I can find and replace our with my, but for now I think that our seems to be the best choice. Thoughts?

    use strict; # while I would like to do this: # # our # ( # $x = 1; # $y = 2; # ); # # it seems that I must do: use ($x, $y); $x = 1; $y = 2; # or this: our $x = 3; our $y = 4; # before I can do: $x = 5; $y = 6;

    should I do the following instead?

    use strict; no strict "vars"; $x = 5; $y = 6;

    Thank you,

        This returns:

        22 33

        # Begin use strict; our ( $var1 ) = ( 10 ); my ( $var2 ) = ( 1 ); our $ourthing = &{ sub { $var1 = $var1 + 10; $var2 = $var2 + 1; return + $var1 + $var2;}}(); print $ourthing . "\n"; my $mything = &{ sub { $var1 = $var1 + 10; $var2 = $var2 + 1; return $ +var1 + $var2;}}(); print $mything . "\n"; # End
        Thanks,

        Somehow I missed that option. I suppose that for my purposes I should just write our a lot. I am working with a long list a variables and I would like to see the value next to the variable name.

        I see that you are pushing for my but I am not convinced that is the best choice based on what I have been reading. It is easy enough to swap back and forth, so I suppose it doesn't matter much..... ( I went and made this to satisfy my curiosity at this point: )

        # Begin use strict; our ( $var1 ) = ( 1 ); my ( $var2 ) = ( 2 ); our $ourcode = 'In ourcode eval:\n\$var1 = ${var1}\n\$var2 = ${var2}\n +'; my $mycode = 'In mycode eval:\n\$var1 = ${var1}\n\$var2 = ${var2}\n'; print eval('"' . $ourcode . '"'); print eval('"' . $mycode . '"'); print "In the main:\n\$var1 = ${var1}\n\$var2 = ${var2}\n"; # End

        I thought that the eval might have tripped up with variables scoped to my, but everything worked out fine with the above test. I can't think of too many reasons that another script might call on variables within this script, so I may go with my after all.

        The result is 3333

        # Begin use strict; our ( $var1 ) = ( 1 ); my ( $var2 ) = ( 2 ); our $ourroutineresult = sub {our $ourroutineresult = $var1 + $var2;}; print &$ourroutineresult; our $ourroutineresult = sub {my $ourroutineresult = $var1 + $var2;}; print &$ourroutineresult; my $myroutineresult = sub {our $myroutineresult = $var1 + $var2;}; print &$myroutineresult; my $myroutineresult = sub {my $myroutineresult = $var1 + $var2;}; print &$myroutineresult; # End

      With very short scripts (less than 6 lines, say), it probably confers no benefits.

      For anything longer, use strict qw(subs vars) seems to always be appropriate. use strict qw(refs) is usually appropriate, but it's sometimes useful to allow non-strict refs.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Is there any way to set my as the default scope for variables when no scope is specified? Why isn't this the case now?

      Thanks,

        Is there any way to set my as the default scope for variables when no scope is specified?

        No, not that I'm aware, though it might be possible to add that functionality (cpan://Method::Signatures).

        Why isn't this the case now?

        1) tradition and backwards compatibilty (perl goes back to 1987) 2) nobody's done the work ( even Method::Signatures is only since 2007 )

        You guys are great. I appreciate all of the information. Everything below is probably useless information to you.

        In all my newbieness I am parsing through a text file that contains ${perlVariable}s with an eval like this:

        foreach my $markdown_filename(@sources) { my $text = ''; open(SOURCE, "<$markdown_filename"); while(<SOURCE>) { $text .= eval('"' . $_ . '"'); } close(SOURCE); print $text; }

        I could incorporate the markdown into the perl script but then I wouldn't have the awesome ability to preview the formatting by drafting the markdown with DownMarker (https://bitbucket.org/wcoenen/downmarker).

        The funny thing is that I was doing this to quiet down the warnings from the eval until I declared everything with my:

        use warnings; no warnings 'once';

        I also ended up declaring the variables with my ($var1, $var2, $etc ); and then defining them below so that I could list them in alphabetical order at the top and in execution order when I define them. I have found that I can use the output from warnings/strict to tell me which variables I need to add to my alphabetical list of initializations.