in reply to my $scope as the default for variables
in thread Please help me print this hash.

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.

Replies are listed 'Best First'.
Re^2: my $scope as the default for variables
by tobyink (Canon) on Jun 19, 2012 at 09:26 UTC

    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'
Re^2: my $scope as the default for variables
by Corion (Patriarch) on Jun 19, 2012 at 09:18 UTC

    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', # ... );
Re^2: my $scope as the default for variables
by Anonymous Monk on Jun 19, 2012 at 09:47 UTC
Re^2: my $scope as the default for variables
by Je55eah (Novice) on Jun 19, 2012 at 09:39 UTC

    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,