f.zz.de
posts /

Unexpected perl

Posted Fri 30 Jun 2017 11:26:07 AM CEST Florian Lohoff
in

I always thought that perl sort function variables $a and $b where completely of local scope - Today i was told a different story.

The error message was:

Can't call method "id" on an undefined value at ./genconf line 1442.

I was trying to sort an array of objects with something like

foreach my $obj ( sort { $a->id() cmp $b->id() } @$objarrayref

The cause of the error message was trivial - 1000 lines down i used $a as an dummy variable

my $a=afunctionwhosereturnidontwant();

Which overided my $a in the sort function - Unexpected.