Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: lost in eval and regexp

by ikegami (Patriarch)
on Nov 21, 2007 at 15:00 UTC ( [id://652148]=note: print w/replies, xml ) Need Help??


in reply to lost in eval and regexp

You should really really really really really really use an existing template module. You would have installed and learned it in the time it took you to get this question answered. Converting your templates over isn't all that hard either.

sub convert { my ($template_name) = @_; my $template; { open my $fh, '<', $template_name or die("Unable to open template file \"$template_name\": $!\ +n"); local $/; $template = <$fh>; } $template =~ s/~~\$(.+?)~~/... new syntax .../g; { open my $fh, '>', $template_name or die("Unable to overwrite template file \"$template_name\" +: $!\n"); print $fh $template; } }

At the very least, pass your variables in a hash. That will get rid of the eval EXPR and solve all scoping problems.

sub replace { my ($template_name, $params) = @_; my $template; { open my $fh, '<', $template_name or die("Unable to open template file \"$template_name\": $!\ +n"); local $/; $template = <$fh>; } $template =~ s/~~\$(.+?)~~/ if (exists($params->{$1})) { if (defined($params->{$1})) { $params->{$1} } else { warn("Template file \"$template_name\" references undefine +d variable $params->{$1}\n"); '' } } else { warn("Template file \"$template_name\" references non-existin +g variable $params->{$1}\n"); '' } /eg; print("$template\n"); } my %params = ( cell => ..., day_prev_month => ..., ); replace($template_name, \%params);

Replies are listed 'Best First'.
Re^2: lost in eval and regexp
by lepetitalbert (Abbot) on Nov 21, 2007 at 16:06 UTC

    Dear Monks,

    Thank you gamache, will try that immediatly

    Good point toolic, here's the output od the first template :

    novembre 2007 </td> <td colspan="2" align="right"> <div id="date"> Mercredi 21 novembre &nbsp; </div> </td> </tr> <tr> <td class="titlebar1"> </td> <td class="titlebar2" width="80px" align="righ +t"> <a class="white" href="/cgi-bin/inlook /agenda.pl?user= &month= 10&year= 07"> &raquo; octobre &nbsp; </a> <td class="titlebar2" width="80px" align="righ +t"> <a class="white" href="/cgi-bin/inlook /agenda.pl?user= &month= 12&year= 07"> &raquo; décembre &nbsp; </a> </td> </tr> </table> </div>

    ikegami, as I agree with you totally, that's what I did first with Html::Template. But the goal of the game is to keep the installaion of the script as simple as 'upload ,run'. And sorry, I was unable to find the regexp :)

    Thank you fellow Monks

    And have a nice day

    "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

      What about "upload,unzip,run"? Templating systems are usually Pure Perl, so there's no installation requirement other than having the files handy.

      You may need the following at the top of your script in order to tell Perl where to look for the modules. Note that only core modules are used.

      use File::Spec::Functions qw( rel2abs ); use File::Basename qw( dirname ); use lib dirname(rel2abs($0));

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://652148]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found