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

slurp, quine and context sensitivity

4 views
Skip to first unread message

Dan Kogai

unread,
Apr 8, 2006, 3:41:07 AM4/8/06
to perl6-l...@perl.org
Folks,

This is a poetic quine (or quine-maker) in perl5.

open myself, $0 and print <myself>;

The same thing in perl6 would be:

my $self = open $*PROGRAM_NAME; for =$self { say }

or

my $self = open $*PROGRAM_NAME; say for =$self;

or

my $self = slurp $*PROGRAM_NAME; print $self;

or even

$*PROGRAM_NAME.slurp.map:{ print };

The last example must not be

$*PROGRAM_NAME.slurp.print;

You can check it out by running the code below;

##
$*PROGRAM_NAME.slurp.print
##

Will print:

##$*PROGRAM_NAME.slurp.print##

This is because print() (and say()) expects list context and slurp()
passes an array of chomped strings. When it comes to autoboxing, the
context-sensitivity of builtins may bite you like this.

With that understood, I would welcome if we have a version of slurp()
which unconditionally returns a scalar. Say swallow()? Of course it
is as easy to implement as the following;

sub swallow($file){ my $content = slurp $file; return $content };

But like say(), I would love to have something like that as a builtin.

Or am I getting too much ruby recently?

Dan the Perl6 Addict

Damian Conway

unread,
Apr 8, 2006, 5:45:25 AM4/8/06
to Dan Kogai, perl6-l...@perl.org
Dan Kogai wrote:

> With that understood, I would welcome if we have a version of slurp()
> which unconditionally returns a scalar.

That'd be:

~slurp $file;

:-)

Damian

Dan Kogai

unread,
Apr 8, 2006, 6:34:20 AM4/8/06
to perl6-l...@perl.org

Very clever. But still not good enough when it comes to autoboxing.

{ ~slurp }($*PROGRAM_NAME).print

and even

(~slurp $*PROGRAM_NAME).print

works as expected but since "~slurp $file" is really ~(slurp $file),

$*PROGRAM_NAME.~slurp.print

does not. The problem of ~stringify, ?boolify, and +numify is that
they are infix operators so it goes the opposite direction.

Oh, while I was testing all these on pugs, I came across this.

##
say $*PROGRAM_NAME.slurp.elems;
##

This says 1 but

##
my @lines = $*PROGRAM_NAME.slurp; say @lines.elems;
##

says 3. This I am confused.

Dan the Perl6 Golfer on the Bunker

Dan Kogai

unread,
Apr 8, 2006, 6:36:39 AM4/8/06
to perl6-l...@perl.org
On Apr 08, 2006, at 19:34 , Dan Kogai wrote:
> does not. The problem of ~stringify, ?boolify, and +numify is that
> they are infix operators so it goes the opposite direction.
>

s/infix/prefix/

Sorry.

Damian Conway

unread,
Apr 8, 2006, 6:52:05 AM4/8/06
to Dan Kogai, perl6-l...@perl.org
Dan Kogai wrote:

>> ~slurp $file;


>
> Very clever. But still not good enough when it comes to autoboxing.
>
> { ~slurp }($*PROGRAM_NAME).print
>
> and even
>
> (~slurp $*PROGRAM_NAME).print
>
> works as expected but since "~slurp $file" is really ~(slurp $file),
>
> $*PROGRAM_NAME.~slurp.print
>
> does not.

You want:

(~$*PROGRAM_NAME.slurp).print;

Or:

$*PROGRAM_NAME.slurp(sep=>undef).print;


The problem of ~stringify, ?boolify, and +numify is that
> they are infix operators so it goes the opposite direction.
>
> Oh, while I was testing all these on pugs, I came across this.
>
> ##
> say $*PROGRAM_NAME.slurp.elems;
> ##

That call to .slurp is in scalar context, since the result is immediately used
as an object.

> This says 1 but
>
> ##
> my @lines = $*PROGRAM_NAME.slurp; say @lines.elems;
> ##
>
> says 3.

That .slurp is in list context.

Damian

0 new messages