Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perldoc -f my should perhaps mention BEGIN and END #7861

Closed
p5pRT opened this issue Apr 2, 2005 · 18 comments
Closed

perldoc -f my should perhaps mention BEGIN and END #7861

p5pRT opened this issue Apr 2, 2005 · 18 comments

Comments

@p5pRT
Copy link

p5pRT commented Apr 2, 2005

Migrated from rt.perl.org#34650 (status was 'rejected')

Searchable as RT34650$

@p5pRT
Copy link
Author

p5pRT commented Apr 2, 2005

From @jidanni

Maybe "perldoc -f my" should add​:
If you are adding my due to adding use strict,
you may need to use our instead in BEGIN or END blocks.

@p5pRT
Copy link
Author

p5pRT commented Apr 2, 2005

From perl5-porters@ton.iguana.be

In article <rt-3.0.11-34650-109976.5.58716831800943@​perl.org>,
  Dan Jacobson (via RT) <perlbug-followup@​perl.org> writes​:

# New Ticket Created by Dan Jacobson
# Please include the string​: [perl #34650]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=34650 >

Maybe "perldoc -f my" should add​:
If you are adding my due to adding use strict,
you may need to use our instead in BEGIN or END blocks.

Absolutely not. You shouldn't just randomly add things to shut up
use strict without understanding the implications. "my" and "our" have
very specific and *different* meanings and should be used when that
meaning is the proper one in the program logic. We shouldn't encourage
"programming by use of magic incantations".

@p5pRT
Copy link
Author

p5pRT commented Apr 2, 2005

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Apr 2, 2005

From @schwern

On Sat, Apr 02, 2005 at 10​:53​:47PM +0000, Ton Hospel wrote​:

Maybe "perldoc -f my" should add​:
If you are adding my due to adding use strict,
you may need to use our instead in BEGIN or END blocks.

Absolutely not. You shouldn't just randomly add things to shut up
use strict without understanding the implications. "my" and "our" have
very specific and *different* meanings and should be used when that
meaning is the proper one in the program logic. We shouldn't encourage
"programming by use of magic incantations".

I agree. I'm also curious as to how BEGIN and END blocks play a role here.
"my" and "our" already happen at compile time.

Dan, maybe you should post what you're doing with our, BEGIN and END so
we can give suggestions on how to do it better.

@p5pRT p5pRT closed this as completed Apr 4, 2005
@p5pRT
Copy link
Author

p5pRT commented Apr 4, 2005

@smpeters - Status changed from 'open' to 'rejected'

@p5pRT
Copy link
Author

p5pRT commented Apr 4, 2005

From @jidanni

D> Dan, maybe you should post what you're doing with our, BEGIN and END so
D> we can give suggestions on how to do it better.

User is told to use strict.
Using strict causes one to need my. he soon finds out.
But one day he wants to use strict with BEGIN. Now with my, his BEGIN
stuff becomes local.
$ seq 3|perl -wne 'BEGIN{$k=3}print "$k$_"' #OK
$ seq 3|perl -wne 'use strict;BEGIN{$k=3}print "$k$_"' #bzzt
$ seq 3|perl -wne 'use strict;BEGIN{my $k=3}print "$k$_"' #bzzt
He finally throws away efficiency to get the program to run​:
$ seq 3|perl -wne 'use strict;my $k=3;print "$k$_"'
Anyway, I'm not sure where to document "special challenges with strict
vs. BEGIN, END, my and our". Anyways, he should try our.

@p5pRT
Copy link
Author

p5pRT commented Apr 4, 2005

From @schwern

On Tue, Apr 05, 2005 at 03​:55​:26AM +0800, Dan Jacobson wrote​:

D> Dan, maybe you should post what you're doing with our, BEGIN and END so
D> we can give suggestions on how to do it better.

User is told to use strict.
Using strict causes one to need my. he soon finds out.
But one day he wants to use strict with BEGIN. Now with my, his BEGIN
stuff becomes local.
$ seq 3|perl -wne 'BEGIN{$k=3}print "$k$_"' #OK
$ seq 3|perl -wne 'use strict;BEGIN{$k=3}print "$k$_"' #bzzt
$ seq 3|perl -wne 'use strict;BEGIN{my $k=3}print "$k$_"' #bzzt
He finally throws away efficiency to get the program to run​:
$ seq 3|perl -wne 'use strict;my $k=3;print "$k$_"'

Whoa... efficiency? How is using the BEGIN block more efficient?

Perhaps in this contrived example of using strict in a -n one-liner it
is insignificantly more efficient to initialze $k once... but its a variable
assignment, who cares? And you don't use strict in one-liners anyway. And
in larger programs where efficiency is a concern, they don't use -n so the
issue of having to cram variable initialization into a pseudo-loop isn't
a problem.

