[lug] Perl question: how to print embedded metacharacters
Matthew Beldyk
matt at beldyk.org
Wed Nov 25 10:07:03 MST 2009
More Perlically Correct: (security considerations aside, I basically
run the perl interpreter on the input and treat it as code again) Just
make sure you do lots of input checking with this program.
[09:57:25] beldyk at chupacabra: ~
$ cat tmp.pl
#!/usr/bin/perl -w
my $str = <<EOS;
foo\nbar\tfoo\a\t
EOS
my $nstr;
eval("\$nstr = \"$str\";");
print $nstr;
[09:57:33] beldyk at chupacabra: ~
$ ./tmp.pl
foo
bar foo
On Wed, Nov 25, 2009 at 9:45 AM, Jeffrey Haemer
<jeffrey.haemer at gmail.com> wrote:
> It didn't take much thought to realize I should have written "Perlitically
> Correct."
>
> On Wed, Nov 25, 2009 at 9:41 AM, Jeffrey Haemer <jeffrey.haemer at gmail.com>
> wrote:
>>
>> Chip,
>> Yep, I'm using the shell to do the dirty work.
>> I don't doubt there are elegant, pure-Perl solutions. I was only trying
>> to find something that would work for you, immediately, until someone else
>> volunteers a more PC (Perlishly Correct) way. :-)
>> On Wed, Nov 25, 2009 at 9:34 AM, Chip Atkinson <chip at pupman.com> wrote:
>>>
>>> Hmmmm... that does work, but isn't it using the bash printf rather than
>>> perl? My concern is how fast it would be. I can experiment there
>>> though,
>>> and it does work.
>>>
>>> Thanks for your help!
>>>
>>> Chip
>>>
>>> On Wed, 25 Nov 2009, Jeffrey Haemer wrote:
>>>
>>> > Chip,
>>> >
>>> > Okay. Would something like this be good enough?
>>> >
>>> >
>>> > #!/usr/bin/perl
>>> >
>>> > while (<>) {
>>> > $_=` printf "$_" `;
>>> > print;
>>> > }
>>> >
>>> >
>>> >
>>> >
>>> > On Wed, Nov 25, 2009 at 9:06 AM, Chip Atkinson <chip at pupman.com> wrote:
>>> >
>>> > > No, unfortunately.
>>> > >
>>> > > The problem is that the script reads the string in from the command
>>> > > file,
>>> > > so I need to interpret the string before printing it. Here's another
>>> > > example:
>>> > > ---------------------
>>> > > chip at chip-desktop:~$ cat try2.pl
>>> > > #!/usr/bin/perl
>>> > >
>>> > > #my $kw = 'abc\ndef';
>>> > > #my $nv = "${\($kw)}";
>>> > >
>>> > > while (<>) {
>>> > > print "$_";
>>> > > }
>>> > > chip at chip-desktop:~$ echo 'abc\ndef' | ./try2.pl
>>> > > abc\ndef
>>> > > chip at chip-desktop:~$
>>> > > ---------------------------------
>>> > > In a sense, I need the opposite of quotemeta -- something that takes
>>> > > in a
>>> > > string and does the metacharacter replacements.
>>> > >
>>> > > I know that something like
>>> > > while (<>) {
>>> > > s/\\n/\n/g;
>>> > > # etc. for all metacharacter sequences...
>>> > > print;
>>> > > }
>>> > >
>>> > > would work but I wanted to avoid the "etc. for all metacharacter
>>> > > sequences"
>>> > >
>>> > > Hopefully that's more understandable.
>>> > >
>>> > > Chip
>>> > >
>>> > > On Wed, 25 Nov 2009, Jeffrey Haemer wrote:
>>> > >
>>> > > > Chip,
>>> > > >
>>> > > > Can you do this instead?
>>> > > >
>>> > > > my $kw = "abc\ndef";
>>> > > >
>>> > > > If not, can you explain in enough detail that I can try again? :-)
>>> > > >
>>> > > > On Wed, Nov 25, 2009 at 8:29 AM, Chip Atkinson <chip at pupman.com>
>>> > > > wrote:
>>> > > >
>>> > > > > Greetings,
>>> > > > >
>>> > > > > I'm working on a perl script that reads strings from a
>>> > > > > configuration
>>> > > file
>>> > > > > and prints them out. What I'd like to be able to do is have any
>>> > > embedded
>>> > > > > metacharacters interpreted but I'd like to avoid doing the
>>> > > substitutions
>>> > > > > myself.
>>> > > > >
>>> > > > > For example, here is a little script to emulate the problem:
>>> > > > >
>>> > > > > #!/usr/bin/perl
>>> > > > >
>>> > > > > my $kw = 'abc\ndef';
>>> > > > >
>>> > > > > print "$kw";
>>> > > > > ----------------------
>>> > > > > the output is
>>> > > > > abc\ndef
>>> > > > >
>>> > > > > What I want is
>>> > > > > abc
>>> > > > > def
>>> > > > >
>>> > > > > --------------
>>> > > > > I tried to jfgi but no joy.
>>> > > > >
>>> > > > > Thanks in advance.
>>> > > > >
>>> > > > > Chip
>>> > > > >
>>> > > > > _______________________________________________
>>> > > > > Web Page: http://lug.boulder.co.us
>>> > > > > Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>>> > > > > Join us on IRC: lug.boulder.co.us port=6667
>>> > > > > channel=#hackingsociety
>>> > > > >
>>> > > >
>>> > > >
>>> > > >
>>> > > > --
>>> > > > Click to Call Me Now! --
>>> > > > http://seejeffrun.blogspot.com/2009/09/call-me-now.html
>>> > > >
>>> > > > Jeffrey Haemer <jeffrey.haemer at gmail.com>
>>> > > > 720-837-8908 [cell], @goyishekop [twitter]
>>> > > > http://www.youtube.com/user/goyishekop [vlog]
>>> > > >
>>> > >
>>> > > _______________________________________________
>>> > > Web Page: http://lug.boulder.co.us
>>> > > Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>>> > > Join us on IRC: lug.boulder.co.us port=6667 channel=#hackingsociety
>>> > >
>>> >
>>> >
>>> >
>>> > --
>>> > Click to Call Me Now! --
>>> > http://seejeffrun.blogspot.com/2009/09/call-me-now.html
>>> >
>>> > Jeffrey Haemer <jeffrey.haemer at gmail.com>
>>> > 720-837-8908 [cell], @goyishekop [twitter]
>>> > http://www.youtube.com/user/goyishekop [vlog]
>>> >
>>>
>>> _______________________________________________
>>> Web Page: http://lug.boulder.co.us
>>> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>>> Join us on IRC: lug.boulder.co.us port=6667 channel=#hackingsociety
>>
>>
>>
>> --
>> Click to Call Me Now! --
>> http://seejeffrun.blogspot.com/2009/09/call-me-now.html
>>
>> Jeffrey Haemer <jeffrey.haemer at gmail.com>
>> 720-837-8908 [cell], @goyishekop [twitter]
>> http://www.youtube.com/user/goyishekop [vlog]
>>
>>
>
>
>
> --
> Click to Call Me Now! --
> http://seejeffrun.blogspot.com/2009/09/call-me-now.html
>
> Jeffrey Haemer <jeffrey.haemer at gmail.com>
> 720-837-8908 [cell], @goyishekop [twitter]
> http://www.youtube.com/user/goyishekop [vlog]
>
>
>
> _______________________________________________
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#hackingsociety
>
--
Calvin: Know what I pray for?
Hobbes: What?
Calvin: The strength to change what I can, the inability to accept
what I can't, and the incapacity to tell the difference.
More information about the LUG
mailing list