using eval to create formats 
Author Message
 using eval to create formats

  What I'm trying to do is to allow a the user to pick line lengths and have
all output wrapped on word boundaries using a format statement within an eval.

It works if I do the following:

$longsentence='this is a long meaningless sentence whose whole purpose in life is to test how a line wraps in perl using formats and how I can use evals to do these things better.  OK, long enough.';

$_ = $longsentence;
format STDOUT =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$_                
.                

while($_){
    write STDOUT  unless /^$/;

Quote:
}

But if I try something like this it fails miserably:

$_=$longsentence;
$picture = '^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<';
$variable = '$_';
$filehandle = 'STDOUT';
eval "format $filehandle =\n$variable\n\.\n";

while($_){
    write STDOUT  unless /^$/;

Quote:
}

Any suggestions??

  Also while I'm asking, what the quickest way given a line length of $len,
assign $len '<''s to a scalar variable?

  Sorry if this has already been answered already but I just started reading
this newsgroup.


Sys Admin, The HydroLab (292-6193)
177 Scott Hall,  1090 Carmack Dr.
Columbus, OH 43210



Wed, 14 Sep 1994 04:50:39 GMT  
 using eval to create formats

Quote:
>But if I try something like this it fails miserably:
>$_=$longsentence;
>$picture = '^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<';
>$variable = '$_';
>$filehandle = 'STDOUT';
>eval "format $filehandle =\n$variable\n\.\n";
>while($_){
>    write STDOUT  unless /^$/;
>}

Should that not have been:

        eval "format $filehandle = \n$picture\n$variable\n.\n"

Quote:
>  Also while I'm asking, what the quickest way given a line length of $len,
>assign $len '<''s to a scalar variable?

Try: $picture = '^' . '<' x $len;

--
Perl's Maternal Uncle
Mark Biggar



Wed, 14 Sep 1994 05:27:26 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. creating formats with eval

2. using eval to create sub definitions

3. Using format to create a line padded with spaces

4. Test for eval() without using eval() ??

5. Printing in format statments using e/g format

6. Using formats, way to avoid exact formatting???

7. formats inside an eval

8. eval and formats

9. create and eval code with CPP line controls?

10. (mutil level assoc arrays and dynamically created array names via eval)

11. Dynamically creating formats

12. (Q) How to create FORMAT on the fly?

 

 
Powered by phpBB® Forum Software