But I do see where you're going with this.

Anyway, I'm not sure where to document "special challenges with strict
vs. BEGIN, END, my and our".

Traditionally this sort of stuff is discussed in perlsub but that's probably
the wrong place. I'd say the strict man page.

our doesn't help this situation as even though it declares a global
variable the scope where it can be used unqualified is lexical.

$ perl -Mstrict -wle 'BEGIN { our $k = 3; } print "$k"'
Variable "$k" is not imported at -e line 1.
Global symbol "$k" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.

You would have to say​:

perl -Mstrict -wle 'our $k; BEGIN { $k = 3; } print "$k"'

And at that point, unless $k really is supposed to be global, you might as
well say​:

perl -Mstrict -wle 'my $k; BEGIN { $k = 3; } print "$k"'

And finally because our() and my() both occur at compile time you can
just eliminate the BEGIN block entirely.

perl -Mstrict -wle 'my $k = 3; print "$k"'

I agree its probably useful for my() to mention our() as they are related.

I also agree that its probably useful to talk about scoping, declaration
and BEGIN blocks as you have to have a firm grip on how scoping AND
compilation works to fully understand that. It is a trap for the unwary.

@p5pRT
Copy link
Author

p5pRT commented Apr 4, 2005

From @Abigail

On Mon, Apr 04, 2005 at 03​:06​:01PM -0700, Michael G Schwern wrote​:

perl -Mstrict -wle 'my $k; BEGIN { $k = 3; } print "$k"'

And finally because our() and my() both occur at compile time you can
just eliminate the BEGIN block entirely.

perl -Mstrict -wle 'my $k = 3; print "$k"'

For this particular case, yes. But in general, there is a difference
between​:

  my $var = EXPR;

and

  my $var; BEGIN {$var = EXPR}

You are right that the my() is done at compile time. The evaluation
of EXPR, and the assignment to the variable aren't.

Abigail

@p5pRT
Copy link
Author

p5pRT commented Apr 4, 2005

From @tamias

On Tue, Apr 05, 2005 at 03​:55​:26AM +0800, Dan Jacobson wrote​:

D> Dan, maybe you should post what you're doing with our, BEGIN and END so
D> we can give suggestions on how to do it better.

User is told to use strict.
Using strict causes one to need my. he soon finds out.
But one day he wants to use strict with BEGIN. Now with my, his BEGIN
stuff becomes local.
$ seq 3|perl -wne 'BEGIN{$k=3}print "$k$_"' #OK
$ seq 3|perl -wne 'use strict;BEGIN{$k=3}print "$k$_"' #bzzt
$ seq 3|perl -wne 'use strict;BEGIN{my $k=3}print "$k$_"' #bzzt
He finally throws away efficiency to get the program to run​:
$ seq 3|perl -wne 'use strict;my $k=3;print "$k$_"'
Anyway, I'm not sure where to document "special challenges with strict
vs. BEGIN, END, my and our". Anyways, he should try our.

seq 3|perl -wne 'use strict;my $k;BEGIN{$k=3}print "$k$_"'

There are no "special challenges". You simply need to declare your
variables at the correct scope, as always.

Using our where you have my doesn't even fix the problem​:

seq 3|perl -wne 'use strict;BEGIN{our $k=3}print "$k$_"' #bzzt

so I'm not sure why you suggested that.

Ronald

@p5pRT
Copy link
Author

p5pRT commented Apr 4, 2005

From @Abigail

On Mon, Apr 04, 2005 at 05​:53​:30PM -0400, Ronald J Kimball wrote​:

On Tue, Apr 05, 2005 at 03​:55​:26AM +0800, Dan Jacobson wrote​:

D> Dan, maybe you should post what you're doing with our, BEGIN and END so
D> we can give suggestions on how to do it better.

User is told to use strict.
Using strict causes one to need my. he soon finds out.
But one day he wants to use strict with BEGIN. Now with my, his BEGIN
stuff becomes local.
$ seq 3|perl -wne 'BEGIN{$k=3}print "$k$_"' #OK
$ seq 3|perl -wne 'use strict;BEGIN{$k=3}print "$k$_"' #bzzt
$ seq 3|perl -wne 'use strict;BEGIN{my $k=3}print "$k$_"' #bzzt
He finally throws away efficiency to get the program to run​:
$ seq 3|perl -wne 'use strict;my $k=3;print "$k$_"'
Anyway, I'm not sure where to document "special challenges with strict
vs. BEGIN, END, my and our". Anyways, he should try our.

seq 3|perl -wne 'use strict;my $k;BEGIN{$k=3}print "$k$_"'

