Re: Indenting

2001-10-16 Thread Rafael Garcia-Suarez

Excuse me for dropping this into the discussion, but
this reminds me a proposal made (by me;-) in p5p last month :
define a new prototype (") that allows to define quotelike
functions. Example :
sub rot13 (") {
my $s = shift; $s =~ tr/A-Za-z/N-ZM-An-zm-A/; return $s;
}
print rot13/uryyb $jbeyq\n/;
# the previous statement being equivalent to "print rot13(qq/uryyb
# $jbeyq\n/);" if rot13 had a ($) prototype

See the threads:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-09/msg01718.html
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-09/msg01800.html

Aaron Sherman wrote in perl.perl6.language:
} On Tue, Oct 16, 2001 at 09:30:44AM -0700, David Wheeler wrote:
} > 
} > That's part of the reason that I almost never use here docs, but the
} > qq{} operator, instead. No need for a closing newline.
} 
} I have not read the RFC, but I do agree that qq is the way to go for
} formatted content. Or, perhaps something like it that in turn 
} resembles the s{}{} operator?
} 
}   qf{^\s+}{   Now is the time
}   for all good men...};
} 
} eq
} 
}   sub qf ($pat,$str) {
}   $str =~ s/$pat//mg;
}   return $str;
}   }
}   qf('^\s+', qq{  Now is the time
}   for all good men...});
} 
} eq
} 
}   qq{Now is the time\nfor all good men...};
} 
} That fits with the current trend and gives us a very flexible
} way to deal with just about any sort of formatting.
} 
} You would want to allow the usual m{} options after the second
} close, but I think the default behavior should be that of m{}mg
} 
} I also think that the above should be abbreviated as:
} 
}   qf{}{   Now is the time
}   for all good men...};
} 
} since useful defaults are always good.

-- 
Rafael Garcia-Suarez



Re: Indenting

2001-10-16 Thread Michael G Schwern

On Tue, Oct 16, 2001 at 08:31:31PM -0700, David Whipp wrote:
> > Is chomp? just a bad example, or is there some utility in asking if a
> > string has already been chomped?
> 
> The query is asking what the string would look like, if it were chomped.

That's a weird use of a query method.  In fact, that sounds like a
normal method call.  Applied to some other situations:

# "What would @list look like if it were sorted"
@sorted_list = @list.sort?

# "What would this filehandle look like if it were open"
$open_fh = $fh.open?

I expect query methods to be simple yes/no questions that return
true/false.  @list.sort? should return whether or not the @list were
sorted (not that this is particularly useful) and $fh.open? to tell me
if the filehandle is open or not.

I think the function/method distinction is a better seperation between
"what would this thing look like if it were acted on" and "act on this
thing".

# What would this look like if it were sorted?
@sorted_list = sort @list;

# Sort this list
@list.sort


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>http://www.pobox.com/~schwern/
Perl6 Quality Assurance <[EMAIL PROTECTED]>   Kwalitee Is Job One
Me? A robot? That's rediculous! For one thing, that doesn't compute at all!



RE: Indenting

2001-10-16 Thread David Whipp

> Is chomp? just a bad example, or is there some utility in asking if a
> string has already been chomped?

The query is asking what the string would look like, if it were chomped.


Dave.



Re: Indenting

2001-10-16 Thread Michael G Schwern

On Tue, Oct 16, 2001 at 08:53:07PM -0500, David M. Lloyd wrote:
> What about 'chomp?' for query but 'chomp' (no decoration) for operation?

Is chomp? just a bad example, or is there some utility in asking if a
string has already been chomped?


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>http://www.pobox.com/~schwern/
Perl6 Quality Assurance <[EMAIL PROTECTED]>   Kwalitee Is Job One
How can I stoop so low?  Years of practise, that's how. It's been hard
going but now I can stoop lower than a pygmy limbo dancer.
-- BOFH



RE: Indenting

2001-10-16 Thread David M. Lloyd

On Tue, 16 Oct 2001, David Whipp wrote:

> . I know it uses valuable characters, but adding C to
> identify a query, and C for an operation does not seem
> unreasonable.

What about 'chomp?' for query but 'chomp' (no decoration) for operation?

