in reply to Is use strict always appropriate?
in thread Please help me print this hash.

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?

Sure

my( $foo, $bar ) = ( 1, 2 );

should I do the following instead?

No, that misses the point , might as well not use strict at all in that case.  strict itself confers no benefits; The benefits come from avoidance of the bad practices forbidden by  strict :)

To understand these read: See Tutorials: Variable Scoping in Perl: the basics, Coping with Scoping , Mini-Tutorial: Perl's Memory Management, Lexical scoping like a fox, Read this if you want to cut your development time in half!

The free Modern Perl book covers all this too

Replies are listed 'Best First'.
another my vs our subroutine test
by Je55eah (Novice) on Jun 19, 2012 at 07:16 UTC

    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
Re^2: Is use strict always appropriate?
by Je55eah (Novice) on Jun 19, 2012 at 04:26 UTC
    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.

      sub one { my $x = "we are in one()\n"; print $x; two(); print $x; } sub two { my $x = "we are in two()\n"; print $x; } one();
      versus
      sub one { our $x = "we are in one()\n"; print $x; two(); print $x; } sub two { our $x = "we are in two()\n"; print $x; } one();

      Don't use our() until you learn what does it really mean.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        There may not be any good way to say this, but I'll take a stab at it.

        When using my within a subroutine, it seems that the subroutine cannot share modified variable values with other subroutines unless the other subroutine is defined within the sub containing the my scoped valiable. Likewise, the my scoped value is not shared with the parent block unless the sub returns the value to the containing scope so that the desired modifications can be made. local is similar, but while the block where a local variable is defined or redefined is being parsed any subs that it triggers get to use the local value. If it were my variable then the subs would have whatever value they inherit from their own parent scope. If the variable is defined in the same block as the subroutines then my and local are effectively the same because the local scope is active as long as the subs are being defined and the subs inherit from my scope.

      I see that you are pushing for my but I am not convinced that is the best choice based ...  eval('"' . $ourcode . '"'); ...

      For the moment you can just take my our word for it :) I know it sounds like an appeal to authority, but its the benefit of experience speaking

      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.

      Well, you really should be writing modules instead of scripts, see Simple Module Tutorial and Program Repair Shop ( Declarations and File-Scope "my" Variables )

testing my vs our with subroutines
by Je55eah (Novice) on Jun 19, 2012 at 06:41 UTC

    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