I have a problem assigning a varable when using (?{}) inside of a regex. When I run the code below I receive this error:
Sunrise is:03:12Sunset is:21:08 Use of uninitialized value in print at D:\scripts\reg.pl line 7, <DATA +> line 2. Use of uninitialized value in print at D:\scripts\reg.pl line 7, <DATA +> line 2. Sunrise is:Sunset is:
If I comment out the use strict line of the code and remove the my($sunrise, $sunset) line the code will run. I really don't want to comment out the use strict part of my code. So my question is how can I change my code so the (?{}) part will play nice with use strict?
Thanksuse strict; use warnings; while (<DATA>) { my ( $sunrise, $sunset ) = get_time($_); print "Sunrise is:", ${$sunrise}, "Sunset is:", ${$sunset}, "\n"; } sub get_time { my ($string) = @_; my ($sunrise, $sunset); $string =~ /sunrise:\s+(\d+:\d+)(?{ $sunrise = $^N}) \s+sunset:\s+(\d+:\d+)(?{$sunset = $^N})/x; return ( \$sunrise, \$sunset ); } __DATA__ Aberdeen, Scotland 57 9 N 2 9 W sunrise: 03:12 sunset: 21:08 Adelaide, Australia 34 55 S 138 36 E sunrise: 06:52 sunset: 16:41
In reply to Assigning a varable inside of (?{}) by monsterzero
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |