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

Is there a way that I can assign multiple lines of code to one variable?

Like, store a bunch of HTML into one variable, and then call that variable somewhere within my script?

I kind of want to do stuff while i'm in a print<<BLAH thing

so i want to be able to do something like this

print<<BLAH;

if {$blah = $blah){
print $blah;
}

BLAH

is that possible?

Replies are listed 'Best First'.
Re: Working with Variables
by stephen (Priest) on Apr 24, 2001 at 00:03 UTC
    Sorta. You can use @{[ ... ]} to interpolate code within quotes, like so:
    print <<"BLAH"; This is the start of it... @{[ evaluated_code($blah) ]} This is the end of it. BLAH sub evaluated_code { if ($blah == $blah) { return $blah } }
    Don't use 'print' in your inner code, or the results will not be what you expect.

    stephen

Re: Working with Variables
by damian1301 (Curate) on Apr 23, 2001 at 23:58 UTC
    Yes, it is. Do something like this:

    $var=<<BLAH; if($lbha = 'poo'){ print "oh"; } BLAH eval $var;
    You're probably wondering about that little eval $var part. Well, all eval does is evaluate the code and execute it as a separate script. Otherwise you would just literally print "if($lbha = 'poo'){ do stuff.... }" out of the screen, which isn't what you want.

    I hope I understood your question and answered it properly.

    Tiptoeing up to a Perl hacker.
    Dave AKA damian

      Lets say that inside a $var=<<VAR; print "Blah"; VAR print<<BLAH; print "Blah"; eval $var; BLAH will that replace eval $var with "Blah"?

        No, it will replace $var with the string print "Blah";\n, so when you run it you'll get

        print "Blah"; eval print "Blah"; ;
        as your output, which is probably not what you want.

        See Masem's post and stephen's post below for how to do more interesting interpolation.



        If God had meant us to fly, he would *never* have give us the railroads.
            --Michael Flanders

        No, what you want is

        $var=<<BLAH; print "Blah"; BLAH eval $var;


        Because when it executes it it will print Blah out on the screen because you have print "Blah"; already inside $var;

        UPDATE:WOULD SOMEONE MIND TELLING ME WHAT IS SO WRONG ABOUT THIS POST THAT I GOT A -- ON IT?? THIS IS SO *beeping* REDICULOUS. CONSTANTLY BEING ATTACKED.

        Tiptoeing up to a Perl hacker.
        Dave AKA damian

Re: Working with Variables
by Masem (Monsignor) on Apr 24, 2001 at 00:01 UTC
    It sounds like you can do one of two things:

    Use a subroutine that you can pass values to and that prints everything out. You don't have to worry about storing all that text into a single variable, and you have a lot more flexibility with this method. If you go this route, you should pass any arguements you have as a signle hash so that you don't have to worry about messing up the order of the arguements.

    Alternative, you could use a template module, like Template Toolkit 2 or HTML::Template, which have some basic logic structures that you can actually embed within the text to do what appear to need it to do.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain