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

'my' declaration and ternary conditional op: unexpected behavior #16883

Open
p5pRT opened this issue Mar 9, 2019 · 3 comments
Open

'my' declaration and ternary conditional op: unexpected behavior #16883

p5pRT opened this issue Mar 9, 2019 · 3 comments

Comments

@p5pRT
Copy link

p5pRT commented Mar 9, 2019

Migrated from rt.perl.org#133918 (status was 'open')

Searchable as RT133918$

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2019

From dunedimensia@gmail.com

Created by dunedimensia@gmail.com

Hello.

It seems that 'my' access both branches of ternary conditional operator(?).
Expected behavior​: only one branch of ternary op. is evaluated. (See code example and its output at the END)

Also 'my' fails to compile ternary op. to declare one of the variables which are in branches. It tries to declare a condition.
If uncomment, error is generated​:
Can't declare conditional expression in "my" at ternary_conditional_and_my.pl line 12, near ") ="
Execution of ternary_conditional_and_my.pl aborted due to compilation errors.

And 'local' do as expected.

Same behavior in version 5.28.0

#!/usr/bin/perl -wl

use strict;

my( $A, $B ) = ( 5, 7 );
our( $C, $D ) = ( 6, 8 );

for my $i ( 0 .. 1 ){
  print " " . "-" x 10;
  print " [$A],[$B]";
 
# my( $i ? $C : $D ) = '-';
# local( $i ? $C : $D ) = '-';
 
  $i ? ( my $A = '-' ) : ( my $B = '-' );
  $i ? ( local $C = '-' ) : ( local $D = '-' );
 
  print " [$A],[$B]";
  print " [$C],[$D]";
}

print "[$A],[$B]";
print "[$C],[$D]";

__END__
----------
[5],[7]
Use of uninitialized value $A in concatenation (.) or string at ternary_conditional_and_my.pl line 18.
[],[-]
[6],[-]
----------
[5],[7]
Use of uninitialized value $B in concatenation (.) or string at ternary_conditional_and_my.pl line 18.
[-],[]
[-],[8]
[5],[7]
[6],[8]

Perl Info

Flags:
    category=core
    severity=wishlist

Site configuration information for perl 5.18.2:

Configured by Debian Project at Tue Nov 20 16:49:55 UTC 2018.

Summary of my perl5 (revision 5 version 18 subversion 2) configuration:
   
  Platform:
    osname=linux, osvers=4.4.0-139-generic, archname=i686-linux-gnu-thread-multi-64int
    uname='linux lcy01-amd64-009 4.4.0-139-generic #165-ubuntu smp wed oct 24 10:58:50 utc 2018 i686 athlon i686 gnulinux '
    

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2019

From @Grinnz

This is intended behavior. "my" has a compile time behavior of declaring the variable in the current lexical scope, which will mask the variable from outside the scope regardless of the ternary. Assignment and "local" occur at runtime, as "local" is dynamically scoped.

-Dan

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2019

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

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

No branches or pull requests

2 participants