Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Frustrating Error: "Global symbol requires explicit package name" - Can't see why?

by Anonymous Monk
on Aug 24, 2015 at 22:40 UTC ( [id://1139748]=perlquestion: print w/replies, xml ) Need Help??

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

I'm struggling to find why I keep getting this error: "Global symbol "$t1" requires explicit package name" as I have $t1 written as "my $t1"... and I can't seem to see any other issues that might be causing the error. I may be horrendously blind right now (and the fix is probably simple...), care to lend a pair of kind eyes?
#!/usr/bin/perl use strict; use warnings; use threads; my $t1 = async { qx(perl process1); } my $t2 = async { qx(perl process2); } my @out1 = $t1->join; my @out2 = $t2->join; print "t1: @out1\n"; print "t2: @out2\n";
Thank you!
  • Comment on Frustrating Error: "Global symbol requires explicit package name" - Can't see why?
  • Download Code

Replies are listed 'Best First'.
Re: Frustrating Error: "Global symbol requires explicit package name" - Can't see why?
by stevieb (Canon) on Aug 24, 2015 at 23:00 UTC

    I think all you need to do is put a statement-ending semi-colon (;) after these two lines:

    my $t1 = async { qx(perl process1); } my $t2 = async { qx(perl process2); }

    ...so they look like this:

    my $t1 = async { qx(perl process1); }; my $t2 = async { qx(perl process2); };

    That's what it shows in the threads documentation, and works for me on one of my machines that has a threads capable perl.

    -stevieb

Re: Frustrating Error: "Global symbol requires explicit package name" - Can't see why?
by aitap (Curate) on Aug 25, 2015 at 07:33 UTC
    When you get a cryptic syntax error which you can't understand how you got, run through Basic debugging checklist, especially perl -MO=Deparse:
    $ perl -MO=Deparse #!/usr/bin/perl use strict; use warnings; use threads; my $t1 = async { qx(perl process1); } my $t2 = async { qx(perl process2); } my @out1 = $t1->join; __END__ Global symbol "$t1" requires explicit package name at - line 10. - had compilation errors. use threads; use warnings; use strict; my $t1 = async(sub { `perl process1`; } , my $t2 = async(sub { `perl process2`; } , my(@out1) = ${'t1'}->join));
    As Deparse clearly shows, absence of semicolons chained the calls to async and join together, making them arguments to each other.
Re: Frustrating Error: "Global symbol requires explicit package name" - Can't see why?
by RichardK (Parson) on Aug 24, 2015 at 23:07 UTC

    threads doc's have two potentially interesting things to say, that may just help.

    firstly, you seem to missing a semicolon or two.

    async BLOCK; "async" creates a thread to execute the block immediately foll +owing it. This block is treated as an anonymous subroutine, and so m +ust have a semicolon after the closing brace.

    also

    Threads are implemented in a way that make them easy to misuse. Few people know how to use them correctly or will be able to provide h +elp. The use of interpreter-based threads in perl is officially discouraged +.
Re: Frustrating Error: "Global symbol requires explicit package name" - Can't see why?
by AnomalousMonk (Archbishop) on Aug 24, 2015 at 23:52 UTC

    And, once you know it, it's easy to duplicate the error:

    c:\@Work\Perl\monks>perl -wMstrict -le "sub foo (&@) { return 42; } ;; my $x = foo { } print $x; " Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.


    Give a man a fish:  <%-{-{-{-<

      Yeah nothing specific about threads, just the fact that my declarations only start being effective after the next semicolon.

      This was done to allow constructs like

      my $x = $x++;

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        When I first read LanX's reply, I was confused because the statement all on its own is an error. But LanX /msg-ed me that the rule allows access to same-named lexicals in surrounding scopes:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 42; if ($x) { my $x = $x++; print $x; } print $x; " 42 43
        Now I understand.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1139748]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found