There are no "special challenges". You simply need to declare your
variables at the correct scope, as always.

  $ seq 3|perl -wne 'use strict;my $k;BEGIN{$k=3}print "$k$_"'
  31
  Use of uninitialized value in concatenation (.) or string at -e line 1, <> line 2.
  2
  Use of uninitialized value in concatenation (.) or string at -e line 1, <> line 3.
  3

Problem is the -n. The entire program is inside a block, which is
reentered for each line of the input. So, any my(), our() (or even local())
will create a new variable or value.

  $ seq 3|perl -wne 'use strict;BEGIN{$​::k=3}print "$​::k$_"'
  31
  32
  33

Abigail

@p5pRT
Copy link
Author

p5pRT commented Apr 5, 2005

From @demerphq

On Apr 5, 2005 12​:34 AM, Abigail <abigail@​abigail.nl> wrote​:

On Mon, Apr 04, 2005 at 05​:53​:30PM -0400, Ronald J Kimball wrote​:

On Tue, Apr 05, 2005 at 03​:55​:26AM +0800, Dan Jacobson wrote​:

D> Dan, maybe you should post what you're doing with our, BEGIN and END so
D> we can give suggestions on how to do it better.

User is told to use strict.
Using strict causes one to need my. he soon finds out.
But one day he wants to use strict with BEGIN. Now with my, his BEGIN
stuff becomes local.
$ seq 3|perl -wne 'BEGIN{$k=3}print "$k$_"' #OK
$ seq 3|perl -wne 'use strict;BEGIN{$k=3}print "$k$_"' #bzzt
$ seq 3|perl -wne 'use strict;BEGIN{my $k=3}print "$k$_"' #bzzt
He finally throws away efficiency to get the program to run​:
$ seq 3|perl -wne 'use strict;my $k=3;print "$k$_"'
Anyway, I'm not sure where to document "special challenges with strict
vs. BEGIN, END, my and our". Anyways, he should try our.

seq 3|perl -wne 'use strict;my $k;BEGIN{$k=3}print "$k$_"'

There are no "special challenges". You simply need to declare your
variables at the correct scope, as always.

$ seq 3|perl -wne 'use strict;my $k;BEGIN{$k=3}print "$k$_"'
31
Use of uninitialized value in concatenation (.) or string at -e line 1, <> line 2.
2
Use of uninitialized value in concatenation (.) or string at -e line 1, <> line 3.
3

Problem is the -n. The entire program is inside a block, which is
reentered for each line of the input. So, any my(), our() (or even local())
will create a new variable or value.

$ seq 3|perl -wne 'use strict;BEGIN{$​::k=3}print "$​::k$_"'
31
32
33

its a pity that

perl -we "use strict; $k=3;" -ne "print $k$_"

doesnt do the right thing. maybe a new switch could be provided that
acts like -e, but is inserted into the code stream _before_ the
while(<>){} wrap. (YKWIM)

It would make thing like this a lot easier to read, and would
eliminate the need for oddness associated with BEGIN.

Just a thought.

Yves

--
perl -Mre=debug -e "/just|another|perl|hacker/"

@p5pRT
Copy link
Author

p5pRT commented Apr 5, 2005

From @Abigail

On Tue, Apr 05, 2005 at 08​:01​:29AM +0200, demerphq wrote​:

its a pity that

perl -we "use strict; $k=3;" -ne "print $k$_"

doesnt do the right thing. maybe a new switch could be provided that
acts like -e, but is inserted into the code stream _before_ the
while(<>){} wrap. (YKWIM)

It would make thing like this a lot easier to read, and would
eliminate the need for oddness associated with BEGIN.

The following works up to 5.8.1​:

  $ seq 3|/opt/perl/5.8.1/bin/perl -Mstrict='});my $k;()=({' -wne 'BEGIN{$k=3}print "$k$_"'
  31
  32
  33

But in 5.8.2 and beyond​:

  $ seq 3|/opt/perl/5.8.2/bin/perl -Mstrict='});my $k;()=({' -wne 'BEGIN{$k=3}print "$k$_"'
  Unknown 'strict' tag(s) '});my $k;()=({' at -e line 0
  BEGIN failed--compilation aborted.

I guess noone runs 'make torturetest'.

Abigail

@p5pRT
Copy link
Author

p5pRT commented Apr 5, 2005

From @pjcj

On Wed, Apr 06, 2005 at 12​:16​:21AM +0200, Abigail wrote​:

The following works up to 5.8.1​:

$ seq 3|/opt/perl/5\.8\.1/bin/perl \-Mstrict='\}\);my $k;\(\)=\(\{' \-wne 'BEGIN\{$k=3\}print "$k$\_"'
31
32
33

But in 5.8.2 and beyond​:

