2 Replies - 2355 Views - Last Post: 11 May 2012 - 07:10 AM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Rather bizarre bug

Posted 11 May 2012 - 12:58 AM

Hi, I just found a bizarre Perl bug when experimenting with use strict. When I use strict and assign the command line argument to variable named $a, the interpreter doesn't complain (it should, when I use strict, I must specify fully qualified names) and prints out its value (first invocation of the script, as shown in the image), whereas I assign the same value to $name, the interpreter throws the usual error (2nd invocation). Can someone explain what's going on? The respective codes are as follows (note the only difference between the two scripts is in $a being replaced by $name!!)

use strict;
$a = shift;
print "the name is $a\n";  #prints out normally



use strict;
$name = shift;
print "the name is $name\n";  #throws error

Attached image(s)

  • Attached Image

This post has been edited by cupidvogel: 11 May 2012 - 12:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Rather bizarre bug

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Rather bizarre bug

Posted 11 May 2012 - 04:54 AM

See perldoc sort. The global variables $a and $b are always defined so that the sort command can use them. Because of this, you should not use them except in custom sort definitions. (Unless you enjoy having to figure out why they suddenly have different values after a subroutine call fourteen levels deep in another module uses sort...)
Was This Post Helpful? 2
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Rather bizarre bug

Posted 11 May 2012 - 07:10 AM

Yeah yeah, right. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1