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

•Re: mmv-like hack in perl?

by merlyn (Sage)
on Apr 14, 2003 at 01:42 UTC ( [id://250195]=note: print w/replies, xml ) Need Help??


in reply to mmv-like hack in perl?

This won't work:
my $pattern = q/(...)(...)/; my $replace = q/$2-$1/; s/$pattern/$replace/g;
because the $replace is inserted into the "double-quoted" string on the right side, and not again rescanned to see if it has any variables to be replaced. So you get a literal $1 and so on.

However, if you trust your invoker, you could try something like:

my $pattern = q/(...)(...)/; my $replace = q/$2-$1/; s/$pattern/qq{$replace}/eeg;
as long as the curly braces balance nicely.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: mmv-like hack in perl?
by mpd (Monk) on Apr 14, 2003 at 05:20 UTC
    Thank you for your help. I think I'm still having a problem with the line:

    s/$pattern/qq{$replace}/eeg;

    Here's the snippet that's failing:

    # get strings from GUI. my $old = my $new = $_->children->get; # get old filename my $pattern = $form->{'entry2'}->get_text(); # e.g. $pattern == "^(\d{2})\. (.*)$" after this my $replace = $form->{'entry1'}->get_text(); # e.g. $replace == "two-$2, one-$1" after this # is the following match necessary? $1, etc # need to be assigned somehow... if($old =~ /$pattern/) { $new =~ s/$pattern/qq{$replace}/eeg; print STDERR $old." => ".$new."\n"; }

    After the $new =~ line, $new is empty, every time. Am I missing something? Thanks again

      $new = $old; $new =~ s/$pattern/qq{$replace}/eeg;
      But you're scaring me here. I said that the string had to be from a trusted source. As soon as you start talking "GUI", that makes me think that the user-ID of the person typing the string is not necessarily the user-ID of the person running the code.

      Let me reemphasize. Using this code as-is will allow any arbitrary Perl code to be executed, including shelling out to execute arbitrary system commands. Clear? It's not just about mangling a string. You must either trust the invoker, or not use this code.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Perfectly clear. That's what I want to do, because this is just a toy for me to play with. I don't plan on releasing this piece of my junky code. I will likely end up parsing it myself as Aristotle suggests below. If something clever comes out, I will post that bit of code, of course.

        Thanks again.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found