$ seq 3|/opt/perl/5\.8\.2/bin/perl \-Mstrict='\}\);my $k;\(\)=\(\{' \-wne 'BEGIN\{$k=3\}print "$k$\_"'
Unknown 'strict' tag\(s\) '\}\);my $k;\(\)=\(\{' at \-e line 0
BEGIN failed\-\-compilation aborted\.

I guess noone runs 'make torturetest'.

I don't, as a rule. I suppose this patch would be in order then?

Inline Patch
--- japh/abigail.t.org	2005-02-21 14:24:03.000000000 +0100
+++ japh/abigail.t	2005-04-06 01:30:36.000000000 +0200
@@ -68,7 +68,7 @@
 my $JaPH_c = "Just another Perl Hacker,";
 my $JaPh_c = "Just another Perl hacker,";
 
-plan tests => 130;
+plan tests => 129;
      
 {   
     my $out  = sprintf "Just another Perl Hacker";
@@ -413,15 +413,6 @@
 ARGS:     $datafile
 EXPECT:   6
 
-#######  Abusing -M
-1
-SWITCHES
--Mstrict='}); print "Just another Perl Hacker"; ({'
--l
-SKIP_OS: VMS
-MSWin32
-NetWare
-
 #######  rand
 srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
 //=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"

-- 

Paul Johnson - paul@​pjcj.net
http​://www.pjcj.net

@p5pRT
Copy link
Author

p5pRT commented Apr 5, 2005

From @Abigail

On Wed, Apr 06, 2005 at 01​:36​:12AM +0200, Paul Johnson wrote​:

On Wed, Apr 06, 2005 at 12​:16​:21AM +0200, Abigail wrote​:

The following works up to 5.8.1​:

$ seq 3|/opt/perl/5\.8\.1/bin/perl \-Mstrict='\}\);my $k;\(\)=\(\{' \-wne 'BEGIN\{$k=3\}print "$k$\_"'
31
32
33

But in 5.8.2 and beyond​:

$ seq 3|/opt/perl/5\.8\.2/bin/perl \-Mstrict='\}\);my $k;\(\)=\(\{' \-wne 'BEGIN\{$k=3\}print "$k$\_"'
Unknown 'strict' tag\(s\) '\}\);my $k;\(\)=\(\{' at \-e line 0
BEGIN failed\-\-compilation aborted\.

I guess noone runs 'make torturetest'.

I don't, as a rule. I suppose this patch would be in order then?

Or rather​:

Inline Patch
--- t/japh/abigail.t.orig       Wed Apr  6 01:38:40 2005
+++ t/japh/abigail.t    Wed Apr  6 01:39:59 2005
@@ -418,7 +418,7 @@
 SWITCHES
 -Mstrict='}); print "Just another Perl Hacker"; ({'
 -l
-SKIP_OS: VMS
+SKIP: No longer works in 5.8.2 and beyond.
 MSWin32
 NetWare




Abigail

@p5pRT
Copy link
Author

p5pRT commented Apr 6, 2005

From @schwern

On Wed, Apr 06, 2005 at 01​:41​:55AM +0200, Abigail wrote​:

+SKIP​: No longer works in 5.8.2 and beyond.

Before we stub it out, anyone know what changed to cause this to break?

@p5pRT
Copy link
Author

p5pRT commented Apr 6, 2005

From @ysth

On Tue, Apr 05, 2005 at 07​:59​:14PM -0700, Michael G Schwern wrote​:

On Wed, Apr 06, 2005 at 01​:41​:55AM +0200, Abigail wrote​:

+SKIP​: No longer works in 5.8.2 and beyond.

Before we stub it out, anyone know what changed to cause this to break?

There was a change at one point to prevent people doing "dangerous" things
with the -M switch; that would be my guess.

@p5pRT
Copy link
Author

p5pRT commented Apr 6, 2005

From @ysth

On Tue, Apr 05, 2005 at 07​:59​:14PM -0700, Michael G Schwern wrote​:

On Wed, Apr 06, 2005 at 01​:41​:55AM +0200, Abigail wrote​:

+SKIP​: No longer works in 5.8.2 and beyond.

Before we stub it out, anyone know what changed to cause this to break?

Ah, it's change 21429. Abigail was relying on the split ',', q{ ... } that
creates the import array; now it does q\0 ... \0 (with null characters).

@p5pRT
Copy link
Author

p5pRT commented Apr 7, 2005

From @rgs

Abigail wrote​:

--- t/japh/abigail.t.orig Wed Apr 6 01​:38​:40 2005
+++ t/japh/abigail.t Wed Apr 6 01​:39​:59 2005

Thanks, applied as #24192.

@​@​ -418,7 +418,7 @​@​
SWITCHES
-Mstrict='}); print "Just another Perl Hacker"; ({'
-l
-SKIP_OS​: VMS
+SKIP​: No longer works in 5.8.2 and beyond.
MSWin32
NetWare

Abigail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant