develooper Front page | perl.perl5.porters | Postings from June 2013

[perl #44075] Implicit loop created by -n may compile invalid programs

From:
Matthew Horsfall via RT
Date:
June 20, 2013 19:33
Subject:
[perl #44075] Implicit loop created by -n may compile invalid programs
Message ID:
rt-3.6.HEAD-2552-1371756774-994.44075-15-0@perl.org
On Mon Jul 23 10:43:23 2007, jsailor wrote:
> On Mon, 23 Jul 2007, Abigail via RT wrote:
> >> For example,
> >>
> >>   $ perl -cne '$'
> >>   -e syntax OK
> >>
> >> compiles, and a beast like,
> >>
> >>   $ echo | perl -ne 'BEGIN {$; = "hello world\n" } print $'
> >>   hello world
> >>
> >> actually runs.
> >>
> >> This does not seem to happen with -p, but I have no idea why.
> >
> >  $ echo "Hello" | perl -pe 'BEGIN {$; = "hello world\n" } print $'
> >  hello world
> >  Hello
> >
> > I'm not sure whether this artifact is actually a bug. It's an oddity,
> > and can be abused for fun and obscurity bonus, but it does behave as
> > documented.
> >
> > Abigail
> 
> Oh, yes, the possibilities for obfuscated code are quite promising. The 
> only thing that's pushing me towards calling it a bug is that, where 
> perlrun describes the implicit loop, it does not include a semicolon. Is 
> this documented in someplace other than perlrun, or am I missing 
> something there?
> 
> (As an aside, at least under my perl, your snippet above doesn't work, 
> as it uses -p instead of -n.)
> 
> -Jonathan

As of blead, these appear to behave differently (but with 5.10.1 they
seemed the same):

mhorsfall@Fireforge:~/perl-1$ ./perl -Ilib -MO=Deparse -ne 'print $'
syntax error at -e line 1, at EOF
-e had compilation errors.
LINE: while (defined($_ = <ARGV>)) {
    ();
}
mhorsfall@Fireforge:~/perl-1$ ./perl -Ilib -MO=Deparse -pe 'print $'
LINE: while (defined($_ = <ARGV>)) {
    print $;
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK

Should these be fixed to be similar or would doing so break other
unknown uses and abused of the two? 

I think consistency would be better, and the abuse of these methods is
now documented in perlsecret.pod:

https://github.com/book/perlsecret/blob/master/lib/perlsecret.pod#eskimo-greeting-

-- Matthew Horsfall (alh)

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=44075



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About