CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error

 



nibalan
Novice

Jun 30, 2011, 4:46 AM

Post #1 of 8 (3162 views)
Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error Can't Post

I am writing a huge script where I am getting error in a particular chunk f code:

use strict;
use warning;
require manualans.pl
for(my $i=0;$i<($num);$i++)
{
my @val=$$reff{$options[$i]};
if ("@val" ne "--create-cache")
{
my $value="value"."_$i";
print $RES ("\nTESTING $options[$i]\n");
if("@val" eq "@$value")
{
print $RES ("RESULT\t\t\t\t\t-->\t\t\t\tPASS\n\n");
}
else
{
print $RES ("RESULT\t\t\t\t\t-->\t\t\t\tFAIL\n\n");
}
}
else
{
subrou::createcache;
}
}

manualans.pl contains:

@value_0="hi";
@value_1="create-cache";
-
-
-
-
1;

When I execute I get the error Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 7. error.

But when I dint use stricts and warnings module and did nt declare using my, I did not get any error. Please see the below code:

require manualans.pl
for( $i=0;$i<($num);$i++)
{
@val=$$reff{$options[$i]};
if ("@val" ne "--create-cache")
{
$value="value"."_$i";
print $RES ("\nTESTING $options[$i]\n");
if("@val" eq "@$value")
{
print $RES ("RESULT\t\t\t\t\t-->\t\t\t\tPASS\n\n");
}
else
{
print $RES ("RESULT\t\t\t\t\t-->\t\t\t\tFAIL\n\n");
}
}
else
{
subrou::createcache;
}
}


This will give the output without any error. I am facing the problem only when I use strict and warnings module. Can you please help me out?


rovf
Veteran

Jun 30, 2011, 5:16 AM

Post #2 of 8 (3160 views)
Re: [nibalan] Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error [In reply to] Can't Post

You are using

@$value

where $value is a string. This is forbidden under "use strict". There are two alternatives to solve this problem:

- Relax the strictness in that area of your program where are you using this feature, with

no strict 'refs';

I suggest that you do it in an enclosing block, as small as possible.

- Rewrite your program so that it does not need symbolic references. Very often you can do without them (though I admit that there are cases where this feature is handy).


FishMonger
Veteran / Moderator

Jun 30, 2011, 7:20 AM

Post #3 of 8 (3155 views)
Re: [rovf] Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error [In reply to] Can't Post

I have never seen a valid reason to use symbolic references.

A much better approach would be to use a hash instead of a symbolic reference.


rovf
Veteran

Jun 30, 2011, 8:01 AM

Post #4 of 8 (3152 views)
Re: [FishMonger] Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error [In reply to] Can't Post


Quote
I have never seen a valid reason to use symbolic references.


It depends on how far you stretch the meaning of 'valid' ;-)

It is true that you never *need* them, and it is also true that they are, in general, more dangerous than symbolic references. If you don't care about the latter, the code sometimes gets a bit "shorter" with symbolic references.

Here is a concrete example which I have seen in a project which has at least some good arguments for using symbolic references:

In this project, member functions (setter/getter) for classes are generated out of the blessed hash which represents the instance. The core of this "generator function" looks like this:


Code
      my $name = $Package."::".lc $key; 
*$name = sub {
$_[0]->{"$key"} = $_[1] if (defined $_[1]);
$_[0]->{"$key"};
};


In this example, $key is the name of the "data member" for which the setter and getter needs to be generated. This code uses a symbolic reference (*$name).

Of course there *are* alternatives to this approach, but I don't know of any alternative which wouldn't have some drawback in a different area, so this is something I could understand as valid application.


FishMonger
Veteran / Moderator

Jun 30, 2011, 9:33 PM

Post #5 of 8 (3107 views)
Re: [rovf] Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error [In reply to] Can't Post

I wouldn't call that a valid application.

I've never come across the need to dynamically create accessor/mutator methods, but I could envision the need. If I were to need this functionality, I'd use one of the modules in the Class:: namespace, such as Class::Accessor. http://search.cpan.org/~kasei/Class-Accessor-0.34/lib/Class/Accessor.pm


rovf
Veteran

Jul 1, 2011, 2:24 AM

Post #6 of 8 (3064 views)
Re: [FishMonger] Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error [In reply to] Can't Post

Gotcha! ;-)

If you look at the source code of, say, Class::Accessor, you will see that this code employs "no strict 'refs'" for exactly the same reason I pointed out.

The same is true for other modules which can be used for this task, for example Class::Struct (which is part of the standard distribution).

So, the question is IMO not so much whether there *are* occasional (rare) usages for using symbolic references - as we can see, they are -, but whether in the project I have seen, it was a good idea to manually implement the accessor functions, or whether to base the design on a module which already exists on CPAN.

I personally often use CPAN stuff, but it also happens that I find modules on CPAN which only halfway do what I want, and then I download them, rename them and change them in the way I want. The distinction between code I write and code I reuse gets a bit blurred in this way...


FishMonger
Veteran / Moderator

Jul 1, 2011, 6:19 AM

Post #7 of 8 (3052 views)
Re: [rovf] Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error [In reply to] Can't Post

Ouch, my bad. :-|

I just picked the first module that popped up and didn't look at its source.

I see this topic routinely coming up on other forums, such as perlmonks and beginners@perl.org (usenet). In those postings a number of well known module authors, for example Uri Guttman, have given good examples of how to achieve things like this without resorting to symbolic refs.

My main point is that if/when it's appropriate to use symbolic refs, it's clearly not an approach that we should be encouraging beginners to use.


rovf
Veteran

Jul 1, 2011, 8:02 AM

Post #8 of 8 (3046 views)
Re: [FishMonger] Can't use string ("value_0") as an ARRAY ref while "strict refs" in use at ch.pl line 8. error [In reply to] Can't Post

I can only agree!!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives