[33104] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4380 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 27 11:09:21 2015

Date: Fri, 27 Feb 2015 08:09:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 27 Feb 2015     Volume: 11 Number: 4380

Today's topics:
    Re: del old files and empty dirs <rweikusat@mobileactivedefense.com>
    Re: del old files and empty dirs <gravitalsun@hotmail.foo>
    Re: del old files and empty dirs <rweikusat@mobileactivedefense.com>
        function returning a list of the indices of its true ar <rweikusat@mobileactivedefense.com>
    Re: function returning a list of the indices of its tru <bjoern@hoehrmann.de>
    Re: function returning a list of the indices of its tru <gravitalsun@hotmail.foo>
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
    Re: function returning a list of the indices of its tru <gamo@telecable.es>
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
    Re: grep <ruben@mrbrklyn.com>
    Re: grep <derykus@gmail.com>
    Re: How does this bizarre script work? <bauhaus@futureapps.invalid>
    Re: How does this bizarre script work? <rweikusat@mobileactivedefense.com>
    Re: How does this bizarre script work? <bauhaus@futureapps.invalid>
    Re: How does this bizarre script work? <rweikusat@mobileactivedefense.com>
    Re: How does this bizarre script work? <*@eli.users.panix.com>
    Re: How does this bizarre script work? <bauhaus@futureapps.invalid>
    Re: How does this bizarre script work? <rweikusat@mobileactivedefense.com>
    Re: How does this bizarre script work? <bauhaus@futureapps.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 26 Feb 2015 14:45:11 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: del old files and empty dirs
Message-Id: <87egpc68aw.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 26/2/2015 01:57, Rainer Weikusat wrote:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
> This is a much faster version, there is no more double checking per
> dir. There is a an even faster approch without File::Find but have an
> issue. anyway

[...]

> 	# Delete old files
> 	wanted=>sub {
> 	return unless -f $File::Find::name;
> 	$days < -M _ ? (unlink $File::Find::name):($clear=0)
> 	},
>
> 	# Delete empty directories
> 	postprocess=>sub {
> 	return if $File::Find::dir eq $dir;
> 	$clear ? (rmdir $File::Find::dir):($clear=1)
> 	}},$dir)
> }

Repeating one of my all-time favorite quotes: "Of course it doesn't
work. But look how fast it is!".

The only way to delete 'empty directories' is to try deleting them. If
this succeeds, they were empty at the time of the attempt, otherwise,
they weren't. Not too mention that there are other things in the
filesystem namespace than regular files and directories, eg, device
nodes, FIFOs or sockets.

BTW, the need to add spurious brackets around something like

$clear = 0

is meant to tell you that you're using the construct in a way it wasn't
meant to be used and that another construct may be more suitable. In
itself, this is not so important, although it might seriously confuse
people used to a different style of writing and will communicate that
you care more for "teddybear programming" (So cute!) than expressing
yourself clearly.


------------------------------

Date: Thu, 26 Feb 2015 17:53:09 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: del old files and empty dirs
Message-Id: <mcnfh9$65a$1@news.ntua.gr>

On 26/2/2015 4:45 μμ, Rainer Weikusat wrote:

> The only way to delete 'empty directories' is to try deleting them. If
> this succeeds, they were empty at the time of the attempt, otherwise,
> they weren't.

the rmdir is happen as soon as possible after the first check so the 
odds are little. If the dir do not get deleted at the specific execution 
it is not the end of the world.

> filesystem namespace than regular files and directories, eg, device
> nodes, FIFOs or sockets.

in general yes , this applied only to dirs you know they contain only 
"normal" files. btw it is working.


------------------------------

Date: Thu, 26 Feb 2015 16:38:02 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: del old files and empty dirs
Message-Id: <87oaogeihh.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 26/2/2015 4:45 μμ, Rainer Weikusat wrote:
>> The only way to delete 'empty directories' is to try deleting them. If
>> this succeeds, they were empty at the time of the attempt, otherwise,
>> they weren't.
>
> the rmdir is happen as soon as possible after the first check so the
> odds are little.

That's another misconception: On a preemptive multiprogamming system,
an arbitrary amount of time can pass between the check and the rmdir.

NB: I don't claim that this isn't prefectly acceptable in practice.


------------------------------

Date: Thu, 26 Feb 2015 21:31:44 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: function returning a list of the indices of its true arguments
Message-Id: <87vbiocqbj.fsf@doppelsaurus.mobileactivedefense.com>

sub trues
{
    map { $_[$_] ? $_ : () } 0 .. $#_;
}

I actually needed this for something and found it rather amusing.


------------------------------

Date: Fri, 27 Feb 2015 00:31:03 +0100
From: Bjoern Hoehrmann <bjoern@hoehrmann.de>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <p1bveadbml0vj0gdv2s57ni0ftp9t4upkb@hive.bjoern.hoehrmann.de>

* Rainer Weikusat wrote in comp.lang.perl.misc:
>sub trues
>{
>    map { $_[$_] ? $_ : () } 0 .. $#_;
>}
>
>I actually needed this for something and found it rather amusing.

That looks like a `grep` to me.
-- 
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
D-10243 Berlin · PGP Pub. KeyID: 0xA4357E78 · http://www.bjoernsworld.de
 Available for hire in Berlin (early 2015)  · http://www.websitedev.de/ 


------------------------------

Date: Fri, 27 Feb 2015 01:50:41 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <mcobgj$2m41$1@news.ntua.gr>

On 26/2/2015 11:31 μμ, Rainer Weikusat wrote:
> sub trues
> {
>      map { $_[$_] ? $_ : () } 0 .. $#_;
> }
>
> I actually needed this for something and found it rather amusing.
>

sub trues
{
splice
@_, $_, 1, $_[$_] ? $_ : '' for 0 .. $#_;
@_
}



------------------------------

Date: Fri, 27 Feb 2015 13:14:32 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <87oaofebt3.fsf@doppelsaurus.mobileactivedefense.com>

Bjoern Hoehrmann <bjoern@hoehrmann.de> writes:
> * Rainer Weikusat wrote in comp.lang.perl.misc:
>>sub trues
>>{
>>    map { $_[$_] ? $_ : () } 0 .. $#_;
>>}
>>
>>I actually needed this for something and found it rather amusing.
>
> That looks like a `grep` to me.

Considering that it's not supposed to be a picture, that's a more
sensible idea. Thanks.


------------------------------

Date: Fri, 27 Feb 2015 15:00:24 +0100
From: gamo <gamo@telecable.es>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <mcpt9m$gbn$1@speranza.aioe.org>

El 26/02/15 a las 22:31, Rainer Weikusat escribió:
> sub trues
> {
>      map { $_[$_] ? $_ : () } 0 .. $#_;
> }
>
> I actually needed this for something and found it rather amusing.
>

I don't think it's neecesary to return empty or undef in false positions.
In "teddybear programming" could be:

sub trues {
	my @list = @_;
	my @result;
	for my $i (0..$#list){
		if ($list[$i]){
			push @result, $i;
		}else{
			# nothing
		}
	}
	return @results;
}


What's wrong with this?

-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Fri, 27 Feb 2015 14:28:13 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <874mq7o2de.fsf@doppelsaurus.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 26/02/15 a las 22:31, Rainer Weikusat escribió:
>> sub trues
>> {
>>      map { $_[$_] ? $_ : () } 0 .. $#_;
>> }
>>
>> I actually needed this for something and found it rather amusing.
>>
>
> I don't think it's neecesary to return empty or undef in false
> positions.

map can return any number of values, including no values. But for this
to work, the last evaluated expression must do this, eg () for 'return a
list with no elements'.

> In "teddybear programming" could be:
>
> sub trues {
> 	my @list = @_;
> 	my @result;
> 	for my $i (0..$#list){
> 		if ($list[$i]){
> 			push @result, $i;
> 		}else{
> 			# nothing
> 		}
> 	}
> 	return @results;
> }
>
> What's wrong with this?

What is wrong with my English, too: I'm not a native speaker, hence, I'm
prone to using phrases/ collocation appearing rather bizarre to native
speakers without even realizing this and said 'native speakers' then either
don't understand me or believe me to be some kind of half-wit.


------------------------------

Date: Thu, 26 Feb 2015 09:12:01 -0500
From: ruben safir <ruben@mrbrklyn.com>
Subject: Re: grep
Message-Id: <mcn9jh$u2$1@reader1.panix.com>

On 02/22/2015 06:55 AM, Eric Pozharski wrote:
> with <mcba9m$d9$1@reader1.panix.com> ruben safir wrote:
> 
>> any idea why this might fail with a known match
>>
>> open (WHITELIST, "<", "/home/ruben/mail/whitelist") or die;
>> my @oknames = <WHITELIST>;
>> close(WHITELIST);
>>
>> ...
>>         if( /(From: *"?[ \w-.].*"?)\b<?([\w].+\@[-a-z.A-Z=]+\.(\w.+))(?!<)/ ){
>> #               print  STDERR "HEY a WINNER\nONE $1 \n Two $2 \n Three $3";
>>                 if ($2 =~ /ruben\@mrbrklyn\.com/){
>>                         last;
>>                 }
>>                 my @ret;
>>                 if( @ret = grep( /.*$2.*/ , @oknames) ){
>>                         print STDERR "\n$2 WHITELISTED @ret\n";
>>                         last;
>>                 }
> 
> Sure:
> 
>   (1) See difference (with your so-called-test-program-works)?
> 
> 	% perl -wle '
> 	"abcdef" =~ m/(ab).*(ef)/ or die "foo";
> 	print $2;
> 	"abcdef" =~ m/ab.*ef/ or die "bar";
> 	print $2
> 	'                                     
> 	ef
> 	Use of uninitialized value $2 in print at -e line 5.
> 
>   (2) use-strict-use-warnings!  Even in production.  That's why your
>       so-called-test-program-works works and your
>       so-called-intest-program-works doesn't work.
> 
>   (3) I see that your gigantic regexp in all captures accepts
>       metachaarcters.  Guess what will happen here?
> 
> 	% perl -wle '
> 	"abc+++" =~ m/(\w)\w+(\W)\W+/ or die "foo";
> 	"abc+++" =~ m/(\w$2)/ or die "bar";
> 	print $1
> 	'                
> 
>   (4) Seriously, dude, seriously.  Email::Address->can( 'parse' )
> 
> 
> *CUT*
> 

why do you lose $2's value?


------------------------------

Date: Thu, 26 Feb 2015 06:19:28 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: grep
Message-Id: <8a2e4dfe-1123-40db-8b29-9d0a9f7d6675@googlegroups.com>

On Thursday, February 26, 2015 at 6:12:05 AM UTC-8, ruben safir wrote:
> On 02/22/2015 06:55 AM, Eric Pozharski wrote:
> > with <mcba9m$d9$1@reader1.panix.com> ruben safir wrote:
> > 
> >> any idea why this might fail with a known match
> >>
> >> open (WHITELIST, "<", "/home/ruben/mail/whitelist") or die;
> >> my @oknames = <WHITELIST>;
> >> close(WHITELIST);
> >>
> >> ...
> >>         if( /(From: *"?[ \w-.].*"?)\b<?([\w].+\@[-a-z.A-Z=]+\.(\w.+))(?!<)/ ){
> >> #               print  STDERR "HEY a WINNER\nONE $1 \n Two $2 \n Three $3";
> >>                 if ($2 =~ /ruben\@mrbrklyn\.com/){
> >>                         last;
> >>                 }
> >>                 my @ret;
> >>                 if( @ret = grep( /.*$2.*/ , @oknames) ){
> >>                         print STDERR "\n$2 WHITELISTED @ret\n";
> >>                         last;
> >>                 }
> > 
> > Sure:
> > 
> >   (1) See difference (with your so-called-test-program-works)?
> > 
> > 	% perl -wle '
> > 	"abcdef" =~ m/(ab).*(ef)/ or die "foo";
> > 	print $2;
> > 	"abcdef" =~ m/ab.*ef/ or die "bar";
> > 	print $2
> > 	'                                     
> > 	ef
> > 	Use of uninitialized value $2 in print at -e line 5.
> > 
> >   (2) use-strict-use-warnings!  Even in production.  That's why your
> >       so-called-test-program-works works and your
> >       so-called-intest-program-works doesn't work.
> > 
> >   (3) I see that your gigantic regexp in all captures accepts
> >       metachaarcters.  Guess what will happen here?
> > 
> > 	% perl -wle '
> > 	"abc+++" =~ m/(\w)\w+(\W)\W+/ or die "foo";
> > 	"abc+++" =~ m/(\w$2)/ or die "bar";
> > 	print $1
> > 	'                
> > 
> >   (4) Seriously, dude, seriously.  Email::Address->can( 'parse' )
> > 
> > 
> > *CUT*
> > 
> 
> why do you lose $2's value?

From perldoc perlre:

 Capture group contents are dynamically scoped and available to you
       outside the pattern until the end of the enclosing block or until the
       next successful match, whichever comes first.  (See "Compound
       Statements" in perlsyn.) 

-- 
Charles DeRykus 


------------------------------

Date: Thu, 26 Feb 2015 09:22:44 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: How does this bizarre script work?
Message-Id: <mcml3b$2q8$1@dont-email.me>

On 25.02.15 18:21, Rainer Weikusat wrote:
> "G.B." <bauhaus@futureapps.invalid> writes:
>> On 25.02.15 07:18, Robbie Hatley wrote:
>>> But why stop there?
>>> Symbol table hashes apparently can use ANYTHING as a key,
>>> even an entire valid Perl program
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>> Got the full length, then, and test your Perl during winter:
>>
>> *{do $0} = \ 1;
>> print STDOUT ${do $0}, "\n";
>
> "Where's the punchline"? AFAICT, this is a script which executes itself
> recursively until the process gets killed because the system ran out of
> memory (depending on 'the system', there's a chance that this will kill
> some other process).

Actually, the script was "well" behaved here, memory usage
grew, but not indefinitely, dropping again at some point
– for reasons that while not deciding this trivial halting
problem, would put it to good use when it gets cold (especially
with this much flu around) by turning an idle desktop computer
into a digital heating.

OTOH, the "do" part points to the long tradition of computed
symbols, in place of static strings, as a "powerful" device: just
like in SNOBOL-4, one could deploy computed labels in Perl, too,
as in

goto &{…};

as a way of structuring programs.



------------------------------

Date: Thu, 26 Feb 2015 16:06:10 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How does this bizarre script work?
Message-Id: <878ufkfyj1.fsf@doppelsaurus.mobileactivedefense.com>

Georg Bauhaus <bauhaus@futureapps.invalid> writes:
> On 25.02.15 18:21, Rainer Weikusat wrote:
>> "G.B." <bauhaus@futureapps.invalid> writes:
>>> On 25.02.15 07:18, Robbie Hatley wrote:
>>>> But why stop there?
>>>> Symbol table hashes apparently can use ANYTHING as a key,
>>>> even an entire valid Perl program
>          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>>> Got the full length, then, and test your Perl during winter:
>>>
>>> *{do $0} = \ 1;
>>> print STDOUT ${do $0}, "\n";
>>
>> "Where's the punchline"? AFAICT, this is a script which executes itself
>> recursively until the process gets killed because the system ran out of
>> memory (depending on 'the system', there's a chance that this will kill
>> some other process).
>
> Actually, the script was "well" behaved here, memory usage
> grew, but not indefinitely, dropping again at some point
> – for reasons that while not deciding this trivial halting
> problem, would put it to good use when it gets cold (especially
> with this much flu around) by turning an idle desktop computer
> into a digital heating.

It's possible to achieve the same while using only a constant amount of
memory and exercising more parts of the system with something like

exec('perl', $0, @ARGV);

and accomplish exactly the same with a mere

do $0

ie, functioning of this code isn't related to 'Perl identifiers' at all,
that's just syntactic window-dressing.

> OTOH, the "do" part points to the long tradition of computed
> symbols, in place of static strings,

Perl supports symbolic references and has been doing since the dawn of
time, eg executing this (in a file named a.pl)

---------
$animal = 'Lama';
$animal100 = '100 Lamas';

print(${$ARGV[0].$ARGV[1]}, "\n");
---------

as

perl a.pl animal

will print 'Lama' and invoking it as

perl a.pl animal 100

'100 Lamas'.

The same could also always accomplished by accessing the symbol table directly.

> as a "powerful" device: just
> like in SNOBOL-4, one could deploy computed labels in Perl, too,
> as in
>
> goto &{…};
>
> as a way of structuring programs.


    $lo_octet = (($prefix + 7) / 8) - 1;
    goto qw(O3 O2 O1)[$lo_octet] if $lo_octet < 3;
    
    add_bits($self, $addr & 0xff, 8);
O1: add_bits($self, ($addr >> 8) & 0xff, 8);
O2: add_bits($self, ($addr >> 16) & 0xff, 8);
O3: add_bits($self, ($addr >> 24) & 0xff, 8);

?


------------------------------

Date: Thu, 26 Feb 2015 18:19:11 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: How does this bizarre script work?
Message-Id: <mcnkh6$tf0$1@dont-email.me>

On 26.02.15 17:06, Rainer Weikusat wrote:
>> as a "powerful" device: just
>> >like in SNOBOL-4, one could deploy computed labels in Perl, too,
>> >as in
>> >
>> >goto &{…};
>> >
>> >as a way of structuring programs.
>
>      $lo_octet = (($prefix + 7) / 8) - 1;
>      goto qw(O3 O2 O1)[$lo_octet] if $lo_octet < 3;
>
>      add_bits($self, $addr & 0xff, 8);
> O1: add_bits($self, ($addr >> 8) & 0xff, 8);
> O2: add_bits($self, ($addr >> 16) & 0xff, 8);
> O3: add_bits($self, ($addr >> 24) & 0xff, 8);
>
> ?

A bit more like "goto the symbolically referenced function"
(and less literally "labels"):

use constant _CLS => {
     'f' => 'f_v3',
     'g' => 'g_dbg',
     'ARGH' => 'dflt',
};

sub vsel {
     goto &{_CLS->{shift @_} // _CLS->{'ARGH'}};
}

sub f_v0 {warn "f v0 called!"}
sub f_v1 {die "f v1"}
sub f_v2 {
     print sprintf "I'm f, %d!\n", 2*$_[0];
}
sub f_v3 {
     print sprintf "I'm f, %f!\n", 2*$_[0];
}

sub g_v0 { print "I'm g!\n" }
sub g_dbg { confess join ',', @_ }

sub dflt {
     warn "what?";
}

vsel ("f", 3.14);
vsel ("g", 'adsf', 2.71);



------------------------------

Date: Thu, 26 Feb 2015 17:39:13 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How does this bizarre script work?
Message-Id: <87k2z4efni.fsf@doppelsaurus.mobileactivedefense.com>

Georg Bauhaus <bauhaus@futureapps.invalid> writes:
> On 26.02.15 17:06, Rainer Weikusat wrote:
>>> as a "powerful" device: just
>>> >like in SNOBOL-4, one could deploy computed labels in Perl, too,
>>> >as in
>>> >
>>> >goto &{…};
>>> >
>>> >as a way of structuring programs.
>>
>>      $lo_octet = (($prefix + 7) / 8) - 1;
>>      goto qw(O3 O2 O1)[$lo_octet] if $lo_octet < 3;
>>
>>      add_bits($self, $addr & 0xff, 8);
>> O1: add_bits($self, ($addr >> 8) & 0xff, 8);
>> O2: add_bits($self, ($addr >> 16) & 0xff, 8);
>> O3: add_bits($self, ($addr >> 24) & 0xff, 8);
>>
>> ?
>
> A bit more like "goto the symbolically referenced function"
> (and less literally "labels"):
>
> use constant _CLS => {
>     'f' => 'f_v3',
>     'g' => 'g_dbg',
>     'ARGH' => 'dflt',
> };
>
> sub vsel {
>     goto &{_CLS->{shift @_} // _CLS->{'ARGH'}};
> }

The goto is not needed here, invoking a subroutine via symbolic
reference works perfectly fine, too. And (as shown above), Perl also
supports 'real' computed gotos.



------------------------------

Date: Fri, 27 Feb 2015 01:15:43 +0000 (UTC)
From: Eli the Bearded <*@eli.users.panix.com>
Subject: Re: How does this bizarre script work?
Message-Id: <eli$1502261959@qz.little-neck.ny.us>

In comp.lang.perl.misc, Robbie Hatley  <see.my.sig@for.my.address> wrote:
> 
> On 2/24/2015 1:50 PM, Rainer Weikusat wrote:
> 
> > *{"\0\0\0"} = \"\0";
> > print(ord(substr(${"\0\0\0"}, 0, 1)), "\n");
> 
> Triple-Null "identifier", eh?  Fine.  But why stop there?
> Symbol table hashes apparently can use ANYTHING as a key,
> even an entire valid Perl program such as:
> say "Cheese!";

The syntax you need to wrap that stuff with gives it away.

   $ cat /tmp/yes
   #!/usr/local/bin/perl5.20.0
   use warnings;
   use strict;

   $_ = "Just another perl hacker,\n";

   sperlPerlg;

   print;
   __END__
   $ /tmp/yes
   Just another Perl hacker,
   $ grep s.perl.Perl.g /tmp/yes
   sperlPerlg;
   $ 

That's right, you can use unprintable characters as delimitors in your
code. ("use warnings" will complain about using them in variable names,
and using them as sub names is flat out not allowed.)

Elijah
------
print qqJust another Perl hacker,\n;


------------------------------

Date: Fri, 27 Feb 2015 10:14:26 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: How does this bizarre script work?
Message-Id: <mcpcg9$p7v$1@dont-email.me>

On 26.02.15 18:39, Rainer Weikusat wrote:

>> sub vsel {
>>      goto &{_CLS->{shift @_} // _CLS->{'ARGH'}};
>> }
>
> The goto is not needed here, invoking a subroutine via symbolic
> reference works perfectly fine, too.

A call without "goto" will affect caller(), so "goto" might be
needed, depending on the use case.  Oddly, a plain call without
arguments but with "&" prefixd is a jump of sorts that implicitly
passes @_.




------------------------------

Date: Fri, 27 Feb 2015 13:57:44 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How does this bizarre script work?
Message-Id: <87bnkfo3s7.fsf@doppelsaurus.mobileactivedefense.com>

Georg Bauhaus <bauhaus@futureapps.invalid> writes:
> On 26.02.15 18:39, Rainer Weikusat wrote:
>
>>> sub vsel {
>>>      goto &{_CLS->{shift @_} // _CLS->{'ARGH'}};
>>> }
>>
>> The goto is not needed here, invoking a subroutine via symbolic
>> reference works perfectly fine, too.
>
> A call without "goto" will affect caller(), so "goto" might be
> needed, depending on the use case.

goto &something

does a tail call to 'something' (I benchmarked this once years ago and
found it no more efficient than doing an ordinary call. I should check
this again): The stack frame of the currently executing subroutine is
removed and the target subroutine is invoked instead 'in the same place'
(and with the current @_). That's supposed to be used by
subroutine-manufacturing subroutines aka AUTOLOAD to hide the fact that
a subroutine was manufactured,

---------
use Carp;

sub catcher
{
    print STDERR ("I'm here to get you!\n");
    evader();
}

catcher();

sub evader
{
    print STDERR ("\t[so you think ...]\n");
    eval('sub dupe { confess("You found me!") }');
    goto &dupe;
}
---------

It's not particularly related to 'computed identifiers'.

> Oddly, a plain call without
> arguments but with "&" prefixd is a jump of sorts that implicitly
> passes @_.

	If a subroutine is called using the "&" form, the argument list is
	optional, and if omitted, no @_ array is set up for the subroutine: the
	@_ array at the time of the call is visible to subroutine instead.  This
	is an efficiency mechanism that new users may wish to avoid.
        (perldoc perlsub)

I found this mostly useful for call-forwarding/ subroutine
composition. Silly example:

-----------
sub countdown
{
    $_[0] != $_[1] and 	++$_[1], &countdown;
    print($_[1]--, "\n");
}

countdown(10);
-----------


------------------------------

Date: Fri, 27 Feb 2015 16:31:18 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: How does this bizarre script work?
Message-Id: <mcq2is$faf$1@dont-email.me>

On 27.02.15 14:57, Rainer Weikusat wrote:
> Georg Bauhaus <bauhaus@futureapps.invalid> writes:
>> On 26.02.15 18:39, Rainer Weikusat wrote:
>>
>>>> sub vsel {
>>>>       goto &{_CLS->{shift @_} // _CLS->{'ARGH'}};
>>>> }
>>>
>>> The goto is not needed here, invoking a subroutine via symbolic
>>> reference works perfectly fine, too.
>>
>> A call without "goto" will affect caller(), so "goto" might be
>> needed, depending on the use case.
>
> goto &something

> It's not particularly related to 'computed identifiers'.

What is?
  
>> Oddly, a plain call without
>> arguments but with "&" prefixd is a jump of sorts that implicitly
>> passes @_.
>
> 	If a subroutine is called using the "&" form,  [...]

then it's a sort of jump (i.e., not any jump). That's fine and it
is an example of how Perl is a feature-rich language at the cost of
some oddities. The time of evaluation of @_ in this context is
also interesting.

> sub countdown
> {
>      $_[0] != $_[1] and 	++$_[1], &countdown;
>      print($_[1]--, "\n");
> }

This looks like code that falls into the same category that Lispers
invoke when they label their somewhat special recursive calls "LOOP".
I don't know why, maybe to make the program stay within the functional
framework while making it look like they had a loop, too. (And most do,
IIRC, but still.)



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4380
***************************************


home help back first fref pref prev next nref lref last post