I think using ? on method names is kind of cute. :-)

- D

<[EMAIL PROTECTED]>




RE: Indenting

2001-10-16 Thread David Whipp

> Well, as discussed briefly in an earlier thread, 
> http:[EMAIL PROTECTED]/msg08514.html
> if we allow ! in function names, we can distinguish between the normal
> and in-place versions of functions without proliferating the number of
> keywords.
> 
> chomp! $string;
> my $chomped_string = chomp $string; 

I've been trying to work out if there is a possible unification
with assignment operators. Compare:

$a.operator:+=(1);
$a.chomp;

Both modify their object. However, I think the issue my be more
fundamental

In fact, in most OOPLs, operations do modify their object while
queries don't. A common strategy is to use a C method
when you want don't want to change the object of the operation.

The use of decorations to distinguish uses of nouns is standard
in perl culture ($@%). We don't currently use anything to
distinguish verbs. I know it uses valuable characters, but
adding C to identify a query, and C for an
operation does not seem unreasonable.

Unfortunately, the use of "!" to mean modify_in_place is potentially
ambiguous, because it is a unary operator. I would expect "foo!$a" to
parse as "foo(!$a)", not "(foo!)$a"

Another possibility would be use use C<$a->chomp> for operations
and C<$a.chomp> for the query, but this doesn't help for the
infix form.

Obvious alternatives such as "chomp= $a" suffer the same flaw.
Damian's suggestion of chomp: may be better, if I can teach my
brain to scan it.

I can't reach a conclusion in my own mind, so I thought I'd throw
out my thoughts.


Dave.



Re: Indenting

2001-10-16 Thread Michael G Schwern

On Wed, Oct 17, 2001 at 07:56:04AM +1000, Damian Conway wrote:
>> Well, as discussed briefly in an earlier thread, 
>> http:[EMAIL PROTECTED]/msg08514.html
>> if we allow ! in function names, we can distinguish between the normal
>> and in-place versions of functions without proliferating the number of
>> keywords.
>> 
>> chomp! $string;# how chomp() currently works
>> my $chomped_string = chomp $string;# like your chomped() function
> 
> Or, rather than this over-excited syntax, perhaps we could distinguish
> the pure function:
> 
>   chomp $string;  # return a chomped copy of $string
> 
> from the method:
>   
>   chomp $string: ;# chomp the original $string
>   # a.k.a. $string.chomp;

That : doesn't really catch the eye well at all.

It seems sort of odd to have a special syntax to disambiguate between
a method call and a function call when we already have syntax to do
so:

chomp $string;  # function call
$string.chomp;  # method call.

Leaving that aside, making the method call work in-place and the
function call work not will work *if and only if* it's consistently
applied.  Otherwise it's just going to get confusing to remember which
method versions work like which functions and which don't.

$string.chomp   # in-place chomp
@array.sort # sorts @array directly
$string.chop# chops the last character off $string

my $clean_string = chomp $string;
my @sorted_array = sort @array;
my $clean_string = chop $string;

etc...

Also, the methods should return their own objects to allow efficient
chaining (ie. we're not copying, we're aliasing):

print $string.chomp;

And this should solve the trailing terminator here-doc problem nicely:

