perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Angus Lees <>
Subject Re: UPD: output table with variable data
Date Fri, 21 Feb 2003 02:07:09 GMT

just some comments on perl/embperl style, none of which are
particularly critical:

At Thu, 20 Feb 2003 10:36:57 +0100, Alexander Hartmaier wrote:
> [-  use nactools; -]

this would be better done as [! use nactools !], since that will only
be executed once. when you use a [- -] block, perl will realise and
not actually reload nactools every time, but you can avoid even the
check by using [! !]

in general, put "use" and perl function declarations inside [! !]

> [$ sub interface_list $]
>   [- @interfacelist = @_; -]

this *copies* the entire array. since you don't modify @interfacelist
during this function (and so don't need a local copy), it is faster to
pass a reference to it instead (or perhaps just use @_ directly in the
following code. array ref is faster still, since perl won't have to
copy the array onto the argument stack in the first place).

ie: call function with interface_list(\@interface_list), and use
[- $ifref = $_[0] -] here. replace all $interfacelist[$n] with
$ifref->[$n] in the following code.

>   [$ if ($#interfacelist >= 0) $]

in perl, an array in scalar context returns a count of its elements. a
boolean expression (in an "if" condition, for example) is a scalar

the "perlish" way to write "if (this array has something in it)" is
simply: [$ if (@interfacelist) $]

(or [$ if (@$ifref) $] if using a reference)

>     <tr class=heading>
>       <th>Mon</th>
>       <th>Device</th>
>       <th>Interface</th>
>       <th>Speed</th>
>       <th>Line</th>
>     </tr>
>     [$ foreach $indx ( 0 .. $#interfacelist ) $]

in the following code, you don't need the actual *index*.  what you
want is a foreach loop that will iterate through each *element* of
@interfacelist.  in perl, this is usually written:
 [$ foreach $interface (@interfacelist) $]

this avoids having to repeatedly lookup the array index on each use,
so is a little faster too.

(or [$ foreach $interface (@$ifref) $] if using a reference)

>       <tr class=light>
>         <td class=[+ $interfacelist[$indx][0] +]><a>[+ $interfacelist[$indx][1]
> +]</a></td>

with the above foreach loop change, these expressions then become:
 [+ $interface->[0] +], [+ $interface->[1] +], etc

remembering that each element of an "array of arrays" is actually a
*reference* to the deeper array.


>       </tr>
>     [$ endforeach $]
>   [$ else $]
>     <tr><td colspan=5><a>no interfaces</a></td></tr>
>   [$ endif $]
> [$ endsub $]

> I'm coding perl and embperl for a year now but I still don't know wheter I
> should use an array/hash or a reference.
> Can somebody explain the pros/cons of each?

passing a reference avoids copying the entire array/hash.  in general,
its faster and more memory efficient to use a reference, except that
it also gets more fiddly later on.  in particular, *returning* a
reference rather than the array/hash itself leads to code like this:

  # get_interfaces() returns an array or () on error
  foreach (get_interfaces()) { ... }

  # vs. get_interfaces() returns an array ref or undef on error
  foreach (@{get_interfaces() || []}) { ... }

in general, always use an array ref if passing large amounts of data
since the copy will hurt.

for small amounts of data, its often easier on the programmer to use a
simple array/hash directly - you'll have to balance that against the
run-time cost.

 - Gus

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message