Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

staements in strings

by PriNet (Monk)
on May 13, 2011 at 21:11 UTC ( [id://904731]=perlquestion: print w/replies, xml ) Need Help??

PriNet has asked for the wisdom of the Perl Monks concerning the following question:

ok, heres a good one... can i place a "statement" inside a string then "execute" that string? i'm using an external file for the additional statements for the original script and therefore cannot use the "eval" function, because it never comes back to my original script. my goal is to be able to provide specific perl script "modules" for different cases in the "original" script. Example:

$Dummy = "\$Object = \"testing\";"; #assign a perl statement to '$D +ummy' $Dummy; #execute the perl statement that is 'inside +' $Dummy

you mean there's any easier way?

Replies are listed 'Best First'.
Re: staements in strings
by wfsp (Abbot) on May 14, 2011 at 08:37 UTC
    CGI applications often need to generate output depending on input/parameters. One way of achieving this is to use a dispatch table. The following highly contrived example demonstrates how a dispatch table could be implemented (not using CGI or modules to keep it simple).
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $params = { param1 => 1, param2 => 2, }; my ($case, $args) = process_params($params); my %dispatch_table = ( case1 => \&case1, case2 => \&case2, default => \&default, ); my $page_header = page_header(); my $body = $dispatch_table{$case}->($args); my $page_footer = page_footer(); print $page_header, $body, $page_footer; sub process_params{ my $params = shift; my $case = q{default}; my $args = {}; if ($params->{q{param1}} eq 1){ $case = q{case1}; } elsif ($params->(q{param2}) eq 2){ $case = q{case2}; } else{ $case = q{default}; } return $case, $args; } sub case1 { my $args = shift; my $output = qq{case one\n}; return $output; } sub case2 { my $args = shift; my $output = qq{case two\n}; return $output; } sub default { my $args = shift; my $output = qq{default\n}; return $output; } sub page_header { my $ph = qq{page header\n}; return $ph; } sub page_footer { my $pf = q{page footer}; return $pf; }
    The "case" subs could call subs/methods in different (application) modules.

    If this approach is suitable then something like CGI::Application would be worth looking at. It can help look after and simplify all this and much more. Combined with, say, HTML::Template, CGI applications, imo, become a whole lot easier and reusable. A little extra effort up front but well worth it.

Re: staements in strings
by LanX (Saint) on May 13, 2011 at 21:25 UTC
    IMHO a XY-question.

    if you can't eval it (because it's to dangerous), you most likely need one of the various config file solutions.

    Cheers Rolf

    UPDATE:

    or what do you mean with because it never comes back to my original script. .

    Do you just wanna import variables from an external module you use or require?

      i have the "original" script that starts an HTML header, prints out some things, assigns some variables, etc., then (depending on the variables values) if needs to load either the "module" A or "module" B and such to do some other scripting changes to variables, even print some things if necessary, then return back to the original script and continue on where it left off to finish the original page. eg; put part of the original script in seperate "modules" and implement the individual "modules" depending on different caese of the original scripts variable status's.


      you mean there's any easier way?
        > i have the "original" script that starts an HTML header, prints out some things, assigns some variables, etc., then (depending on the variables values) if needs to load either the "module" A or "module" B and such to do some other scripting changes to variables, even print some things if necessary, then return back to the original script and continue on where it left off to finish the original page. eg; put part of the original script in seperate "modules" and implement the individual "modules" depending on different caese of the original scripts variable status's.

        What???

        I can only guess what you are trying to do, here some general tips about communication between modules

        1. when using a module (compile time!) you can pass strings to its import method.
        2. import can set variables in the namespace of the importing module
        3. a require evals a modules code like "do filepath" would, that is at runtime and just in place (i.e.  eval `cat path/file.pl` but only once).
        4. even if your modules are in different namespaces (package) you can always get and set all their global variables by qualifying the $package::path and access all their package::functions().

        So there are plenty of possibilities, if you show us more specific code examples of your use case I'm sure someone can hint you to a best practice approach.

        Cheers Rolf

        you know how to access package variables?

        e.g.

        $packagename::varname

        Cheers Rolf

      of course, your refernce to "use" has me thinking...


      you mean there's any easier way?
Re: staements in strings
by Plankton (Vicar) on May 13, 2011 at 21:21 UTC
    Why not just run it and find out?

      i did try the example... the interpeter just "skips" over the statement and doesn't recognize it... but i'm still trying to crash the interpeter ! *heh*


      you mean there's any easier way?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found