[lug] Perl question: how to print embedded metacharacters

Tom Christiansen tchrist at perl.com
Wed Nov 25 10:44:19 MST 2009


Like, don't have a substr(<<EOF, -5, 3).

>--001636b2be36b22d07047934bb19
>Content-Type: text/html; charset=ISO-8859-1
>Content-Transfer-Encoding: quoted-printable

>Chip,<div><br></div><div>Yep, I'm using the shell to do the dirty work.=
>=A0<div><br></div><div><div>I don't doubt there are elegant, pure-Perl =
>solutions. =A0I was only trying to find something that would work for you, =
>immediately, until someone else volunteers a more PC (Perlishly Correct) wa=
>y. :-)<div>

><br><div class=3D"gmail_quote">On Wed, Nov 25, 2009 at 9:34 AM, Chip Atkins=
>on <span dir=3D"ltr"><<a href=3D"mailto:chip at pupman.com">chip at pupman.com=
></a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin=
>:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

>Hmmmm... that does work, but isn't it using the bash printf rather than=
><br>
>perl? =A0My concern is how fast it would be. =A0I can experiment there thou=
>gh,<br>
>and it does work.<br>
><br>
>Thanks for your help!<br>
><div><div></div><div class=3D"h5"><br>
>Chip<br>
><br>
>On Wed, 25 Nov 2009, Jeffrey Haemer wrote:<br>
><br>
>> Chip,<br>
>><br>
>> Okay. =A0Would something like this be good enough?<br>
>><br>
>><br>
>> #!/usr/bin/perl<br>
>><br>
>> while (<>) {<br>
>> =A0 $_=3D` printf "$_" `;<br>
>> =A0 print;<br>

    sub lame_printf { return scalar `perl -e 'printf("$_[0]", @_[1 .. $#_])'` }
    sub real_printf { return                 sprintf( $_[0] , @_[1 .. $#_])   }

    $mask = "%#08X";
    $moo = lame_printf($mask, 0xdeAdBeEf);
    $cow = real_printf($mask, 0xDEaDbEeF);

    print "lame is $moo\n";
    print "real is $cow\n";

} 
>> }<br>
>><br>
>><br>
>><br>
>><br>
>> On Wed, Nov 25, 2009 at 9:06 AM, Chip Atkinson <<a href=3D"mailto:c=
>hip at pupman.com">chip at pupman.com</a>> wrote:<br>
>><br>
>> > No, unfortunately.<br>
>> ><br>
>> > The problem is that the script reads the string in from the comma=
>nd file,<br>
>> > so I need to interpret the string before printing it. =A0Here&#39=
>;s another<br>
>> > example:<br>
>> > ---------------------<br>
>> > chip at chip-desktop:~$ cat <a href=3D"http://try2.pl" target=3D"_bl=
>ank">try2.pl</a><br>
>> > #!/usr/bin/perl<br>
>> ><br>
>> > #my $kw =3D 'abc\ndef';<br>
>> > #my $nv =3D "${\($kw)}";<br>
>> ><br>
>> > while (<>) {<br>
>> > =A0print "$_";<br>
>> > }<br>
>> > chip at chip-desktop:~$ echo 'abc\ndef' | ./<a href=3D"http:=
>//try2.pl" target=3D"_blank">try2.pl</a><br>
>> > abc\ndef<br>
>> > chip at chip-desktop:~$<br>
>> > ---------------------------------<br>
>> > In a sense, I need the opposite of quotemeta -- something that ta=
>kes in a<br>
>> > string and does the metacharacter replacements.<br>
>> ><br>
>> > I know that something like<br>
>> > while (<>) {<br>
>> > =A0s/\\n/\n/g;<br>
>> > =A0# etc. for all metacharacter sequences...<br>
>> > =A0print;<br>
>> > }<br>
>> ><br>
>> > would work but I wanted to avoid the "etc. for all metachara=
>cter<br>
>> > sequences"<br>
>> ><br>
>> > Hopefully that's more understandable.<br>
>> ><br>
>> > Chip<br>
>> ><br>
>> > On Wed, 25 Nov 2009, Jeffrey Haemer wrote:<br>
>> ><br>
>> > > Chip,<br>
>> > ><br>
>> > > Can you do this instead?<br>
>> > ><br>
>> > > my $kw =3D "abc\ndef";<br>
>> > ><br>
>> > > If not, can you explain in enough detail that I can try agai=
>n? :-)<br>
>> > ><br>
>> > > On Wed, Nov 25, 2009 at 8:29 AM, Chip Atkinson <<a href=
>=3D"mailto:chip at pupman.com">chip at pupman.com</a>> wrote:<br>
>> > ><br>
>> > > > Greetings,<br>
>> > > ><br>
>> > > > I'm working on a perl script that reads strings fro=
>m a configuration<br>
>> > file<br>
>> > > > and prints them out. =A0What I'd like to be able to=
> do is have any<br>
>> > embedded<br>
>> > > > metacharacters interpreted but I'd like to avoid do=
>ing the<br>
>> > substitutions<br>
>> > > > myself.<br>
>> > > ><br>
>> > > > For example, here is a little script to emulate the pro=
>blem:<br>
>> > > ><br>
>> > > > #!/usr/bin/perl<br>
>> > > ><br>
>> > > > my $kw =3D 'abc\ndef';<br>
>> > > ><br>
>> > > > print "$kw";<br>
>> > > > ----------------------<br>
>> > > > the output is<br>
>> > > > abc\ndef<br>
>> > > ><br>
>> > > > What I want is<br>
>> > > > abc<br>
>> > > > def<br>
>> > > ><br>
>> > > > --------------<br>
>> > > > I tried to jfgi but no joy.<br>
>> > > ><br>
>> > > > Thanks in advance.<br>
>> > > ><br>
>> > > > Chip<br>
>> > > ><br>
>> > > > _______________________________________________<br>
>> > > > Web Page: =A0<a href=3D"http://lug.boulder.co.us" targe=
>t=3D"_blank">http://lug.boulder.co.us</a><br>
>> > > > Mailing List: <a href=3D"http://lists.lug.boulder.co.us=
>/mailman/listinfo/lug" target=3D"_blank">http://lists.lug.boulder.co.us/mai=
>lman/listinfo/lug</a><br>
>> > > > Join us on IRC: <a href=3D"http://lug.boulder.co.us" ta=
>rget=3D"_blank">lug.boulder.co.us</a> port=3D6667 channel=3D#hackingsociety=
><br>
>> > > ><br>
>> > ><br>
>> > ><br>
>> > ><br>
>> > > --<br>
>> > > Click to Call Me Now! --<br>
>> > > <a href=3D"http://seejeffrun.blogspot.com/2009/09/call-me-no=
>w.html" target=3D"_blank">http://seejeffrun.blogspot.com/2009/09/call-me-no=
>w.html</a><br>
>> > ><br>
>> > > Jeffrey Haemer <<a href=3D"mailto:jeffrey.haemer at gmail.co=
>m">jeffrey.haemer at gmail.com</a>><br>
>> > > 720-837-8908 [cell], =A0 at goyishekop [twitter]<br>
>> > > <a href=3D"http://www.youtube.com/user/goyishekop" target=3D=
>"_blank">http://www.youtube.com/user/goyishekop</a> [vlog]<br>
>> > ><br>
>> ><br>
>> > _______________________________________________<br>
>> > Web Page: =A0<a href=3D"http://lug.boulder.co.us" target=3D"_blan=
>k">http://lug.boulder.co.us</a><br>
>> > Mailing List: <a href=3D"http://lists.lug.boulder.co.us/mailman/l=
>istinfo/lug" target=3D"_blank">http://lists.lug.boulder.co.us/mailman/listi=
>nfo/lug</a><br>
>> > Join us on IRC: <a href=3D"http://lug.boulder.co.us" target=3D"_b=
>lank">lug.boulder.co.us</a> port=3D6667 channel=3D#hackingsociety<br>
>> ><br>
>><br>
>><br>
>><br>
>> --<br>
>> Click to Call Me Now! --<br>
>> <a href=3D"http://seejeffrun.blogspot.com/2009/09/call-me-now.html" ta=
>rget=3D"_blank">http://seejeffrun.blogspot.com/2009/09/call-me-now.html</a>=
><br>
>><br>
>> Jeffrey Haemer <<a href=3D"mailto:jeffrey.haemer at gmail.com">jeffrey=
>.haemer at gmail.com</a>><br>
>> 720-837-8908 [cell], =A0 at goyishekop [twitter]<br>
>> <a href=3D"http://www.youtube.com/user/goyishekop" target=3D"_blank">h=
>ttp://www.youtube.com/user/goyishekop</a> [vlog]<br>
>><br>
><br>
>_______________________________________________<br>
>Web Page: =A0<a href=3D"http://lug.boulder.co.us" target=3D"_blank">http://=
>lug.boulder.co.us</a><br>
>Mailing List: <a href=3D"http://lists.lug.boulder.co.us/mailman/listinfo/lu=
>g" target=3D"_blank">http://lists.lug.boulder.co.us/mailman/listinfo/lug</a=
>><br>
>Join us on IRC: <a href=3D"http://lug.boulder.co.us" target=3D"_blank">lug.=
>boulder.co.us</a> port=3D6667 channel=3D#hackingsociety<br>
></div></div></blockquote></div><br><br clear=3D"all"><br>-- <br>Click to Ca=
>ll Me Now! -- <a href=3D"http://seejeffrun.blogspot.com/2009/09/call-me-now=
>.html">http://seejeffrun.blogspot.com/2009/09/call-me-now.html</a><br><br>

>Jeffrey Haemer <<a href=3D"mailto:jeffrey.haemer at gmail.com">jeffrey.haem=
>er at gmail.com</a>><br>720-837-8908 [cell], =A0 at goyishekop [twitter]<br><a=
> href=3D"http://www.youtube.com/user/goyishekop">http://www.youtube.com/use=
>r/goyishekop</a> [vlog]<br>

><br><br>
></div></div></div></div>

>--001636b2be36b22d07047934bb19--

print chr(sprintf "%x" => 027*0b11) x (0b1<<0x1) => "\cW\c@\cZ" ^ "cow"
EOF



More information about the LUG mailing list