Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Treating a variable as a variable in a regular expression?

by parkinglot (Initiate)
on Jul 08, 2014 at 12:45 UTC ( [id://1092715]=perlquestion: print w/replies, xml ) Need Help??

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

My problem is as follows: I have the lines:

$function_used = "memcpy"; $function_call = "memcpy(a, b, sizeof(a));" $function_call =~ s/$function_used//g;

The problem is the $function_used isnt being treated as a variable (as you know). So how can one solve this? !!!!!!!!!!!! I solved this by removing the global identifier. !!!!!!!!!!!!

Replies are listed 'Best First'.
Re: Treating a variable as a variable in a regular expression?
by AnomalousMonk (Archbishop) on Jul 08, 2014 at 13:45 UTC
    I solved this by removing the global identifier. !!!!!!!!!!!!

    No, you solved it by making the incomplete statement
        $function_call = "memcpy(a, b, sizeof(a));"
    (no ; terminates the statement) into a complete statement (by adding a ; and then not mentioning it to anyone, possibly even including yourself ;).

    c:\@Work\Perl>perl -wMstrict -le "my $function_used = 'memcpy'; my $function_call = 'memcpy(a, b, sizeof(a));'; $function_call =~ s/$function_used//g; print qq{'$function_call'}; " '(a, b, sizeof(a));'

    (I'm assuming that by 'problem', you mean the fact that the OPed code will not compile.)

    Update: If there's any chance that  $function_used may contain regex metacharacters, it's a very good idea to use quotemeta on it at some point, e.g.:
        $function_call =~ s/\Q$function_used\E//g;

Re: Treating a variable as a variable in a regular expression?
by LanX (Saint) on Jul 08, 2014 at 13:37 UTC
    > $function_used isnt being treated as a variable (as you know)

    Nope, I doubt this! Even without being able to test the code you posted. (did you?)

    memcpy should be deleted now.

    My best guess is that in your real code $function_used doesn't contain what you think it should.

    Maybe a problem with scoping?

    > So how can one solve this? !!!!!!!!!!!! I solved this by removing the global identifier. !!!!!!!!!!!!

    Your keyboard is broken, maybe that's the problem?(...??????????)

    :)

    Cheers Rolf

    (addicted to the Perl Programming Language)

    update

    now I can test, the code you posted explicitly informs you about the wrong semicolon:

    > perl $function_used = "memcpy"; $function_call = "memcpy(a, b, sizeof(a));" $function_call =~ s/$function_used//g; Scalar found where operator expected at - line 3, near "$function_call +" (Missing semicolon on previous line?) syntax error at - line 3, near "$function_call " Execution of - aborted due to compilation errors.

    and the corrected code doesn't have any of your imagined "problems"

    > perl $function_used = "memcpy"; $function_call = "memcpy(a, b, sizeof(a))"; $function_call =~ s/$function_used//g; print $function_call; __END__ (a, b, sizeof(a))

    so I think you should better take a look into How (Not) To Ask A Question :)

Re: Treating a variable as a variable in a regular expression?
by InfiniteSilence (Curate) on Jul 08, 2014 at 13:13 UTC
    perl -e 'sub foo {return qq|abracadabra|}; my $line = qq|hi there: sub +|; if($line=~s/sub/&foo/eg){print $line};'

    Prints

    hi there: abracadabra

    Celebrate Intellectual Diversity

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found