Question: I've a question related to a very basic thing in Perl, but I'm unable to find an efficient solution.

Here's a bit of context first. I use [Net::Pcap](http://search.cpan.org/perldoc/Net::Pcap) etc and when I'm in my function which processes packets (used by `pcap_loop`) I get a `$packet` scalar which contains my whole packet (ethernet header + ip header + tcp/udp header + payload).

What I want to do is to change the first 6 bytes of this `$packet` (the ethernet destination) in order to get a `$packet` that I can send using `pcap_sendpacket`, using a user defined destination mac address (for example passed as a command line argument or so), such as `00:11:22:33:44:55`. So I can split the user defined address (using `split` for example) to get every 6 parts of the destination mac address, and convert it to hex using the `hex` function, but now I want to modify the first bytes of my packet to replace them with these `hex`ed bytes. How should I proceed ?

I thought using some concatenation (.) but I think that this solution is dirty.

Thinking in C (because I did it in C some time ago, but I want this to be done in Perl), Once I got my "`u_char packet[]`", I just had to do a `memcpy` of my user-supplied ethernet address to the 6 first bytes of my `packet[]` and it worked.

Answer: 
Have a look at the description of [`substr()`](http://perldoc.perl.org/functions/substr.html) in [`perldoc perlfunc`](http://perldoc.perl.org/perlfunc.html). It has four-parameter form that lets you replace parts of a scalar:

```
substr EXPR, OFFSET, LENGTH, REPLACEMENT

```

So something like this should work for you:

```
substr $packet, 0, 6, $new_header

```
```
tcprewrite --enet-dmac=00:07:0d:0b:98:00 -i orignalmac.pcap -o newmac.pcap

```