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

Greetings Fellow Monks,

I have a small substitution problem. I've searched extensively on google, on the super search and tried numerous different test but I just can't get it right.

Let's say I have a text file that contains normal and special 'perl' characters. I would like to remove some detail...

Example of my file:

ZZ<<;ght;''d;\r\n\t\t\t\t\t;$_;LicBpack;'c*'y=eval;perl$@pop&&die@;D9

I would like to discard all the following:

y<<;''\r\n$_pack$_'c*'=eval@&&die

or if it's more readable:

y << ; '' \r \n $_ pack $_ 'c*' = eval @ && die

All suggestions welcome . . .

Replies are listed 'Best First'.
Re: String Substitution...
by BrowserUk (Patriarch) on May 16, 2008 at 00:59 UTC

    Does this produce the required result?

    $s = q[ZZ<<;ght;''d;\r\n\t\t\t\t\t;$_;LicBpack;'c*'y=eval;perl$@pop&&d +ie@;D9];; $s =~ s[$_][]g for map quotemeta, qw[ y << ; pack = eval @ && die], '\n', '\r', "'c*'", "''", '$_';; print $s;; ZZghtd\t\t\t\t\tLicBperl$popD9

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you to all!

      Your wisdom has enlightened me. I tested the other solutions and they seemed to work, but I went with the code from BrowserUk, slim-line and easy to tweak to my requirements.

      My big problem was just to avoid the "map quotameta" and special characters!

      I am a happy monk! :-)

Re: String Substitution...
by toolic (Bishop) on May 16, 2008 at 00:59 UTC
    Are you familiar with regular expressions? If not, read through perlre and perlreftut. Using the substitution operator is a common way to remove strings. This should get you started:
    use strict; use warnings; my $str = q{ZZ<<;ght;''d;\r\n\t\t\t\t\t;$_;LicBpack;'c*'y=eval;perl$@p +op&&die@;D9}; $str =~ s/\by\b//g; $str =~ s/\beval\b//g; $str =~ s/\bdie\b//g; $str =~ s/\\(n|r)//g; # alternation print $str, "\n";

    prints:

    ZZ<<;ght;''d;\t\t\t\t\t;$_;LicBpack;'c*'=;perl$@pop&&@;D9

    Give it a try. If you still can not solve your problem, post some code that you have tried, and ask detailed questions.

Re: String Substitution...
by graff (Chancellor) on May 16, 2008 at 05:49 UTC
    Is this the sort of result that you want?
    my @trash = qw/y << ; '' \r \n $_ pack 'c*' = eval @ && die/; my $src = qw/ZZ<<;ght;''d;\r\n\t\t\t\t\t;$_;LicBpack;'c*'y=eval;perl$@ +pop&&die@;D9/; for my $kill ( @trash ) { $src =~ s/\Q$kill\E//g; } print $src, "\n";
    The result I get when I run that is:
    ZZghtd\t\t\t\t\tLicBperl$popD9
    But it's quite possible that I don't understand your question.