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

Trying to make a new operator

6 views
Skip to first unread message

Richard Hainsworth

unread,
Sep 17, 2006, 8:10:58 AM9/17/06
to perl6...@perl.org
I tried to make a new "growth" operator.
The code was

(32 + 48).say;

sub infix:<+> ($left,$right) {
return 100 * ($right/$left -1);
};

$pugs ./p6test.p6

returns with
50

But if I change the + character to (say) a cyrillic letter Д, I get the
following error:
$ pugs ./p6test.p6
***
unexpected "\1041"
expecting operator or ")"
at ./p6test.p6 line 1, column 5


I also tried other latin characters, but I get errors. It seems I can
change an existing operator, but not introduce another.

What am I doing wrong?

Daniel Hulme

unread,
Sep 17, 2006, 11:35:39 AM9/17/06
to perl6...@perl.org
> What am I doing wrong?
Sounds like you need to define (or at least declare) the new operator
before you use it. Perl 6, like Perl 5 compiles with a single pass, so
when you are using your random operator, it hasn't yet read the
declaration further down the file.

It does its best to DTRT with most functions, but when you're defining
arbitrarily random operators, it can only be so clever.

--
Sufficiently advanced humour is indistinguishable from tedium.
corollary:
Humour distinguishable from tedium is insufficiently advanced.
http://surreal.istic.org/ Hack code until it works, then stop.

Yuval Kogman

unread,
Sep 17, 2006, 1:07:30 PM9/17/06
to Daniel Hulme, perl6...@perl.org
On Sun, Sep 17, 2006 at 16:35:39 +0100, Daniel Hulme wrote:
> > What am I doing wrong?
> Sounds like you need to define (or at least declare) the new operator
> before you use it. Perl 6, like Perl 5 compiles with a single pass, so
> when you are using your random operator, it hasn't yet read the
> declaration further down the file.

s/use/parse/;

--
Yuval Kogman <nothi...@woobling.org>
http://nothingmuch.woobling.org 0xEBD27418

Richard Hainsworth

unread,
Sep 20, 2006, 10:31:37 AM9/20/06
to Yuval Kogman, Daniel Hulme, perl6...@perl.org
Thanks for help. For anyone else, the following works.

sub infix:<grew_by_to> {...};

(32 grew_by_to 48).say;

sub infix:<grew_by_to> ($left, $right) {
return ($right/$left - 1) * 100 ~ '%';
};

Steffen Schwigon

unread,
Sep 20, 2006, 2:31:17 PM9/20/06
to perl6...@perl.org
Richard Hainsworth <ric...@rusrating.ru> writes:
> Thanks for help. For anyone else, the following works.
>
> sub infix:<grew_by_to> {...};
>
> (32 grew_by_to 48).say;
>
> sub infix:<grew_by_to> ($left, $right) {
> return ($right/$left - 1) * 100 ~ '%';
> };

Thanks for reporting the solution back.

And it even works with unicode operators. Looks like we finally
really get our "ankh, pentagram, and that smiley teddy bear from
that Grateful Dead album". (*) :-) Thanks to Unicode, thanks to Pugs.

(*) The senior ones of us possibly remember the good old days:
http://www.perl.org/yapc/2002/movies/themovie/

GreetinX
Steffen
--
Steffen Schwigon <schw...@webit.de>
Dresden Perl Mongers <http://dresden-pm.org/>

Richard Hainsworth

unread,
Sep 22, 2006, 11:12:04 AM9/22/06
to perl6...@perl.org
Steffen Schwigon wrote:
> <snip>

> Thanks for reporting the solution back.
>
> And it even works with unicode operators. Looks like we finally
> really get our "ankh, pentagram, and that smiley teddy bear from
> that Grateful Dead album". (*) :-) Thanks to Unicode, thanks to Pugs
>
So hopefully in the same spirit :

sub infix:<☥> {...};
sub infix:<☆> {...};
sub infix:<☺> {...};

(3 ☥ 40 ☆ 7 ☺ 50).say;

sub infix:<☥> ($l,$r) {
<The > ~ $l ~ < wise men spent > ~ $r ~ < days >
};

sub infix:<☆> ($l,$r) {
$l ~ < following a star in the > ~ $r ~ <-th heaven >
};

sub infix:<☺> ($l,$r) {
$l ~ < to become very happy for > ~ $r ~ < days and nights>
};


Biggest problems are the following:
a) finding the symbols - I had to use two editors, and getting them to
show them on screen
b) mixing types. My version of pugs does not respect the white space at
the start and end of strings. Also if the functions are given as <<text
$l more text>>, all the white space is excluded. Not sure if this is a
feature or a non-feature.

Richard

Markus Laire

unread,
Sep 22, 2006, 11:56:47 AM9/22/06
to perl6...@perl.org
On 9/22/06, Richard Hainsworth <ric...@rusrating.ru> wrote:
> Biggest problems are the following:
> a) finding the symbols - I had to use two editors, and getting them to
> show them on screen

Good place to see all of the symbols in Unicode is
http://unicode.org/charts/symbols.html (a lot of PDF-files there)

You could also try to find the proper symbol from
http://www.iam.uni-bonn.de/~alt/html/unicode_3.html
and then copy-pasting it to your editor.
This seemed to work for me with Firefox+KWrite after I changed the
encoding to UTF-8 (but my font didn't show all the symbols).

> b) mixing types. My version of pugs does not respect the white space at
> the start and end of strings. Also if the functions are given as <<text
> $l more text>>, all the white space is excluded. Not sure if this is a
> feature or a non-feature.

< text more text > creates a list, not a string, and is meant to
ignore the extra spaces because spaces are only used to delimit the
list-items.
(I'm not sure about <<...>>)

If you want to create strings, just use the quotes like here:

sub infix:<☥> {...};
sub infix:<☆> {...};
sub infix:<☺> {...};

(3 ☥ 40 ☆ 7 ☺ 50).say;

sub infix:<☥> ($l,$r) {
return "The $l wise men spent $r days";
};

sub infix:<☆> ($l,$r) {
return "$l following a star in the $r-th heaven";
};

sub infix:<☺> ($l,$r) {
return "$l to become very happy for $r days and nights"
};

--
Markus Laire

0 new messages