print (   Kwalitee Is Job One
Do you actually think about what you are saying or is it an improvisational 
game of Mad Libs that you play in your head?



Re: Indenting

2001-10-16 Thread Damian Conway


   > Well, as discussed briefly in an earlier thread, 
   > http:[EMAIL PROTECTED]/msg08514.html
   > if we allow ! in function names, we can distinguish between the normal
   > and in-place versions of functions without proliferating the number of
   > keywords.
   > 
   > chomp! $string;# how chomp() currently works
   > my $chomped_string = chomp $string;# like your chomped() function

Or, rather than this over-excited syntax, perhaps we could distinguish
the pure function:

chomp $string;  # return a chomped copy of $string

from the method:

chomp $string: ;# chomp the original $string
# a.k.a. $string.chomp;

Damian



Re: Indenting

2001-10-16 Thread Michael G Schwern

On Tue, Oct 16, 2001 at 12:56:16PM +0200, Bart Lateur wrote:
> Which reminds me... one of the less attractive features of here docs is
> the fact that the quoted document always has to end in a newline. That
> is annoying at times.

> If there was an easy way to chomp() that newline and return the
> remainder of the string, that would solve this too. Like this?
> 
>   sub chomped ($) {
>chomp(my $s = shift);
>return $s;
>   }

Given that you can write chomped() in two lines, and given that you
want the trailing newline in at least 90% of all cases, doesn't that
pretty much solve the problem without a built-in?


> I don't really like this solution much. And it's not worth an extra
> keyword, for chomped() to become a built-in.

Well, as discussed briefly in an earlier thread, 
http:[EMAIL PROTECTED]/msg08514.html
if we allow ! in function names, we can distinguish between the normal
and in-place versions of functions without proliferating the number of
keywords.

chomp! $string;# how chomp() currently works
my $chomped_string = chomp $string;# like your chomped() function


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>http://www.pobox.com/~schwern/
Perl6 Quality Assurance <[EMAIL PROTECTED]>   Kwalitee Is Job One
at last, paste for all
citizens will proclaim me
paste enema god
-- imploded



Re: Indenting

2001-10-16 Thread Aaron Sherman

On Tue, Oct 16, 2001 at 09:30:44AM -0700, David Wheeler wrote:
> 
> That's part of the reason that I almost never use here docs, but the
> qq{} operator, instead. No need for a closing newline.

I have not read the RFC, but I do agree that qq is the way to go for
formatted content. Or, perhaps something like it that in turn 
resembles the s{}{} operator?

qf{^\s+}{   Now is the time
for all good men...};

eq

sub qf ($pat,$str) {
$str =~ s/$pat//mg;
return $str;
}
qf('^\s+', qq{  Now is the time
for all good men...});

eq

qq{Now is the time\nfor all good men...};

That fits with the current trend and gives us a very flexible
way to deal with just about any sort of formatting.

You would want to allow the usual m{} options after the second
close, but I think the default behavior should be that of m{}mg

I also think that the above should be abbreviated as:

qf{}{   Now is the time
for all good men...};

since useful defaults are always good.

-- 
Aaron Sherman
[EMAIL PROTECTED] finger [EMAIL PROTECTED] for GPG info. Fingerprint:
www.ajs.com/~ajs6DC1 F67A B9FB 2FBA D04C  619E FC35 5713 2676 CEAF
  "Write your letters in the sand for the day I'll take your hand
   In the land that our grandchildren knew." -Queen/_'39_



Re: Indenting

2001-10-16 Thread David Wheeler

On Tue, 2001-10-16 at 03:56, Bart Lateur wrote:

> Which reminds me... one of the less attractive features of here docs
is
> the fact that the quoted document always has to end in a newline. That
> is annoying at times.

That's part of the reason that I almost never use here docs, but the
qq{} operator, instead. No need for a closing newline.

David

-- 
David Wheeler AIM: dwTheory
[EMAIL PROTECTED] ICQ: 15726394
   Yahoo!: dew7e
   Jabber:
[EMAIL PROTECTED]




Re: Indenting

2001-10-16 Thread Bart Lateur

On Mon, 15 Oct 2001 20:53:24 -0400, Michael G Schwern wrote:

>Not only is it a bit faster than the s/^\s+//gm regex, but it is also
>more flexible.
>
>if( $self->feeling_snooty ) {
>print <<'POEM';
>Sometimes
>form has to follow function
>all over the page.
>POEM
>}

Which reminds me... one of the less attractive features of here docs is
the fact that the quoted document always has to end in a newline. That
is annoying at times.

For example, I often use here docs as a template mechanism: convert the
original text file to a here doc, split into three parts: intro, main
body (loop body), outtro; and you can generate such a doc with as many
repetitions as you like. If the three parts may be split right after a
newline, as commonly in HTML, there's no problem. But sometimes you want
to split in the middle of a line.

If there was an easy way to chomp() that newline and return the
remainder of the string, that would solve this too. Like this?

sub chomped ($) {
 chomp(my $s = shift);
 return $s;
}

I don't really like this solution much. And it's not worth an extra
keyword, for chomped() to become a built-in.

This, in turn, reminds me of the other common problem, where s///
modifies a string, instead of returning a modified copy, as some people
would like.

Ain't such a chain of thoughts lovely...  :-)

-- 
Bart.



Re: Indenting

2001-10-15 Thread Michael G Schwern

On Sat, Oct 13, 2001 at 04:30:08PM +0200, raptor wrote:
> I was looking at TPJ one-liners and saw this :
> 
> #32A trick for indenting here strings
> 
> ($definition = <<'FINIS') =~ s/^\s+//gm;
> The five varieties of camelids are the familliar
> camel, his friends the llama and the alpaca, and
> the rather less well-known guanaco and vicuna.
> FINIS
> 
> Courtesy of The Perl Cookbook
> 
> It is very cool if we have a way to set this RegEx so that it executes in
> compile time I mean if we have the ability to set this, so that we have
> any funny formating we want w/o loosing the speed of parsing it at
> runtime...

There was a big hub-bub about this back when RFCs were flying around.
If I remember Apoc 2 correctly, it will work like so:

$definition = <<'FINIS';
The five varieties of camelids are the familliar
camel, his friends the llama and the alpaca, and
the rather less well-known guanaco and vicuna.
FINIS

The here-doc text will be stripped up to the indented terminator.  So
in this case, all the leading whitespace will be stripped off.

Not only is it a bit faster than the s/^\s+//gm regex, but it is also
more flexible.

if( $self->feeling_snooty ) {
print <<'POEM';
Sometimes
form has to follow function
all over the page.
POEM
}

Rather than simply stripping the whitespace off the front, which would
lose the layout of the poem, it only strips as much as POEM is
indented.  Like having s/^\s{4}//gm.  So you get the equivalent of:

print
"Sometimes\n".
"form has to follow function\n".
"all over the page.\n";


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>http://www.pobox.com/~schwern/
Perl6 Quality Assurance <[EMAIL PROTECTED]>   Kwalitee Is Job One
Home of da bomb



Re: Indenting

2001-10-15 Thread RaFaL Pocztarski

Bart Lateur wrote:

> I can remember that an even smarter version of this was discussed in an
> RFC -- I don't remember which -- so it's not unlikely that it will be
> part of Perl6.

RFC162 - Heredoc contents:
http://dev.perl.org/rfc/162.html

Apocalypse 2:
http://dev.perl.org/perl6/apocalypse/2#rfc 162: heredoc contents
(I don't like spaces in anchor names...)

- RaFaL Pocztarski, [EMAIL PROTECTED]





Re: Indenting

2001-10-14 Thread Bart Lateur

On Sat, 13 Oct 2001 16:30:08 +0200, raptor wrote:

>I was looking at TPJ one-liners and saw this :
>
>#32A trick for indenting here strings
>
>($definition = <<'FINIS') =~ s/^\s+//gm;
>The five varieties of camelids are the familliar
>camel, his friends the llama and the alpaca, and
>the rather less well-known guanaco and vicuna.
>FINIS
>
>Courtesy of The Perl Cookbook
>
>It is very cool if we have a way to set this RegEx so that it executes in
>compile time

I can remember that an even smarter version of this was discussed in an
RFC -- I don't remember which -- so it's not unlikely that it will be
part of Perl6.

Besides, if this is a fixed string, you can rearrange your source code
so that it executes only once. It doesn't really matter then if this is
at compile time, or at run time.

-- 
Bart.



Indenting

2001-10-13 Thread raptor

Hi,

I was looking at TPJ one-liners and saw this :

#32A trick for indenting here strings

($definition = <<'FINIS') =~ s/^\s+//gm;
The five varieties of camelids are the familliar
camel, his friends the llama and the alpaca, and
the rather less well-known guanaco and vicuna.
FINIS

Courtesy of The Perl Cookbook

It is very cool if we have a way to set this RegEx so that it executes in
compile time I mean if we have the ability to set this, so that we have
any funny formating we want w/o loosing the speed of parsing it at
runtime...
Or it works that way !! already..
cheers
=
iVAN
[EMAIL PROTECTED]
=