For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > Re: inherited perl program









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: inherited perl program
Mr. Shawn H. Corey

2006-07-07, 6:57 pm

Smith, Derek wrote:
> I have inherited a program that does snmp walks and gets, but within the
> code (keep in mind they turned off strict for references)
>
> I am failing to understand the push @$node_type "$name";
>
> The person who wrote this said with strict on the push statement above
> will not work...complains about a scalar is undefined. I said yes it
> will not b/c you can only push an array and $ is scalar, arrays in
> scalar context return the count of elements.
>
>
>
> His reply was well I am doing this b/c I need to create a dynamic array
> composed of all device types.
>
> Is this bad code as I have never seen push @$arrayname?
>
> Please explain what this is doing.
>


Yes, this is bad practice. The scalar $node_type contains strings from
@all_node_types. The expression @$node_type is a symbolic reference (see
`perldoc perlref`) to an array with the same name as the string. For
example, the first string is "CiscoAPVPN", so for it, @$node_type is the
same as @CiscoAPVPN. This is also why the statement 'no strict "refs";'
must be used; it allows symbolic references.

The preferred practice is to use a hash:

my %all_nodes = ();

....

push @{ $all_nodes{$node_type} }, $name;



--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by
doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com