Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Doubt in Eval

by Anonymous Monk
on Feb 16, 2012 at 17:28 UTC ( #954292=note: print w/replies, xml ) Need Help??

in reply to Doubt in Eval

$a is exempt from strict checking, so let's use a different variable name. Let's enable the pragmas strict and warnings to point out mistakes that beginners are liable to make. I'll demonstrate that the eval statement is a red herring, it can be removed and the same problem still shows: "Global symbol "$aa" requires explicit package name atů"
#!/usr/bin/env perl use strict; use warnings; sub get_accounts { print $aa; } my $aa = 5; get_accounts();

Lesson to learn: pass parameters to functions, do not rely on lexical variables leaking into the function from an outer scope. (Globals are okay.) To help enforce this through coding style, always place subroutine definitions before the main part of the code.

Replies are listed 'Best First'.
Re^2: Doubt in Eval
by chromatic (Archbishop) on Feb 16, 2012 at 22:08 UTC
    I'll demonstrate that the eval statement is a red herring....

    It's not a red herring: you changed the order of the code and thus its behavior. When the parser encounters $aa in your example, it hasn't seen the declaration three lines later.

    If you had merely removed the eval block, the result would run just fine without the variable declaration error.

    Improve your skills with Modern Perl: the free book.

Re^2: Doubt in Eval
by Anonymous Monk on Feb 16, 2012 at 21:38 UTC
    Instead of using strict and warnings, one should use common::sense.
      Common sense is to use strict and warnings.
        perldoc common::sense NAME common::sense - save a tree AND a kitten, use common::sense! SYNOPSIS use common::sense; # supposed to be the same, with much lower memory usage, as: # # use utf8; # use strict qw(vars subs); # use feature qw(say state switch); # no warnings; # use warnings qw(FATAL closed threads internal debugging pack # portable prototype inplace io pipe unpack mallo +c # deprecated glob digit printf layer # reserved taint closure semicolon); # no warnings qw(exec newline unopened);
      $ corelist strict warnings Common::Sense strict was first released with perl 5 warnings was first released with perl v5.6.0 Common::Sense was not in CORE (or so I think) $

      Requiring the installation of a module that does little but save typing? Virtuous Laziness is not about pushing work off onto others.

      Additionally strict refs is more useful than not & can be easily disabled in the few scopes that it actually gets in the way.

      I agree w/ zwon. Common Sense is better w/o the double colon.

Re^2: Doubt in Eval
by chrestomanci (Priest) on Feb 17, 2012 at 21:26 UTC

    As a footnote, the reason that $a is exempt from strict checking, is because it is one of the magic variables that you can use to specify a custom comparison to sort

    For example, suppose you have a list of hashes, and you want to sort by the value of a key, you might write:,

    my @persons_by_age = sort { $a->{'age'} <=> $b->{'age'} } @people;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954292]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-05-21 00:13 GMT
Find Nodes?
    Voting Booth?