top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

accessing variables in subroutines using perl

0 votes
304 views

The following example doesn't compile:

use strict;
use warnings;

sub test {
 print $counter . "n";
}

my $counter = 0;
while($counter < 5) {
 test();
 $counter++;
}

It says "Global symbol "$counter" requires explicit package name ...". When I put the subroutine after the 'while' loop, it works just fine, so what's the problem?

posted Jun 26, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

The subroutine does not see the lexical $counter variable because it was declared after its scope. For more information, see:
http://www.plover.com/FAQs/Namespaces.html

answer Jun 26, 2013 by anonymous
0 votes

You trying to print a variable $counter that was initialized outsider the scope of the subroutine.

answer Jun 26, 2013 by anonymous
Similar Questions
+1 vote

I would retrieve my hash table when i pass it in argument at a function. In my case, function1 return my hash table, then i pass my hash table in argument to my function2 and in my function2 i would retrieve my hash table for browse it.

sub function1{
    my code;
    return %hash;
}

sub function2{
    my %hash=$_[0];
    my code browse my hash;
}

my %hash = function1();
function2(%hash);

I have the following error : Odd number of elements in hash assignment at

+1 vote

I am facing some difficulty using join to display the array elements. Here is the code snippet

[code]use strict;use warnings
my @fruits = qw/apple mango orange banana guava/;
#print '[', join '][', @fruits;#print ']';
print '[', join '][', @fruits, ']';best,
[/code]

[output]
      [apple][mango][orange][banana][guava][]
[/output]

How can I make the output to eliminate the last empty square brackets [] using a single print statement. I used two print statements as shown in the code snippet above (#lines are commented out). Any help is greatly appreciated.

+1 vote

I have a Linux machine and windows machine, I need to run a GUI of Linux machine on the windows machine.
I don't want to use putty or any other software. I have to use any Perl modules to achieve this please help.

+1 vote

I need to access ClearQuest programatically using Perl. I just found ClearQuest OSLC REST API. Client is planning to use ClearQuest web 7.1.1. I read that it comes with OSLC REST API out the box.
Can someone please provide me some examples on how I can access the CQ through Perl using this API.

...