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

I'm going to develop a module that includes Variables in Strings. They must come from different files, because permissions are important. I wanted to use the ref in @INC mechanism, because I want the power of perl. But I must restrict it to the user. So the module makes checks on the source. But it fails with a mysterious ^P in the source. Only if I use -Dt on the Interpreter it doesn't fail. Here is a patch archive:
diff -uP null/com xxx/com --- null/com Thu Jan 1 01:00:00 1970 +++ xxx/com Sun Mar 2 18:51:51 2003 @@ -0,0 +1,12 @@ + 507 debugperl -DptT -we 'require "t.pl"' -Mwr=t.pl 2>&1|cat >ptT + 508 debugperl -DpT -we 'require "t.pl"' -Mwr=t.pl 2>&1|cat >pT + 509 grep -v '^\(' ptT|less + 510 grep -v '^(' ptT|less + 511 less pT + 512 grep -v '^(' ptT|diff - pT|less + 513 grep -v '^(' ptT|diff pT - |less + 514 man diff + 515 grep -v '^(' ptT|diff -yu pT - |less + 516 grep -v '^(' ptT|diff -y pT - |less + 517 grep -v '^(' ptT|diff -y pT - |less>err + 518 history>com diff -uP null/t.pl xxx/t.pl --- null/t.pl Thu Jan 1 01:00:00 1970 +++ xxx/t.pl Fri Feb 28 10:10:03 2003 @@ -0,0 +1,11 @@ +'dudu' +<HTML> +<HEAD> + <TITLE>Testseite</TITLE> +</HEAD> +<BODY> + TesT!!Test!TEST!!${u} +</BODY> +$@; +</HTML> +</HTML> diff -uP null/wr.pm xxx/wr.pm --- null/wr.pm Thu Jan 1 01:00:00 1970 +++ xxx/wr.pm Fri Feb 28 13:36:06 2003 @@ -0,0 +1,66 @@ +package wr; +sub import{ + shift; + my $sub=shift; + if(ref$sub){ + $@='@'; + print $sub->()."</HTML>\n"; + }else{ + unshift @::INC,($sub=[sub{ + #print @_; + my $me=shift;#print @$me; + my $file=shift; + return if $file ne $me->[1]; + my $ret=""; + for my $p (@::INC){ + $ret=1,next if$p eq$me; + next unless $ret; + my$et; + open $et,"<","$p/$file" and $ret=$et,last;#print "$p/$file$! +\n"; + }#print"---$!",$ret; + return unless ref$ret;#print "+++$file"; + return(sub{ + my $m=$_[1]; + return 0 unless defined $m->[1]; + my$c=3; + while(length($_=$m->[1])||$c--){ + my $n=$_[0]; + my $r=$m->[3]->($m->[4]); + if($n){ + if($n<=length){ + $n=substr$_,0,$n; + $_=substr$_,length$n; + ($_,$n)=($n,$_); + $m->[1]=$n;print"§"; + return length; + } + }else{ + if($n=1+index($_,"\n")){ + $n=substr$_,0,$n; + $_=substr$_,length$n; + ($_,$n)=($n,$_); + $m->[1]=$n;print; + return length; + } + } + undef$m->[0]unless ref$r; + $m->[2]=~s/($r)//; + $m->[1].=$1; + next if length $1; + return 0 unless$n=$m->[0]; + $m->[2].=<$n>; + } + return 0; + },[$ret,'use '.__PACKAGE__. + ' sub{package al;<<"</HTML>"},','',$me->[2]]) + },$sub,sub{local$;='$;';s{^(</HTML>)$}{$1\n;return$;;\n}#and retu +rn'${' + ;return qr/([^\\\$@]|[a]|\$([@"]|\{[a-zA-Z0-9]+\}))*/m +s;}]); + return $sub; + } +} +1; + + + + +
By the way: I use the perl of the debian woody distribution.

Replies are listed 'Best First'.
Re: ref in @INC and debugging
by IlyaM (Parson) on Mar 07, 2003 at 17:04 UTC
    I tried to decypher your code but I give up. It is simply unreadable mess. I doubt you will understand it yourself after a week. Some hints:
    1. Use strict and warnings
    2. Use human readable variable names. When I see $n, $m, etc it say me nothing.
    3. Split large code chunks on subroutines with human readable names.
    4. Don't use exotic Perl features just because you can (i.e. code refs in @INC to convert templating language in Perl code on fly). There are much simplier ways to do the same thing (compile template in Perl code). Take a look on existing templaiting modules to see how. BTW probably they already do what you want or can be easily extended to do it.
    5. If you need to restrict power of Perl expressions you need to use Safe. Parsing Perl code is tricky.

    --
    Ilya Martynov, ilya@iponweb.net
    CTO IPonWEB (UK) Ltd
    Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
    Personal website - http://martynov.org

      Zu 4. It's not because I can. It's because I can't. Install modules I mean. (Pure perl is okay. But counts against mem)
        Don't use Perl if your mem is that restricted. Use C or ASM. Or, talk to your sysadmin to get the appropriate modules installed. Some jobs just can't be done, given enough restrictions.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: ref in @INC and debugging
by robartes (Priest) on Mar 07, 2003 at 14:33 UTC
    I'm afraid you'll have to be a bit more clear to get an answer. What exactly do you want to do? Is it working, and if not, why not? Do you have example code?

    CU
    Robartes-

Re: ref in @INC and debugging
by Tomte (Priest) on Mar 07, 2003 at 14:35 UTC

    What exactly is your problem?

    Reconsider your Problem; Restate it in an understandable way and present readable code.
    I'm pretty sure you will gain more rewarding answers then my slightly pis**d off remark.

    kind regards,
    tomte


Re: ref in @INC and debugging
by Anonymous Monk on Mar 07, 2003 at 16:09 UTC
    That code is the full archive of all I have along this line so far. I couldn't upload it in tar-format. So I used patch. To unpack patch an empty directory with this code. There is wr.pm, the code I'm speaking about. Then there is t.pl, an examplefile. Then there is com, the commands I tried. Everytime I turn debugging on with -Dt (and only then) it runs. If not, it doesn't compile.
      As I understand there are only three files. You could just inline them in your post. Diff format doesn't make your post easy reading.

      --
      Ilya Martynov, ilya@iponweb.net
      CTO IPonWEB (UK) Ltd
      Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
      Personal website - http://martynov.org

        But a better download!