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: Advanced:
quiteing un-neccessary warnings

 



john
Deleted

Mar 16, 2000, 9:02 AM

Post #1 of 9 (3506 views)
quiteing un-neccessary warnings Can't Post

I access a "main" level object parameter in a subroutine. Like this:

...
use strict;
...
my $m = OBJ->new();
...
&routine($pass, \@some);
...
sub routine {
my($test, $valuelist_ref)=@_;
unless ($test = 'true') {
map({$_ .= $m->{suffix}} @{$valuelist_ref});
}

My problem is that perl warns that $m won't stay shared... I know this, I don't care. (And I'm not assigning to it anyway am I?) I only want perl to stop warning me about this though...


japhy
Enthusiast / Moderator

Mar 15, 2000, 10:33 PM

Post #2 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

Under what conditions are you getting this warning? What's your version of Perl? This sounds like you're defining a subroutine inside a subroutine:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl -w

use strict;

sub foo {
my $m = { suffix => '.html' };
my @array = ("foo","bar");
&routine(1, \@array);
print "@array";

sub routine {
my ($test, $iter) = @_;
if ($test) {
map { $_ .= $m->{suffix} } @$iter;
}
}
}
</pre><HR></BLOCKQUOTE>

This gives me the compile-time warning of Variable "$m" will not stay shared at john.pl line 14.

If you want this warning to be specifically turned off, there is a way to do that in Perl 5.6 (coming soon), and I have a module that lets you do it in versions of Perl BEFORE 5.6. If you're interested in it, let me know.


john
Deleted

Mar 15, 2000, 10:54 PM

Post #3 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

Apache 1.39; mod_perl 1.21; perl 5.005_3

The error is happening under registry.pm and that does wierd stuff with the namespace which is consistant with nested subr thing you mention.

I wouldn't mind so much if my logs weren't taking that warning with each hit. <G>

Here's another thing... this only happened after I cleaned up the code. I only pass the varible structures I'm going to change in a sub to the sub.

Do I use additional memory by passing a copy of the object to the sub? or is it like a reference? (I can get rid of the worning by passing the object to the sub.)

Thanks,
John


japhy
Enthusiast / Moderator

Mar 16, 2000, 11:11 AM

Post #4 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

You're not passing a copy of a reference
(or object) if you do:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$a = [1,2,3];
print "$a\n"; # ARRAY(0x12345)
foo($a);
sub foo { print "$_[0]\n" } # same string
</pre><HR></BLOCKQUOTE>

You're not passing a "copy", you're passing
a simple scalar, which holds the reference.


john
Deleted

Mar 16, 2000, 11:55 AM

Post #5 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

must I explicitly ...

%m = ( 'key' => "value" );
$m = \%m;
&foo($m);

or can I ...

%m = ( 'key' => "value" );
&foo(\%m);

isn't \%m a scalar? or am I as confused as I think I am?


john
Deleted

Mar 16, 2000, 12:11 PM

Post #6 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

yes... I am confused. my last question refered to %m. A hash had nothing to do with my original issue.

$m is an object (a blessed reference by definition, right?);

So I should be able to pass it without signifigant memory penalty.

I think. Maybe.

Thanks for the help,
John


Cure
User

Mar 17, 2000, 11:47 AM

Post #7 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

Hi John

\%m is a reference. And references are scalars.

$m = \%m => So, the $m is a scalar that holds the cell address of the %m.

An object in perl is known as a reference thingy.


Cure



john
Deleted

Mar 17, 2000, 11:57 AM

Post #8 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

I guess my real issue (which may be imagined or over-estimated) is if I do this:

my $m = OBJ->new();
&some($m);

sub some {
my $m = shift;
...
}

Does another instance of the object get created in memory? If so, is there an efficient way to get around this memory use?


Cure
User

Mar 17, 2000, 3:42 PM

Post #9 of 9 (3506 views)
Re: quiteing un-neccessary warnings [In reply to] Can't Post

Hi
John

No, The OBJECT stored in $main::m is really just a scalar reference to the hash. The my $m = shift makes a copy of the REFERENCE.
But the reference in $m points to the same object instance as $main::m.


Cure

 
 


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

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