Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string escape function available?

15 views
Skip to first unread message

Patrick R. Michaud

unread,
Nov 21, 2004, 11:35:11 AM11/21/04
to perl6-i...@perl.org

Is there some opcode or function built into the standard parrot
distribution that will print a string with appropriate escapes
around special characters (i.e., display newlines as '\n',
returns as '\r', non-printable characters as '\xnn' or '\nnn', etc.)?

I can certainly write a function to do this, but would be very glad
to take advantage of any that's already available.

Thanks,

Pm

William Coleda

unread,
Nov 21, 2004, 12:49:35 PM11/21/04
to Patrick R. Michaud, perl6-i...@perl.org
Data::Escape is probably your best bet.


.sub _main

load_bytecode "runtime/parrot/library/Data/Escape.pbc"
.local pmc escaper
escaper = find_global "Data::Escape", "String"

$S0 = "\n"
$S1 = escaper($S0, "\"")
print $S1
print $S0

end
.end

Leopold Toetsch

unread,
Nov 21, 2004, 1:48:03 PM11/21/04
to William Coleda, perl6-i...@perl.org
William Coleda <wi...@coleda.com> wrote:
> Data::Escape is probably your best bet.


> .sub _main

> load_bytecode "runtime/parrot/library/Data/Escape.pbc"
^^^^^^^^^^^^^^^

Just drop that prefix - "library/foo" ought to do it.

leo

Patrick R. Michaud

unread,
Nov 21, 2004, 4:43:30 PM11/21/04
to Leopold Toetsch, William Coleda, perl6-i...@perl.org
On Sun, Nov 21, 2004 at 07:48:03PM +0100, Leopold Toetsch wrote:
> William Coleda <wi...@coleda.com> wrote:
> > Data::Escape is probably your best bet.

Good bet, thanks.

> > .sub _main
> > load_bytecode "runtime/parrot/library/Data/Escape.pbc"
> ^^^^^^^^^^^^^^^
> Just drop that prefix - "library/foo" ought to do it.

On my machine 'library/foo' doesn't seem to be sufficient -- I have
to provide a complete path (relative or absolute) to the resource.
Or is there some other environment variable or the like that I should
have set?

[pmichaud@localhost parrot]$ cat esc.pir
.sub _main
load_bytecode 'library/Data/Escape.pbc'
$P0 = find_global "Data::Escape", "String"

$S0 = $P0("hello\nthere\n", '"')
print $S0
print "\n"
.end
[pmichaud@localhost parrot]$ parrot esc.pir
Parrot VM: Can't stat library/Data/Escape.pbc, code 2.
Segmentation fault
[pmichaud@localhost parrot]$ uname -a
Linux localhost.localdomain 2.4.20-31.9 #1 Tue Apr 13 18:04:23 EDT 2004 i686 i686 i386 GNU/Linux


Pm

Leopold Toetsch

unread,
Nov 22, 2004, 6:12:12 AM11/22/04
to Patrick R. Michaud, perl6-i...@perl.org
Patrick R. Michaud <pmic...@pobox.com> wrote:
> On Sun, Nov 21, 2004 at 07:48:03PM +0100, Leopold Toetsch wrote:
>> William Coleda <wi...@coleda.com> wrote:

>> > .sub _main
>> > load_bytecode "runtime/parrot/library/Data/Escape.pbc"
>> ^^^^^^^^^^^^^^^
>> Just drop that prefix - "library/foo" ought to do it.

> On my machine 'library/foo' doesn't seem to be sufficient -- I have
> to provide a complete path (relative or absolute) to the resource.

Argh, yes. Loading bytecode didn't search the library paths. Fixed.

BTW still patches wanted that check the filename, if it's absolute.

> Pm

leo

0 new messages