meanroc has asked for the wisdom of the Perl Monks concerning the following question:

Hello , I want to say I am sorry first, because I criate one topic confusied..I do not speak english very well, and I am noob in perl, so I need more patience from you....So, I coded:

#!/usr/bin/perl use strict; use warnings; my $sys="$^O";if ($sys eq linux) { $lim="clear";} else { $lim="cls"; } print "hello"; my $ts=<STDIN>; print "$ts"; system("$lim");

So when I run:Global symbol "$lim" requires explicit package name at /root/Desktop/exemplo.pl line 4;4;8. Bareword "linux" not allowed while "strict subs" in use at /root/Desktop/exemplo.pl

What is wrong? Thanks and sorry.

Replies are listed 'Best First'.
Re: Global symbol requires explicit package..
by SuicideJunkie (Vicar) on Jan 21, 2014 at 18:10 UTC

    At line 4 you use $lim, and it isn't declared anywhere with my

    Also at line 4, you have a strange word linux which isn't a keyword and isn't defined as a sub anywhere either. You probably want to put quotes around that to make it a string.

Re: Global symbol requires explicit package..
by Eily (Monsignor) on Jan 21, 2014 at 18:12 UTC

    use strict; generates errors for some features / construct that are judged unsafe. More info on that in strict. And you can look at perldiag for more information on what the error messages mean.

    For exemple, the first one means :

    (F) You've said "use strict" or "use strict vars", which indicates that all variables must either be lexically scoped (using "my" or "state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::").
    You should declare $lim before using it. With a my $lim, for exemple.

    The second one means that since there are no quotes around 'linux', it should (because of strict) be interpreted by Perl as a function and not a string, but there's no function called linux in your code. If you want a string, write "linux".

Re: Global symbol requires explicit package..
by Laurent_R (Canon) on Jan 21, 2014 at 18:19 UTC
    Try to change your code this way:
    my $sys = $^O; my $lim; if ($sys eq "linux") { $lim="clear"; } else { $lim="cls"; } # ...
    You actually don't really need the $sys variable, you could compare dirfectly $^O, but it does not matter.

      Indeed; the whole thing could be written as:

      my $lim = ($sys eq 'linux') ? 'clear' : 'cls';
      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Global symbol requires explicit package..
by meanroc (Acolyte) on Jan 21, 2014 at 18:54 UTC

    Thak you so much guys..Now works.I do not need to delete the topic right??

      NO! Do not delete it. Others might have the same question and learn :)

      I'd write it even shorter:

      my $lim = $^O eq "linux" ? "clear" : "cls";

      The code you posted doe not past strict btw. So it must be changed before you ran it. Under strict, the bareword linux is not allowed.

      BTW, do not change the original post either :)


      Enjoy, Have FUN! H.Merijn
        So would I, but I wanted to stick as much as possible to the OP's code, so that he or she could see the required changes.