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

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,

Replies are listed 'Best First'.
Re: my $scope as the default for variables
by Anonymous Monk on Jun 19, 2012 at 08:17 UTC

    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 )

      Also, a major part of needing to predeclare your variables is to avoid typos. Having default-lexical scope avoids a lot of the nasty action-at-a-distance that default-global scope produces, but it still can't detect the typo between $outfile and $out_file for you, which I consider the major point of using strict.

        :) I was going to list this as a reason, but I remembered warnings

        $ perl -Mwarnings -e " $foo = 1; " Name "main::foo" used only once: possible typo at -e line 1. $ perl -Mwarnings -e " my $foo = 1; "

        If warnings can detect this I see no reason a New pragma 'scope' to change Perl's default scoping couldn't do the same

        Naturally that won't help if you're making the typo twice, but neither does strict/my :)

Re: my $scope as the default for variables
by Je55eah (Novice) on Jun 19, 2012 at 09:09 UTC

    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.

      This is kinda a dangerous thing to do, unless you completely trust the author of the files you're processing. (And you should probably not completely trust the author. Even if you're the author.) Otherwise they could slip in something like...

      Hello @{[ qx!rm -fr ~/Documents! ]} World

      ... and cause your eval to delete important documents.

      Better would be to stuff the variables that you want them to be able to access into a hash called %vars and process it like this:

      %vars = ( hostname => 'my.example.com', sitename => 'My Example', ); #... while(<SOURCE>) { s/\$\{(\w+?)\}/$vars{$1}/e; $text .= $_; }
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      As using the string form of eval with untrusted input is a considerable security risk, and your markdown can easily contain double-quotes which break your code, maybe you are interested in using a templating system or simply applying this very small, very simple templating system? It allows you to restrict what variables get interpolated, and it prevents execution of arbitrary code:

      sub interpolate { my ($text, %vars) = @_; $text =~ s!\$(\w+)|\$\{(\w+)\}! my $name = $1 || $2; exists $vars{ $name } ? $vars{ $name } : '$' . $name !ge; return $text }; print interpolate( 'Hello $user, this is $var1. We also use the unknown variable $foo +.', var1 => 'The value of var1', user => 'Je55eah', # ... );

      You are right, of course, about needing a templating system, and it should be a do later upgrade if I continue to use this script. For now I am focused on getting the actual document created ASAP. All of my input is trusted since I am writing it. You did make a good point about the quotes. Perhaps I will change those quotes to qq(). One thing that I do not want to do is have a third list of the variables that I need to change every time I add a variable or modify a variable name. Maybe I will use one of the scripts that you have suggested after I take a few minutes to grok that regex or whatever that is.

      Thanks,